Skip to content

Commit e502c50

Browse files
aduh95danielleadams
authored andcommitted
test: refactor test/es-module/test-esm-resolve-type
Using TLA to avoid undetected never-settling promises, and avoid running parallel calls to `process.chdir`. PR-URL: #43178 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent aa7d4e5 commit e502c50

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

test/es-module/test-esm-resolve-type.js renamed to test/es-module/test-esm-resolve-type.mjs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict';
21
// Flags: --expose-internals
32

43
/**
@@ -7,23 +6,25 @@
76
* { url: <url_value>, format: <'module'|'commonjs'|undefined> };
87
*/
98

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';
1616

1717
if (!common.isMainThread) {
1818
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'
2020
);
2121
}
2222

23-
const assert = require('assert');
23+
import assert from 'assert';
24+
import internalResolve from 'node:internal/modules/esm/resolve';
2425
const {
2526
defaultResolve: resolve
26-
} = require('internal/modules/esm/resolve');
27+
} = internalResolve;
2728

2829
const rel = (file) => path.join(tmpdir.path, file);
2930
const previousCwd = process.cwd();
@@ -36,34 +37,29 @@ try {
3637
* ensure that resolving by full path does not return the format
3738
* with the defaultResolver
3839
*/
39-
[
40+
await Promise.all([
4041
[ '/es-modules/package-type-module/index.js', 'module' ],
4142
[ '/es-modules/package-type-commonjs/index.js', 'commonjs' ],
4243
[ '/es-modules/package-without-type/index.js', 'commonjs' ],
4344
[ '/es-modules/package-without-pjson/index.js', 'commonjs' ],
44-
].forEach(async (testVariant) => {
45-
const [ testScript, expectedType ] = testVariant;
45+
].map(async ([ testScript, expectedType ]) => {
4646
const resolvedPath = path.resolve(fixtures.path(testScript));
4747
const resolveResult = await resolve(url.pathToFileURL(resolvedPath));
4848
assert.strictEqual(resolveResult.format, expectedType);
49-
});
49+
}));
5050

5151
/**
5252
* create a test module and try to resolve it by module name.
5353
* check the result is as expected
5454
*
5555
* for test-module-ne: everything .js that is not 'module' is 'commonjs'
5656
*/
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+
]) {
6763
process.chdir(previousCwd);
6864
tmpdir.refresh();
6965
process.chdir(tmpdir.path);
@@ -93,7 +89,7 @@ try {
9389
assert.strictEqual(resolveResult.format, expectedResolvedType);
9490

9591
fs.rmSync(nmDir, { recursive: true, force: true });
96-
});
92+
}
9793

9894
// Helpers
9995
const createDir = (path) => {
@@ -102,7 +98,7 @@ try {
10298
}
10399
};
104100

105-
async function testDualPackageWithJsMainScriptAndModuleType() {
101+
{
106102
// Create a dummy dual package
107103
//
108104
/**
@@ -177,11 +173,9 @@ try {
177173
assert.ok(resolveResult.url.includes('my-dual-package/es/index.js'));
178174
}
179175

180-
testDualPackageWithJsMainScriptAndModuleType();
181-
182176
// TestParameters are ModuleName, mainRequireScript, mainImportScript,
183177
// mainPackageType, subdirPkgJsonType, expectedResolvedFormat, mainSuffix
184-
[
178+
await Promise.all([
185179
[ 'mjs-mod-mod', 'index.js', 'index.mjs', 'module', 'module', 'module'],
186180
[ 'mjs-com-com', 'idx.js', 'idx.mjs', 'commonjs', 'commonjs', 'module'],
187181
[ 'mjs-mod-com', 'index.js', 'imp.mjs', 'module', 'commonjs', 'module'],
@@ -192,7 +186,7 @@ try {
192186
[ 'hmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '#Key'],
193187
[ 'qhmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '?k=v#h'],
194188
[ 'ts-mod-com', 'index.js', 'imp.ts', 'module', 'commonjs', undefined],
195-
].forEach(async (testVariant) => {
189+
].map(async (testVariant) => {
196190
const [
197191
moduleName,
198192
mainRequireScript,
@@ -243,7 +237,7 @@ try {
243237
const resolveResult = await resolve(`${moduleName}`);
244238
assert.strictEqual(resolveResult.format, expectedResolvedFormat);
245239
assert.ok(resolveResult.url.endsWith(`${moduleName}/subdir/${mainImportScript}${mainSuffix}`));
246-
});
240+
}));
247241

248242
} finally {
249243
process.chdir(previousCwd);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy