Skip to content

Commit

Permalink
Explicit and separate CJS and ESM entrypoints
Browse files Browse the repository at this point in the history
Fixes #2671.

AVA now only has a default export: the test() function.
  • Loading branch information
novemberborn committed Mar 28, 2021
1 parent cc4e622 commit 5e08444
Show file tree
Hide file tree
Showing 147 changed files with 174 additions and 206 deletions.
7 changes: 7 additions & 0 deletions ava.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 👉 Due to the package exports, XO's use of our ESLint plugin loads this
// file using the AVA code in the repository, but our self-hosted tests use the
// installed "test-ava" version.
module.exports = {
files: ['test/**', '!test/**/{fixtures,helpers}/**'],
ignoredByWatcher: ['{coverage,docs,media,test-d,test-tap}/**']
};
4 changes: 0 additions & 4 deletions ava.config.js

This file was deleted.

3 changes: 0 additions & 3 deletions cli.js

This file was deleted.

3 changes: 3 additions & 0 deletions entrypoints/cli.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
import * as cli from '../lib/cli.js'; // eslint-disable-line import/extensions
cli.run();
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const v8 = require('v8');
const {Worker} = require('worker_threads');

const {classify, hasExtension, isHelperish, matches, normalizeFileForMatching, normalizePatterns} = require('./lib/globs');
const {classify, hasExtension, isHelperish, matches, normalizeFileForMatching, normalizePatterns} = require('../lib/globs');

const MAX_DATA_LENGTH_EXCLUSIVE = 100 * 1024; // Allocate 100 KiB to exchange globs.

Expand Down
2 changes: 2 additions & 0 deletions entrypoints/main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require('../lib/worker/main');
2 changes: 2 additions & 0 deletions entrypoints/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import test from '../lib/worker/main.js'; // eslint-disable-line import/extensions
export default test;
2 changes: 2 additions & 0 deletions entrypoints/plugin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require('../lib/worker/plugin');
4 changes: 4 additions & 0 deletions entrypoints/plugin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
import * as plugin from '../lib/worker/plugin.js'; // eslint-disable-line import/extensions
const {registerSharedWorker} = plugin;
export {registerSharedWorker};
30 changes: 0 additions & 30 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,33 +683,3 @@ declare const test: TestInterface;

/** Call to declare a test, or chain to declare hooks or test modifiers */
export default test;

/** Call to declare a hook that is run once, after all tests have passed, or chain to declare modifiers. */
export const after: AfterInterface;

/** Call to declare a hook that is run after each passing test, or chain to declare modifiers. */
export const afterEach: AfterInterface;

/** Call to declare a hook that is run once, before all tests, or chain to declare modifiers. */
export const before: BeforeInterface;

/** Call to declare a hook that is run before each test, or chain to declare modifiers. */
export const beforeEach: BeforeInterface;

/** Call to declare a test that is expected to fail, or chain to declare modifiers. */
export const failing: FailingInterface;

/** Call to declare a test that is run exclusively, along with other tests declared with `.only()`. */
export const only: OnlyInterface;

/** Call to declare a serial test, or chain to declare serial hooks or test modifiers. */
export const serial: SerialInterface;

/** Skip this test. */
export const skip: SkipInterface;

/** Declare a test that should be implemented later. */
export const todo: TodoDeclaration;

/** Meta data associated with the current process. */
export const meta: MetaInterface;
2 changes: 0 additions & 2 deletions index.js

This file was deleted.

19 changes: 1 addition & 18 deletions lib/worker/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
'use strict';
const runner = require('./base').getRunner();
const makeCjsExport = () => {
function test(...args) {
return runner.chain(...args);
}

return Object.assign(test, runner.chain);
};

// Support CommonJS modules by exporting a test function that can be fully
// chained. Also support ES module loaders by exporting __esModule and a
// default. Support `import * as ava from 'ava'` use cases by exporting a
// `test` member. Do all this whilst preventing `test.test.test() or
// `test.default.test()` chains, though in CommonJS `test.test()` is
// unavoidable.
module.exports = Object.assign(makeCjsExport(), {
__esModule: true,
default: runner.chain
});
module.exports = runner.chain;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
"repository": "avajs/ava",
"homepage": "https://avajs.dev",
"bin": {
"ava": "cli.js"
"ava": "./entrypoints/cli.mjs"
},
"exports": {
".": {
"import": "./entrypoints/main.mjs",
"require": "./entrypoints/main.cjs"
},
"./eslint-plugin-helper": "./entrypoints/eslint-plugin-helper.cjs",
"./plugin": {
"import": "./entrypoints/plugin.mjs",
"require": "./entrypoints/plugin.cjs"
}
},
"engines": {
"node": ">=12.20 <13 || >=14.15 <15 || >=15"
Expand All @@ -16,10 +27,8 @@
"test": "xo && tsd && npm run -s cover"
},
"files": [
"entrypoints",
"lib",
"*.js",
"!*.config.js",
"index.d.ts",
"*.d.ts"
],
"keywords": [
Expand Down
2 changes: 0 additions & 2 deletions plugin.js

This file was deleted.

2 changes: 1 addition & 1 deletion test-d/assertions-as-type-guards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import test from '..';
import test from '..'; // eslint-disable-line import/no-unresolved

type Expected = {foo: 'bar'};
const expected: Expected = {foo: 'bar'};
Expand Down
2 changes: 1 addition & 1 deletion test-d/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectError, expectType} from 'tsd';
import anyTest, {Macro, TestInterface} from '..';
import anyTest, {Macro, TestInterface} from '..'; // eslint-disable-line import/no-unresolved

interface Context {
foo: string;
Expand Down
2 changes: 1 addition & 1 deletion test-d/like.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from '..';
import test from '..'; // eslint-disable-line import/no-unresolved

test('like', t => {
t.like({
Expand Down
2 changes: 1 addition & 1 deletion test-d/macros.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import test, {ExecutionContext, Macro} from '..';
import test, {ExecutionContext, Macro} from '..'; // eslint-disable-line import/no-unresolved

// Explicitly type as a macro.
{
Expand Down
2 changes: 1 addition & 1 deletion test-d/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import * as plugin from '../plugin';
import * as plugin from '../plugin'; // eslint-disable-line import/no-unresolved

expectType<plugin.SharedWorker.Plugin.Experimental.Protocol>(plugin.registerSharedWorker({filename: '', supportedProtocols: ['experimental']}));

Expand Down
2 changes: 1 addition & 1 deletion test-d/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectError} from 'tsd';
import test from '..';
import test from '..'; // eslint-disable-line import/no-unresolved

test('snapshot', t => {
t.snapshot({foo: 'bar'});
Expand Down
2 changes: 1 addition & 1 deletion test-d/throws.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import test from '..';
import test from '..'; // eslint-disable-line import/no-unresolved

class CustomError extends Error {
foo: string;
Expand Down
2 changes: 1 addition & 1 deletion test-d/try-commit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import test, {ExecutionContext, Macro} from '..';
import test, {ExecutionContext, Macro} from '..'; // eslint-disable-line import/no-unresolved

test('attempt', async t => {
const attempt = await t.try(
Expand Down
2 changes: 1 addition & 1 deletion test-tap/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ for (const opt of opts) {
});
return api.run({files: [path.join(__dirname, 'fixture', 'meta.js')]})
.then(runStatus => {
t.is(runStatus.stats.passedTests, 3);
t.is(runStatus.stats.passedTests, 2);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test-tap/eslint-plugin-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const path = require('path');
const {test} = require('tap');

const {load} = require('../eslint-plugin-helper');
const {load} = require('../entrypoints/eslint-plugin-helper.cjs');

const projectDir = path.join(__dirname, 'fixture/eslint-plugin-helper');
const overrideDir = path.join(__dirname, 'fixture/eslint-plugin-helper/for-overriding');
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/_generate-source-map-initial.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const babel = require('@babel/core');

const transformed = babel.transform(`
import {mapFile} from 'source-map-fixtures';
import test from '../../';
import test from '../../entrypoints/main.cjs';
const fixture = mapFile('throws').require();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/caching/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
t.true(2 + 2 === 4);
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/cjs.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../..');
const test = require('../../entrypoints/main.cjs');

test('pass', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/enhanced-assertion-formatting.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../..');
const test = require('../../entrypoints/main.cjs');

const foo = 'foo';

Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/error-in-anonymous-function.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../..');
const test = require('../../entrypoints/main.cjs');

const getAnonymousFn = () => () => {
throw new Error();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/es2015.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../..');
const test = require('../../entrypoints/main.cjs');

test('test', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/extensions/test.foo.bar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

const one = {one: 1};
const two = {two: 2};
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/crash/crashes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require('../../../..'); // eslint-disable-line import/no-unassigned-import
require('../../../../entrypoints/main.cjs'); // eslint-disable-line import/no-unassigned-import

process.exit(1); // eslint-disable-line unicorn/no-process-exit
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/crash/passes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('first pass', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/multiple-files/fails.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('first pass', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/multiple-files/passes-slow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {parentPort} = require('worker_threads');
const pEvent = require('p-event');
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test.serial('first pass', async t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/multiple-files/passes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('first pass', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/single-file/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('first pass', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/timeout/fails.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const delay = require('delay');
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('slow pass', async t => {
await delay(1000);
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/timeout/passes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('first pass', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/without-error/a.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('pass', t => t.pass());
2 changes: 1 addition & 1 deletion test-tap/fixture/fail-fast/without-error/b.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

setTimeout(() => {
test.serial('pass', t => t.pass());
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/formatting-color.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../..');
const test = require('../../entrypoints/main.cjs');

test('test', t => {
t.deepEqual({foo: 1}, {foo: 2});
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/hooks-skipped.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../..');
const test = require('../../entrypoints/main.cjs');

test.before(() => {
throw new Error('should not run');
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/ignored-dirs/helpers/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../../..');
const test = require('../../../../entrypoints/main.cjs');

test('test', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/ignored-dirs/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('pass', t => t.pass());
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
try {
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/improper-t-throws/caught-and-leaked.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
try {
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/improper-t-throws/caught.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
try {
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/improper-t-throws/leaked-from-promise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
try {
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/improper-t-throws/promise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
return Promise.resolve().then(() => {
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/improper-t-throws/throws.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', t => {
t.throws(throwSync());
Expand Down
2 changes: 1 addition & 1 deletion test-tap/fixture/improper-t-throws/unhandled-rejection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const delay = require('delay');
const test = require('../../..');
const test = require('../../../entrypoints/main.cjs');

test('test', async t => {
Promise.resolve().then(() => {
Expand Down
Loading

0 comments on commit 5e08444

Please sign in to comment.