Skip to content

Commit a53214b

Browse files
move spawnAsPromised to test/common as spawnPromisified
1 parent 24157ab commit a53214b

31 files changed

+253
-317
lines changed

test/common/index.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
const process = global.process; // Some tests tamper with the process global.
2525

2626
const assert = require('assert');
27-
const { exec, execSync, spawnSync } = require('child_process');
27+
const { exec, execSync, spawnSync, spawn: spawnAsync } = require('child_process');
2828
const fs = require('fs');
2929
// Do not require 'os' until needed so that test-os-checked-function can
3030
// monkey patch it. If 'os' is required here, that test will fail.
@@ -842,6 +842,36 @@ function requireNoPackageJSONAbove(dir = __dirname) {
842842
}
843843
}
844844

845+
function spawnPromisified(...args) {
846+
let stderr = '';
847+
let stdout = '';
848+
849+
const child = spawnAsync(...args);
850+
child.stderr.setEncoding('utf8');
851+
child.stderr.on('data', (data) => { stderr += data; });
852+
child.stdout.setEncoding('utf8');
853+
child.stdout.on('data', (data) => { stdout += data; });
854+
855+
return new Promise((resolve, reject) => {
856+
child.on('close', (code, signal) => {
857+
resolve({
858+
code,
859+
signal,
860+
stderr,
861+
stdout,
862+
});
863+
});
864+
child.on('error', (code, signal) => {
865+
reject({
866+
code,
867+
signal,
868+
stderr,
869+
stdout,
870+
});
871+
});
872+
});
873+
}
874+
845875
const common = {
846876
allowGlobals,
847877
buildType,
@@ -891,6 +921,7 @@ const common = {
891921
skipIfEslintMissing,
892922
skipIfInspectorDisabled,
893923
skipIfWorker,
924+
spawnPromisified,
894925

895926
get enoughTestMem() {
896927
return require('os').totalmem() > 0x70000000; /* 1.75 Gb */

test/common/index.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const {
4848
getArrayBufferViews,
4949
getBufferSources,
5050
getTTYfd,
51-
runWithInvalidFD
51+
runWithInvalidFD,
52+
spawnPromisified,
5253
} = common;
5354

5455
export {
@@ -97,5 +98,6 @@ export {
9798
getBufferSources,
9899
getTTYfd,
99100
runWithInvalidFD,
100-
createRequire
101+
createRequire,
102+
spawnPromisified,
101103
};

test/es-module/helper.spawnAsPromised.mjs

-33
This file was deleted.

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

+45-48
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { mustCall } = require('../common');
3+
const { spawnPromisified } = require('../common');
44
const fixtures = require('../common/fixtures.js');
55
const assert = require('node:assert');
66
const path = require('node:path');
@@ -14,58 +14,55 @@ const pjson = path.resolve(
1414
fixtures.path('/es-modules/package-type-module/package.json')
1515
);
1616

17-
(async () => {
18-
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
1917

20-
describe('CJS ↔︎ ESM interop warnings', { concurrency: true }, () => {
18+
describe('CJS ↔︎ ESM interop warnings', { concurrency: true }, () => {
2119

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]);
20+
it(async () => {
21+
const required = path.resolve(
22+
fixtures.path('/es-modules/package-type-module/cjs.js')
23+
);
24+
const basename = 'cjs.js';
25+
const { code, signal, stderr } = await spawnPromisified(execPath, [requiringCjsAsEsm]);
2826

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-
);
27+
assert.ok(
28+
stderr.replaceAll('\r', '').includes(
29+
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringCjsAsEsm} not supported.\n`
30+
)
31+
);
32+
assert.ok(
33+
stderr.replaceAll('\r', '').includes(
34+
`Instead rename ${basename} to end in .cjs, change the requiring ` +
35+
'code to use dynamic import() which is available in all CommonJS ' +
36+
`modules, or change "type": "module" to "type": "commonjs" in ${pjson} to ` +
37+
'treat all .js files as CommonJS (using .mjs for all ES modules ' +
38+
'instead).\n'
39+
)
40+
);
4341

44-
assert.strictEqual(code, 1);
45-
assert.strictEqual(signal, null);
46-
});
42+
assert.strictEqual(code, 1);
43+
assert.strictEqual(signal, null);
44+
});
4745

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]);
46+
it(async () => {
47+
const required = path.resolve(
48+
fixtures.path('/es-modules/package-type-module/esm.js')
49+
);
50+
const basename = 'esm.js';
51+
const { code, signal, stderr } = await spawnPromisified(execPath, [requiringEsm]);
5452

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-
);
53+
assert.ok(
54+
stderr.replace(/\r/g, '').includes(
55+
`Error [ERR_REQUIRE_ESM]: require() of ES Module ${required} from ${requiringEsm} not supported.\n`
56+
)
57+
);
58+
assert.ok(
59+
stderr.replace(/\r/g, '').includes(
60+
`Instead change the require of ${basename} in ${requiringEsm} to` +
61+
' a dynamic import() which is available in all CommonJS modules.\n'
62+
)
63+
);
6664

67-
assert.strictEqual(code, 1);
68-
assert.strictEqual(signal, null);
69-
});
65+
assert.strictEqual(code, 1);
66+
assert.strictEqual(signal, null);
7067
});
71-
})().then(mustCall());
68+
});
+8-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { mustCall } = require('../common');
3+
const { spawnPromisified } = require('../common');
44
const fixtures = require('../common/fixtures.js');
55
const assert = require('node:assert');
66
const { execPath } = require('node:process');
@@ -9,16 +9,12 @@ const { describe, it } = require('node:test');
99

1010
const entry = fixtures.path('/es-modules/builtin-imports-case.mjs');
1111

12-
(async () => {
13-
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
12+
describe('ESM: importing builtins & CJS', () => {
13+
it('should work', async () => {
14+
const { code, signal, stdout } = await spawnPromisified(execPath, [entry]);
1415

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-
});
16+
assert.strictEqual(code, 0);
17+
assert.strictEqual(signal, null);
18+
assert.strictEqual(stdout, 'ok\n');
2319
});
24-
})().then(mustCall());
20+
});
+17-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
'use strict';
22

3-
const { mustCall } = require('../common');
3+
const { spawnPromisified } = require('../common');
44
const fixtures = require('../common/fixtures.js');
55
const assert = require('node:assert');
66
const { execPath } = require('node:process');
77
const { describe, it } = require('node:test');
88

99

10-
(async () => {
11-
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
10+
describe('ESM: importing CJS', { concurrency: true }, () => {
11+
it('should support valid CJS exports', async () => {
12+
const validEntry = fixtures.path('/es-modules/cjs-exports.mjs');
13+
const { code, signal, stdout } = await spawnPromisified(execPath, [validEntry]);
1214

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-
});
15+
assert.strictEqual(code, 0);
16+
assert.strictEqual(signal, null);
17+
assert.strictEqual(stdout, 'ok\n');
18+
});
2219

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]);
20+
it('should eror on invalid CJS exports', async () => {
21+
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
22+
const { code, signal, stderr } = await spawnPromisified(execPath, [invalidEntry]);
2623

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-
});
24+
assert.strictEqual(code, 1);
25+
assert.strictEqual(signal, null);
26+
assert.ok(stderr.includes('Warning: To load an ES module'));
27+
assert.ok(stderr.includes('Unexpected token \'export\''));
3228
});
33-
})().then(mustCall());
29+
});

test/es-module/test-esm-cjs-load-error-note.mjs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import '../common/index.mjs';
1+
import { spawnPromisified } from '../common/index.mjs';
22
import * as fixtures from '../common/fixtures.mjs';
33
import assert from 'node:assert';
44
import { execPath } from 'node:process';
55
import { describe, it } from 'node:test';
66

7-
import spawn from './helper.spawnAsPromised.mjs';
8-
97

108
// Expect note to be included in the error output
119
const expectedNote = 'To load an ES module, ' +
@@ -21,7 +19,7 @@ const mustNotIncludeMessage = {
2119
includeNote: false,
2220
};
2321

24-
describe('ESM: ', { concurrently: true }, () => {
22+
describe('ESM: Errors for unexpected exports', { concurrently: true }, () => {
2523
for (
2624
const { errorNeedle, filePath, getMessage, includeNote }
2725
of [
@@ -82,7 +80,7 @@ describe('ESM: ', { concurrently: true }, () => {
8280
]
8381
) {
8482
it(`should ${includeNote ? '' : 'NOT'} include note`, async () => {
85-
const { code, stderr } = await spawn(execPath, [filePath]);
83+
const { code, stderr } = await spawnPromisified(execPath, [filePath]);
8684

8785
assert.strictEqual(code, 1);
8886

test/es-module/test-esm-cjs-main.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
'use strict';
22

3-
const { mustCall } = require('../common');
3+
const { spawnPromisified } = require('../common');
44
const fixtures = require('../common/fixtures.js');
55
const assert = require('node:assert');
66
const { execPath } = require('node:process');
77
const { describe, it } = require('node:test');
88

99

10-
(async () => {
11-
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
10+
describe('ESM: importing CJS', () => {
11+
it('should work', async () => {
12+
const { code, signal, stdout } = await spawnPromisified(execPath, [
13+
fixtures.path('/es-modules/cjs.js'),
14+
]);
1215

13-
describe('ESM: importing CJS', () => {
14-
it('should work', async () => {
15-
const { code, signal, stdout } = await spawn(execPath, [
16-
fixtures.path('/es-modules/cjs.js'),
17-
]);
18-
19-
assert.strictEqual(code, 0);
20-
assert.strictEqual(signal, null);
21-
assert.strictEqual(stdout, 'executed\n');
22-
});
16+
assert.strictEqual(code, 0);
17+
assert.strictEqual(signal, null);
18+
assert.strictEqual(stdout, 'executed\n');
2319
});
24-
})().then(mustCall());
20+
});
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
'use strict';
22

3-
const { mustCall } = require('../common');
3+
const { spawnPromisified } = require('../common');
44
const fixtures = require('../common/fixtures.js');
55
const assert = require('node:assert');
66
const { execPath } = require('node:process');
77
const { describe, it } = require('node:test');
88

99

10-
(async () => {
11-
const { default: spawn } = await import('./helper.spawnAsPromised.mjs');
10+
describe('ESM: importing an encoded path', () => {
11+
it('should throw', async () => {
12+
const { code } = await spawnPromisified(execPath, [
13+
fixtures.path('es-module-url/native.mjs'),
14+
]);
1215

13-
describe('ESM: importing an encoded path', () => {
14-
it('should throw', async () => {
15-
const { code } = await spawn(execPath, [
16-
fixtures.path('es-module-url/native.mjs'),
17-
]);
18-
19-
assert.strictEqual(code, 1);
20-
});
16+
assert.strictEqual(code, 1);
2117
});
22-
})().then(mustCall());
18+
});

0 commit comments

Comments
 (0)