-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor ESM tests to improve performance
PR-URL: #43784 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
1 parent
728e18e
commit b19564b
Showing
56 changed files
with
1,390 additions
and
1,442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,68 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const { spawn } = require('child_process'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const { spawnPromisified } = require('../common'); | ||
const fixtures = require('../common/fixtures.js'); | ||
const assert = require('node:assert'); | ||
const path = require('node:path'); | ||
const { execPath } = require('node:process'); | ||
const { describe, it } = require('node:test'); | ||
|
||
|
||
const requiringCjsAsEsm = path.resolve(fixtures.path('/es-modules/cjs-esm.js')); | ||
const requiringEsm = path.resolve(fixtures.path('/es-modules/cjs-esm-esm.js')); | ||
const pjson = path.resolve( | ||
fixtures.path('/es-modules/package-type-module/package.json') | ||
); | ||
|
||
{ | ||
const required = path.resolve( | ||
fixtures.path('/es-modules/package-type-module/cjs.js') | ||
); | ||
const basename = 'cjs.js'; | ||
const child = spawn(process.execPath, [requiringCjsAsEsm]); | ||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
|
||
describe('CJS ↔︎ ESM interop warnings', { concurrency: true }, () => { | ||
|
||
it(async () => { | ||
const required = path.resolve( | ||
fixtures.path('/es-modules/package-type-module/cjs.js') | ||
); | ||
const basename = 'cjs.js'; | ||
const { code, signal, stderr } = await spawnPromisified(execPath, [requiringCjsAsEsm]); | ||
|
||
assert.ok( | ||
stderr.replaceAll('\r', '').includes( | ||
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringCjsAsEsm} not supported.\n` | ||
) | ||
); | ||
assert.ok( | ||
stderr.replaceAll('\r', '').includes( | ||
`Instead rename ${basename} to end in .cjs, change the requiring ` + | ||
'code to use dynamic import() which is available in all CommonJS ' + | ||
`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` + | ||
'treat all .js files as CommonJS (using .mjs for all ES modules ' + | ||
'instead).\n' | ||
) | ||
); | ||
|
||
assert.strictEqual(code, 1); | ||
assert.strictEqual(signal, null); | ||
}); | ||
|
||
assert.ok(stderr.replaceAll('\r', '').includes( | ||
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${ | ||
requiringCjsAsEsm} not supported.\n`)); | ||
assert.ok(stderr.replaceAll('\r', '').includes( | ||
`Instead rename ${basename} to end in .cjs, change the requiring ` + | ||
'code to use dynamic import() which is available in all CommonJS ' + | ||
`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` + | ||
'treat all .js files as CommonJS (using .mjs for all ES modules ' + | ||
'instead).\n')); | ||
})); | ||
} | ||
it(async () => { | ||
const required = path.resolve( | ||
fixtures.path('/es-modules/package-type-module/esm.js') | ||
); | ||
const basename = 'esm.js'; | ||
const { code, signal, stderr } = await spawnPromisified(execPath, [requiringEsm]); | ||
|
||
assert.ok( | ||
stderr.replace(/\r/g, '').includes( | ||
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringEsm} not supported.\n` | ||
) | ||
); | ||
assert.ok( | ||
stderr.replace(/\r/g, '').includes( | ||
`Instead change the require of ${basename} in ${requiringEsm} to` + | ||
' a dynamic import() which is available in all CommonJS modules.\n' | ||
) | ||
); | ||
|
||
{ | ||
const required = path.resolve( | ||
fixtures.path('/es-modules/package-type-module/esm.js') | ||
); | ||
const basename = 'esm.js'; | ||
const child = spawn(process.execPath, [requiringEsm]); | ||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 1); | ||
assert.strictEqual(signal, null); | ||
|
||
assert.ok(stderr.replace(/\r/g, '').includes( | ||
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${ | ||
requiringEsm} not supported.\n`)); | ||
assert.ok(stderr.replace(/\r/g, '').includes( | ||
`Instead change the require of ${basename} in ${requiringEsm} to` + | ||
' a dynamic import() which is available in all CommonJS modules.\n')); | ||
})); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const { spawn } = require('child_process'); | ||
const assert = require('assert'); | ||
const { spawnPromisified } = require('../common'); | ||
const fixtures = require('../common/fixtures.js'); | ||
const assert = require('node:assert'); | ||
const { execPath } = require('node:process'); | ||
const { describe, it } = require('node:test'); | ||
|
||
|
||
const entry = fixtures.path('/es-modules/builtin-imports-case.mjs'); | ||
|
||
const child = spawn(process.execPath, [entry]); | ||
child.stderr.setEncoding('utf8'); | ||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
describe('ESM: importing builtins & CJS', () => { | ||
it('should work', async () => { | ||
const { code, signal, stdout } = await spawnPromisified(execPath, [entry]); | ||
|
||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'ok\n'); | ||
}); | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'ok\n'); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const { spawn } = require('child_process'); | ||
const assert = require('assert'); | ||
const { spawnPromisified } = require('../common'); | ||
const fixtures = require('../common/fixtures.js'); | ||
const assert = require('node:assert'); | ||
const { execPath } = require('node:process'); | ||
const { describe, it } = require('node:test'); | ||
|
||
const entry = fixtures.path('/es-modules/cjs-exports.mjs'); | ||
|
||
let child = spawn(process.execPath, [entry]); | ||
child.stderr.setEncoding('utf8'); | ||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'ok\n'); | ||
})); | ||
describe('ESM: importing CJS', { concurrency: true }, () => { | ||
it('should support valid CJS exports', async () => { | ||
const validEntry = fixtures.path('/es-modules/cjs-exports.mjs'); | ||
const { code, signal, stdout } = await spawnPromisified(execPath, [validEntry]); | ||
|
||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
assert.strictEqual(stdout, 'ok\n'); | ||
}); | ||
|
||
it('should eror on invalid CJS exports', async () => { | ||
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs'); | ||
const { code, signal, stderr } = await spawnPromisified(execPath, [invalidEntry]); | ||
|
||
const entryInvalid = fixtures.path('/es-modules/cjs-exports-invalid.mjs'); | ||
child = spawn(process.execPath, [entryInvalid]); | ||
let stderr = ''; | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { | ||
stderr += data; | ||
assert.strictEqual(code, 1); | ||
assert.strictEqual(signal, null); | ||
assert.ok(stderr.includes('Warning: To load an ES module')); | ||
assert.ok(stderr.includes('Unexpected token \'export\'')); | ||
}); | ||
}); | ||
child.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 1); | ||
assert.strictEqual(signal, null); | ||
assert.ok(stderr.includes('Warning: To load an ES module')); | ||
assert.ok(stderr.includes('Unexpected token \'export\'')); | ||
})); |
Oops, something went wrong.