Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mostly hidden test.default that provides test #2836

Merged
merged 1 commit into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/create-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,28 @@ export default function createChain(fn, defaults, meta) {

root.meta = meta;

// Our type definition uses ESM syntax; when using CJS with VSCode, the
// auto-completion assumes the root is accessed through `require('ava').default`.
// Placate VSCode by adding a mostly hidden default property on the root.
// This is available through both CJS and ESM imports. We use a proxy so that
// we don't end up with root.default.default.default chains.
Object.defineProperty(root, 'default', {
configurable: false,
enumerable: false,
writable: false,
value: new Proxy(root, {
apply(target, thisArg, argumentsList) {
target.apply(thisArg, argumentsList);
},
get(target, prop) {
if (prop === 'default') {
throw new TypeError('Cannot access default.default');
}

return target[prop];
},
}),
});

return root;
}
7 changes: 7 additions & 0 deletions test/cjs-default/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ava": {
"files": [
"*.cjs"
]
}
}
19 changes: 19 additions & 0 deletions test/cjs-default/fixtures/test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const test = require('ava');

test.default('callable', t => { // eslint-disable-line ava/no-unknown-modifiers
t.pass();
});

test('no recursion', t => {
t.throws(() => test.default.default, {
name: 'TypeError',
});
});

test('not enumerable', t => {
t.false(Object.keys(test).includes('default'));
});

test('main export equals the ESM export', async t => {
t.is(test, (await import('ava')).default); // eslint-disable-line node/no-unsupported-features/es-syntax
});
7 changes: 7 additions & 0 deletions test/cjs-default/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from '@ava/test';

import {fixture} from '../helpers/exec.js';

test('ok', async t => {
await t.notThrowsAsync(fixture());
});