|
1 | | -'use strict'; |
2 | 1 | // Flags: --expose-internals |
3 | 2 |
|
4 | 3 | /** |
|
7 | 6 | * { url: <url_value>, format: <'module'|'commonjs'|undefined> }; |
8 | 7 | */ |
9 | 8 |
|
10 | | -const common = require('../common'); |
11 | | -const tmpdir = require('../common/tmpdir'); |
12 | | -const fixtures = require('../common/fixtures'); |
13 | | -const path = require('path'); |
14 | | -const fs = require('fs'); |
15 | | -const url = require('url'); |
| 9 | +import * as common from '../common/index.mjs'; |
| 10 | +import tmpdir from '../common/tmpdir.js'; |
| 11 | +import * as fixtures from '../common/fixtures.mjs'; |
| 12 | +import path from 'path'; |
| 13 | +import fs from 'fs'; |
| 14 | +import url from 'url'; |
| 15 | +import process from 'process'; |
16 | 16 |
|
17 | 17 | if (!common.isMainThread) { |
18 | 18 | common.skip( |
19 | | - 'test-esm-resolve-type.js: process.chdir is not available in Workers' |
| 19 | + 'test-esm-resolve-type.mjs: process.chdir is not available in Workers' |
20 | 20 | ); |
21 | 21 | } |
22 | 22 |
|
23 | | -const assert = require('assert'); |
| 23 | +import assert from 'assert'; |
| 24 | +import internalResolve from 'node:internal/modules/esm/resolve'; |
24 | 25 | const { |
25 | 26 | defaultResolve: resolve |
26 | | -} = require('internal/modules/esm/resolve'); |
| 27 | +} = internalResolve; |
27 | 28 |
|
28 | 29 | const rel = (file) => path.join(tmpdir.path, file); |
29 | 30 | const previousCwd = process.cwd(); |
@@ -36,34 +37,29 @@ try { |
36 | 37 | * ensure that resolving by full path does not return the format |
37 | 38 | * with the defaultResolver |
38 | 39 | */ |
39 | | - [ |
| 40 | + await Promise.all([ |
40 | 41 | [ '/es-modules/package-type-module/index.js', 'module' ], |
41 | 42 | [ '/es-modules/package-type-commonjs/index.js', 'commonjs' ], |
42 | 43 | [ '/es-modules/package-without-type/index.js', 'commonjs' ], |
43 | 44 | [ '/es-modules/package-without-pjson/index.js', 'commonjs' ], |
44 | | - ].forEach(async (testVariant) => { |
45 | | - const [ testScript, expectedType ] = testVariant; |
| 45 | + ].map(async ([ testScript, expectedType ]) => { |
46 | 46 | const resolvedPath = path.resolve(fixtures.path(testScript)); |
47 | 47 | const resolveResult = await resolve(url.pathToFileURL(resolvedPath)); |
48 | 48 | assert.strictEqual(resolveResult.format, expectedType); |
49 | | - }); |
| 49 | + })); |
50 | 50 |
|
51 | 51 | /** |
52 | 52 | * create a test module and try to resolve it by module name. |
53 | 53 | * check the result is as expected |
54 | 54 | * |
55 | 55 | * for test-module-ne: everything .js that is not 'module' is 'commonjs' |
56 | 56 | */ |
57 | | - |
58 | | - [ [ 'test-module-mainjs', 'js', 'module', 'module'], |
59 | | - [ 'test-module-mainmjs', 'mjs', 'module', 'module'], |
60 | | - [ 'test-module-cjs', 'js', 'commonjs', 'commonjs'], |
61 | | - [ 'test-module-ne', 'js', undefined, 'commonjs'], |
62 | | - ].forEach(async (testVariant) => { |
63 | | - const [ moduleName, |
64 | | - moduleExtenstion, |
65 | | - moduleType, |
66 | | - expectedResolvedType ] = testVariant; |
| 57 | + for (const [ moduleName, moduleExtenstion, moduleType, expectedResolvedType ] of |
| 58 | + [ [ 'test-module-mainjs', 'js', 'module', 'module'], |
| 59 | + [ 'test-module-mainmjs', 'mjs', 'module', 'module'], |
| 60 | + [ 'test-module-cjs', 'js', 'commonjs', 'commonjs'], |
| 61 | + [ 'test-module-ne', 'js', undefined, 'commonjs'], |
| 62 | + ]) { |
67 | 63 | process.chdir(previousCwd); |
68 | 64 | tmpdir.refresh(); |
69 | 65 | process.chdir(tmpdir.path); |
|
93 | 89 | assert.strictEqual(resolveResult.format, expectedResolvedType); |
94 | 90 |
|
95 | 91 | fs.rmSync(nmDir, { recursive: true, force: true }); |
96 | | - }); |
| 92 | + } |
97 | 93 |
|
98 | 94 | // Helpers |
99 | 95 | const createDir = (path) => { |
|
102 | 98 | } |
103 | 99 | }; |
104 | 100 |
|
105 | | - async function testDualPackageWithJsMainScriptAndModuleType() { |
| 101 | + { |
106 | 102 | // Create a dummy dual package |
107 | 103 | // |
108 | 104 | /** |
@@ -177,11 +173,9 @@ try { |
177 | 173 | assert.ok(resolveResult.url.includes('my-dual-package/es/index.js')); |
178 | 174 | } |
179 | 175 |
|
180 | | - testDualPackageWithJsMainScriptAndModuleType(); |
181 | | - |
182 | 176 | // TestParameters are ModuleName, mainRequireScript, mainImportScript, |
183 | 177 | // mainPackageType, subdirPkgJsonType, expectedResolvedFormat, mainSuffix |
184 | | - [ |
| 178 | + await Promise.all([ |
185 | 179 | [ 'mjs-mod-mod', 'index.js', 'index.mjs', 'module', 'module', 'module'], |
186 | 180 | [ 'mjs-com-com', 'idx.js', 'idx.mjs', 'commonjs', 'commonjs', 'module'], |
187 | 181 | [ 'mjs-mod-com', 'index.js', 'imp.mjs', 'module', 'commonjs', 'module'], |
@@ -192,7 +186,7 @@ try { |
192 | 186 | [ 'hmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '#Key'], |
193 | 187 | [ 'qhmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '?k=v#h'], |
194 | 188 | [ 'ts-mod-com', 'index.js', 'imp.ts', 'module', 'commonjs', undefined], |
195 | | - ].forEach(async (testVariant) => { |
| 189 | + ].map(async (testVariant) => { |
196 | 190 | const [ |
197 | 191 | moduleName, |
198 | 192 | mainRequireScript, |
@@ -243,7 +237,7 @@ try { |
243 | 237 | const resolveResult = await resolve(`${moduleName}`); |
244 | 238 | assert.strictEqual(resolveResult.format, expectedResolvedFormat); |
245 | 239 | assert.ok(resolveResult.url.endsWith(`${moduleName}/subdir/${mainImportScript}${mainSuffix}`)); |
246 | | - }); |
| 240 | + })); |
247 | 241 |
|
248 | 242 | } finally { |
249 | 243 | process.chdir(previousCwd); |
|