@@ -7,45 +7,60 @@ const fs = require('fs'); |
7 | 7 | const path = require('path'); |
8 | 8 | const { createRequire } = require('module'); |
9 | 9 |
|
10 | | -assert.throws( |
11 | | - () => require('test'), |
12 | | - common.expectsError({ code: 'MODULE_NOT_FOUND' }), |
13 | | -); |
14 | | - |
15 | | -(async () => { |
16 | | - await assert.rejects( |
17 | | - async () => import('test'), |
18 | | - common.expectsError({ code: 'ERR_MODULE_NOT_FOUND' }), |
| 10 | +for (const name in ['test', 'test/reporters']) { |
| 11 | + assert.throws( |
| 12 | + () => require(name), |
| 13 | + common.expectsError({ code: 'MODULE_NOT_FOUND' }), |
19 | 14 | ); |
20 | | -})().then(common.mustCall()); |
21 | 15 |
|
22 | | -assert.throws( |
23 | | - () => require.resolve('test'), |
24 | | - common.expectsError({ code: 'MODULE_NOT_FOUND' }), |
25 | | -); |
| 16 | + (async () => { |
| 17 | + await assert.rejects( |
| 18 | + async () => import(name), |
| 19 | + common.expectsError({ code: 'ERR_MODULE_NOT_FOUND' }), |
| 20 | + ); |
| 21 | + })().then(common.mustCall()); |
| 22 | + |
| 23 | + assert.throws( |
| 24 | + () => require.resolve(name), |
| 25 | + common.expectsError({ code: 'MODULE_NOT_FOUND' }), |
| 26 | + ); |
| 27 | +} |
26 | 28 |
|
27 | 29 | // Verify that files in node_modules can be resolved. |
28 | 30 | tmpdir.refresh(); |
29 | 31 |
|
30 | 32 | const packageRoot = path.join(tmpdir.path, 'node_modules', 'test'); |
| 33 | +const reportersDir = path.join(tmpdir.path, 'node_modules', 'test', 'reporters'); |
31 | 34 | const indexFile = path.join(packageRoot, 'index.js'); |
| 35 | +const reportersIndexFile = path.join(reportersDir, 'index.js'); |
32 | 36 |
|
33 | | -fs.mkdirSync(packageRoot, { recursive: true }); |
| 37 | +fs.mkdirSync(reportersDir, { recursive: true }); |
34 | 38 | fs.writeFileSync(indexFile, 'module.exports = { marker: 1 };'); |
| 39 | +fs.writeFileSync(reportersIndexFile, 'module.exports = { marker: 1 };'); |
35 | 40 |
|
36 | | -function test(argv) { |
| 41 | +function test(argv, expectedToFail = false) { |
37 | 42 | const child = spawnSync(process.execPath, argv, { cwd: tmpdir.path }); |
38 | | - assert.strictEqual(child.status, 0); |
39 | | - assert.strictEqual(child.stdout.toString().trim(), '{ marker: 1 }'); |
| 43 | + if (expectedToFail) { |
| 44 | + assert.strictEqual(child.status, 1); |
| 45 | + assert.strictEqual(child.stdout.toString().trim(), ''); |
| 46 | + } else { |
| 47 | + assert.strictEqual(child.status, 0); |
| 48 | + assert.strictEqual(child.stdout.toString().trim(), '{ marker: 1 }'); |
| 49 | + } |
40 | 50 | } |
41 | 51 |
|
42 | 52 | test(['-e', 'console.log(require("test"))']); |
| 53 | +test(['-e', 'console.log(require("test/reporters"))']); |
43 | 54 | test(['-e', 'import("test").then(m=>console.log(m.default))']); |
| 55 | +test(['-e', 'import("test/reporters").then(m=>console.log(m.default))'], true); |
44 | 56 | test(['--input-type=module', '-e', 'import test from "test";console.log(test)']); |
| 57 | +test(['--input-type=module', '-e', 'import test from "test/reporters";console.log(test)'], true); |
45 | 58 | test(['--input-type=module', '-e', 'console.log((await import("test")).default)']); |
| 59 | +test(['--input-type=module', '-e', 'console.log((await import("test/reporters")).default)'], true); |
46 | 60 |
|
47 | 61 | { |
48 | 62 | const dummyFile = path.join(tmpdir.path, 'file.js'); |
49 | 63 | const require = createRequire(dummyFile); |
50 | 64 | assert.strictEqual(require.resolve('test'), indexFile); |
| 65 | + assert.strictEqual(require.resolve('test/reporters'), reportersIndexFile); |
51 | 66 | } |