Skip to content

Commit 3f6f977

Browse files
convert tests to node:test & revert ESM-ification of CJS tests
1 parent 7b4a2b1 commit 3f6f977

20 files changed

+738
-637
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
require('../common/index.js');
4+
const fixtures = require('../common/fixtures.js');
5+
const assert = require('node:assert');
6+
const path = require('node:path');
7+
const { execPath } = require('node:process');
8+
const { describe, it } = require('node:test');
9+
10+
11+
const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js'));
12+
const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js'));
13+
const pjson = path.resolve(
14+
fixtures.path('/es-modules/package-type-module/package.json')
15+
);
16+
17+
(async () => {
18+
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
19+
20+
describe('CJS ↔︎ ESM interop warnings', { concurrency: true }, () => {
21+
22+
it(async () => {
23+
const required = path.resolve(
24+
fixtures.path('/es-modules/package-type-module/cjs.js')
25+
);
26+
const basename = 'cjs.js';
27+
const { code, signal, stderr } = await spawn(execPath, [requiringCjsAsEsm]);
28+
29+
assert.ok(
30+
stderr.replaceAll('\r', '').includes(
31+
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringCjsAsEsm} not supported.\n`
32+
)
33+
);
34+
assert.ok(
35+
stderr.replaceAll('\r', '').includes(
36+
`Instead rename ${basename} to end in .cjs, change the requiring ` +
37+
'code to use dynamic import() which is available in all CommonJS ' +
38+
`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` +
39+
'treat all .js files as CommonJS (using .mjs for all ES modules ' +
40+
'instead).\n'
41+
)
42+
);
43+
44+
assert.strictEqual(code, 1);
45+
assert.strictEqual(signal, null);
46+
});
47+
48+
it(async () => {
49+
const required = path.resolve(
50+
fixtures.path('/es-modules/package-type-module/esm.js')
51+
);
52+
const basename = 'esm.js';
53+
const { code, signal, stderr } = await spawn(execPath, [requiringEsm]);
54+
55+
assert.ok(
56+
stderr.replace(/\r/g, '').includes(
57+
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringEsm} not supported.\n`
58+
)
59+
);
60+
assert.ok(
61+
stderr.replace(/\r/g, '').includes(
62+
`Instead change the require of ${basename} in ${requiringEsm} to` +
63+
' a dynamic import() which is available in all CommonJS modules.\n'
64+
)
65+
);
66+
67+
assert.strictEqual(code, 1);
68+
assert.strictEqual(signal, null);
69+
});
70+
});
71+
})();

test/es-module/test-cjs-esm-warn.mjs

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
require('../common/index.js');
4+
const fixtures = require('../common/fixtures.js');
5+
const assert = require('node:assert');
6+
const { execPath } = require('node:process');
7+
const { describe, it } = require('node:test');
8+
9+
10+
const entry = fixtures.path('/es-modules/builtin-imports-case.mjs');
11+
12+
(async () => {
13+
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
14+
15+
describe('ESM: importing builtins & CJS', () => {
16+
it('should work', async () => {
17+
const { code, signal, stdout } = await spawn(execPath, [entry]);
18+
19+
assert.strictEqual(code, 0);
20+
assert.strictEqual(signal, null);
21+
assert.strictEqual(stdout, 'ok\n');
22+
});
23+
});
24+
})();

test/es-module/test-esm-cjs-builtins.mjs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
require('../common/index.js');
4+
const fixtures = require('../common/fixtures.js');
5+
const assert = require('node:assert');
6+
const { execPath } = require('node:process');
7+
const { describe, it } = require('node:test');
8+
9+
10+
(async () => {
11+
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
12+
13+
describe('ESM: importing CJS', { concurrency: true }, () => {
14+
it('should support valid CJS exports', async () => {
15+
const validEntry = fixtures.path('/es-modules/cjs-exports.mjs');
16+
const { code, signal, stdout } = await spawn(execPath, [validEntry]);
17+
18+
assert.strictEqual(code, 0);
19+
assert.strictEqual(signal, null);
20+
assert.strictEqual(stdout, 'ok\n');
21+
});
22+
23+
it('should eror on invalid CJS exports', async () => {
24+
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
25+
const { code, signal, stderr } = await spawn(execPath, [invalidEntry]);
26+
27+
assert.strictEqual(code, 1);
28+
assert.strictEqual(signal, null);
29+
assert.ok(stderr.includes('Warning: To load an ES module'));
30+
assert.ok(stderr.includes('Unexpected token \'export\''));
31+
});
32+
});
33+
})();

test/es-module/test-esm-cjs-exports.mjs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { mustCall } from '../common/index.mjs';
1+
import '../common/index.mjs';
22
import * as fixtures from '../common/fixtures.mjs';
33
import assert from 'node:assert';
44
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
56

67
import spawn from './helper.spawnAsPromised.mjs';
78

@@ -20,57 +21,69 @@ const mustNotIncludeMessage = {
2021
includeNote: false,
2122
};
2223

23-
for (
24-
const { errorNeedle, filePath, getMessage, includeNote }
25-
of [
26-
{
27-
filePath: fixtures.path('/es-modules/es-note-unexpected-export-1.cjs'),
28-
...mustIncludeMessage,
29-
},
30-
{
31-
filePath: fixtures.path('/es-modules/es-note-unexpected-import-1.cjs'),
32-
...mustIncludeMessage,
33-
},
34-
{
35-
filePath: fixtures.path('/es-modules/es-note-promiserej-import-2.cjs'),
36-
...mustNotIncludeMessage,
37-
},
38-
{
39-
filePath: fixtures.path('/es-modules/es-note-unexpected-import-3.cjs'),
40-
...mustIncludeMessage,
41-
},
42-
{
43-
filePath: fixtures.path('/es-modules/es-note-unexpected-import-4.cjs'),
44-
...mustIncludeMessage,
45-
},
46-
{
47-
filePath: fixtures.path('/es-modules/es-note-unexpected-import-5.cjs'),
48-
...mustNotIncludeMessage,
49-
},
50-
{
51-
filePath: fixtures.path('/es-modules/es-note-error-1.mjs'),
52-
...mustNotIncludeMessage,
53-
errorNeedle: 'Error: some error',
54-
},
55-
{
56-
filePath: fixtures.path('/es-modules/es-note-error-2.mjs'),
57-
...mustNotIncludeMessage,
58-
errorNeedle: 'string',
59-
},
60-
{
61-
filePath: fixtures.path('/es-modules/es-note-error-3.mjs'),
62-
...mustNotIncludeMessage,
63-
errorNeedle: 'null',
64-
},
65-
{
66-
filePath: fixtures.path('/es-modules/es-note-error-4.mjs'),
67-
...mustNotIncludeMessage,
68-
errorNeedle: 'undefined',
69-
},
70-
]
71-
) {
72-
spawn(execPath, [filePath])
73-
.then(mustCall(({ code, stderr }) => {
24+
describe('ESM: ', { concurrently: true }, () => {
25+
for (
26+
const { errorNeedle, filePath, getMessage, includeNote }
27+
of [
28+
{
29+
// name: '',
30+
filePath: fixtures.path('/es-modules/es-note-unexpected-export-1.cjs'),
31+
...mustIncludeMessage,
32+
},
33+
{
34+
// name: '',
35+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-1.cjs'),
36+
...mustIncludeMessage,
37+
},
38+
{
39+
// name: '',
40+
filePath: fixtures.path('/es-modules/es-note-promiserej-import-2.cjs'),
41+
...mustNotIncludeMessage,
42+
},
43+
{
44+
// name: '',
45+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-3.cjs'),
46+
...mustIncludeMessage,
47+
},
48+
{
49+
// name: '',
50+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-4.cjs'),
51+
...mustIncludeMessage,
52+
},
53+
{
54+
// name: '',
55+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-5.cjs'),
56+
...mustNotIncludeMessage,
57+
},
58+
{
59+
// name: '',
60+
filePath: fixtures.path('/es-modules/es-note-error-1.mjs'),
61+
...mustNotIncludeMessage,
62+
errorNeedle: 'Error: some error',
63+
},
64+
{
65+
// name: '',
66+
filePath: fixtures.path('/es-modules/es-note-error-2.mjs'),
67+
...mustNotIncludeMessage,
68+
errorNeedle: 'string',
69+
},
70+
{
71+
// name: '',
72+
filePath: fixtures.path('/es-modules/es-note-error-3.mjs'),
73+
...mustNotIncludeMessage,
74+
errorNeedle: 'null',
75+
},
76+
{
77+
// name: '',
78+
filePath: fixtures.path('/es-modules/es-note-error-4.mjs'),
79+
...mustNotIncludeMessage,
80+
errorNeedle: 'undefined',
81+
},
82+
]
83+
) {
84+
it(`should ${includeNote ? '' : 'NOT'} include note`, async () => {
85+
const { code, stderr } = await spawn(execPath, [filePath]);
86+
7487
assert.strictEqual(code, 1);
7588

7689
if (errorNeedle) stderr.includes(errorNeedle);
@@ -80,5 +93,6 @@ for (
8093
includeNote ? includesNote : !includesNote,
8194
`${filePath} ${getMessage(stderr)}`,
8295
);
83-
}));
84-
}
96+
});
97+
}
98+
});

0 commit comments

Comments
 (0)