|
1 | 1 | import { mustCall } from '../common/index.mjs'; |
2 | 2 | import { fileURL, path } from '../common/fixtures.mjs'; |
3 | 3 | import { match, ok, notStrictEqual, strictEqual } from 'assert'; |
4 | | -import { spawn } from 'child_process'; |
5 | | -import { execPath } from 'process'; |
| 4 | +import { execPath } from 'node:process'; |
| 5 | +import { describe, it } from 'node:test'; |
6 | 6 |
|
7 | | -{ |
8 | | - const child = spawn(execPath, [ |
9 | | - '--experimental-loader', |
10 | | - fileURL('es-module-loaders', 'thenable-load-hook.mjs').href, |
11 | | - path('es-modules', 'test-esm-ok.mjs'), |
12 | | - ]); |
| 7 | +import spawn from './helper.spawnAsPromised.mjs'; |
| 8 | + |
| 9 | + |
| 10 | +describe('ESM: thenable loader hooks', { concurrency: true }, () => { |
| 11 | + it('should behave as a normal promise resolution', async () => { |
| 12 | + const { code, stderr} = await spawn(execPath, [ |
| 13 | + '--experimental-loader', |
| 14 | + fileURL('es-module-loaders', 'thenable-load-hook.mjs').href, |
| 15 | + path('es-modules', 'test-esm-ok.mjs'), |
| 16 | + ]); |
13 | 17 |
|
14 | | - let stderr = ''; |
15 | | - child.stderr.setEncoding('utf8'); |
16 | | - child.stderr.on('data', (data) => { |
17 | | - stderr += data; |
18 | | - }); |
19 | | - child.on('close', mustCall((code, _signal) => { |
20 | 18 | strictEqual(code, 0); |
21 | 19 | ok(!stderr.includes('must not call')); |
22 | | - })); |
23 | | -} |
| 20 | + }); |
24 | 21 |
|
25 | | -{ |
26 | | - const child = spawn(execPath, [ |
27 | | - '--experimental-loader', |
28 | | - fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href, |
29 | | - path('es-modules', 'test-esm-ok.mjs'), |
30 | | - ]); |
| 22 | + it('should crash the node process rejection with an error', async () => { |
| 23 | + const { code, stderr } = await spawn(execPath, [ |
| 24 | + '--experimental-loader', |
| 25 | + fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href, |
| 26 | + path('es-modules', 'test-esm-ok.mjs'), |
| 27 | + ]); |
31 | 28 |
|
32 | | - let stderr = ''; |
33 | | - child.stderr.setEncoding('utf8'); |
34 | | - child.stderr.on('data', (data) => { |
35 | | - stderr += data; |
36 | | - }); |
37 | | - child.on('close', mustCall((code, _signal) => { |
38 | 29 | notStrictEqual(code, 0); |
39 | | - |
40 | 30 | match(stderr, /\sError: must crash the process\r?\n/); |
41 | | - |
42 | 31 | ok(!stderr.includes('must not call')); |
43 | | - })); |
44 | | -} |
| 32 | + }); |
45 | 33 |
|
46 | | -{ |
47 | | - const child = spawn(execPath, [ |
48 | | - '--experimental-loader', |
49 | | - fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href, |
50 | | - path('es-modules', 'test-esm-ok.mjs'), |
51 | | - ]); |
| 34 | + it('should just reject without an error (but NOT crash the node process)', async () => { |
| 35 | + const { code, stderr } = await spawn(execPath, [ |
| 36 | + '--experimental-loader', |
| 37 | + fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href, |
| 38 | + path('es-modules', 'test-esm-ok.mjs'), |
| 39 | + ]); |
52 | 40 |
|
53 | | - let stderr = ''; |
54 | | - child.stderr.setEncoding('utf8'); |
55 | | - child.stderr.on('data', (data) => { |
56 | | - stderr += data; |
57 | | - }); |
58 | | - child.on('close', mustCall((code, _signal) => { |
59 | 41 | notStrictEqual(code, 0); |
60 | | - |
61 | 42 | match(stderr, /\sundefined\r?\n/); |
62 | | - |
63 | 43 | ok(!stderr.includes('must not call')); |
64 | | - })); |
65 | | -} |
| 44 | + }); |
| 45 | +}); |
0 commit comments