|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | require('../common'); |
4 | | -const { spawnSyncAndExitWithoutError } = require('../common/child_process'); |
5 | | -const fixtures = require('../common/fixtures'); |
6 | | - |
| 4 | +const { spawnSyncAndAssert } = require('../common/child_process'); |
| 5 | +const { fixturesDir } = require('../common/fixtures'); |
7 | 6 | const stderr = /ExperimentalWarning: Support for loading ES Module in require/; |
8 | 7 |
|
9 | | -// Test named exports. |
10 | | -{ |
11 | | - spawnSyncAndExitWithoutError( |
12 | | - process.execPath, |
13 | | - [ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-module-loaders/module-named-exports.mjs') ], |
14 | | - { |
15 | | - stderr, |
16 | | - } |
17 | | - ); |
18 | | -} |
| 8 | +function testPreload(preloadFlag) { |
| 9 | + // Test named exports. |
| 10 | + { |
| 11 | + spawnSyncAndAssert( |
| 12 | + process.execPath, |
| 13 | + [ |
| 14 | + '--experimental-require-module', |
| 15 | + preloadFlag, |
| 16 | + './es-module-loaders/module-named-exports.mjs', |
| 17 | + './printA.js', |
| 18 | + ], |
| 19 | + { |
| 20 | + cwd: fixturesDir |
| 21 | + }, |
| 22 | + { |
| 23 | + stdout: 'A', |
| 24 | + stderr, |
| 25 | + trim: true, |
| 26 | + } |
| 27 | + ); |
| 28 | + } |
19 | 29 |
|
20 | | -// Test ESM that import ESM. |
21 | | -{ |
22 | | - spawnSyncAndExitWithoutError( |
23 | | - process.execPath, |
24 | | - [ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/import-esm.mjs') ], |
25 | | - { |
26 | | - stderr, |
27 | | - stdout: 'world', |
28 | | - trim: true, |
29 | | - } |
30 | | - ); |
31 | | -} |
| 30 | + // Test ESM that import ESM. |
| 31 | + { |
| 32 | + spawnSyncAndAssert( |
| 33 | + process.execPath, |
| 34 | + [ |
| 35 | + '--experimental-require-module', |
| 36 | + preloadFlag, |
| 37 | + './es-modules/import-esm.mjs', |
| 38 | + './printA.js', |
| 39 | + ], |
| 40 | + { |
| 41 | + cwd: fixturesDir |
| 42 | + }, |
| 43 | + { |
| 44 | + stderr, |
| 45 | + stdout: /^world\s+A$/, |
| 46 | + trim: true, |
| 47 | + } |
| 48 | + ); |
| 49 | + } |
32 | 50 |
|
33 | | -// Test ESM that import CJS. |
34 | | -{ |
35 | | - spawnSyncAndExitWithoutError( |
36 | | - process.execPath, |
37 | | - [ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/cjs-exports.mjs') ], |
38 | | - { |
39 | | - stdout: 'ok', |
40 | | - stderr, |
41 | | - trim: true, |
42 | | - } |
43 | | - ); |
44 | | -} |
| 51 | + // Test ESM that import CJS. |
| 52 | + { |
| 53 | + spawnSyncAndAssert( |
| 54 | + process.execPath, |
| 55 | + [ |
| 56 | + '--experimental-require-module', |
| 57 | + preloadFlag, |
| 58 | + './es-modules/cjs-exports.mjs', |
| 59 | + './printA.js', |
| 60 | + ], |
| 61 | + { |
| 62 | + cwd: fixturesDir |
| 63 | + }, |
| 64 | + { |
| 65 | + stdout: /^ok\s+A$/, |
| 66 | + stderr, |
| 67 | + trim: true, |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
45 | 71 |
|
46 | | -// Test ESM that require() CJS. |
47 | | -// Can't use the common/index.mjs here because that checks the globals, and |
48 | | -// -r injects a bunch of globals. |
49 | | -{ |
50 | | - spawnSyncAndExitWithoutError( |
51 | | - process.execPath, |
52 | | - [ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/require-cjs.mjs') ], |
53 | | - { |
54 | | - stdout: 'world', |
55 | | - stderr, |
56 | | - trim: true, |
57 | | - } |
58 | | - ); |
| 72 | + // Test ESM that require() CJS. |
| 73 | + // Can't use the common/index.mjs here because that checks the globals, and |
| 74 | + // -r injects a bunch of globals. |
| 75 | + { |
| 76 | + spawnSyncAndAssert( |
| 77 | + process.execPath, |
| 78 | + [ |
| 79 | + '--experimental-require-module', |
| 80 | + preloadFlag, |
| 81 | + './es-modules/require-cjs.mjs', |
| 82 | + './printA.js', |
| 83 | + ], |
| 84 | + { |
| 85 | + cwd: fixturesDir |
| 86 | + }, |
| 87 | + { |
| 88 | + stdout: /^world\s+A$/, |
| 89 | + stderr, |
| 90 | + trim: true, |
| 91 | + } |
| 92 | + ); |
| 93 | + } |
59 | 94 | } |
60 | 95 |
|
61 | | -// Test "type": "module" and "main" field in package.json. |
| 96 | +testPreload('--require'); |
| 97 | +testPreload('--import'); |
| 98 | + |
| 99 | +// Test "type": "module" and "main" field in package.json, this is only for --require because |
| 100 | +// --import does not support extension-less preloads. |
62 | 101 | { |
63 | | - spawnSyncAndExitWithoutError( |
| 102 | + spawnSyncAndAssert( |
64 | 103 | process.execPath, |
65 | | - [ '--experimental-require-module', '-r', fixtures.path('../fixtures/es-modules/package-type-module') ], |
| 104 | + [ |
| 105 | + '--experimental-require-module', |
| 106 | + '--require', |
| 107 | + './es-modules/package-type-module', |
| 108 | + './printA.js', |
| 109 | + ], |
| 110 | + { |
| 111 | + cwd: fixturesDir |
| 112 | + }, |
66 | 113 | { |
67 | | - stdout: 'package-type-module', |
| 114 | + stdout: /^package-type-module\s+A$/, |
68 | 115 | stderr, |
69 | 116 | trim: true, |
70 | 117 | } |
|
0 commit comments