Skip to content

Commit fc69080

Browse files
aduh95ruyadorno
authored andcommitted
test: refactor some esm tests
PR-URL: #55472 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 0a79ebd commit fc69080

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

test/es-module/test-esm-import-meta-resolve.mjs

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
// Flags: --experimental-import-meta-resolve
22
import { spawnPromisified } from '../common/index.mjs';
3+
import { fileURL as fixturesFileURL } from '../common/fixtures.mjs';
34
import assert from 'assert';
45
import { spawn } from 'child_process';
56
import { execPath } from 'process';
67

7-
const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
8-
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/';
8+
const fixtures = `${fixturesFileURL()}/`;
99

1010
assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'),
11-
dirname + 'test-esm-import-meta.mjs');
11+
new URL('./test-esm-import-meta.mjs', import.meta.url).href);
1212
assert.strictEqual(import.meta.resolve('./notfound.mjs'), new URL('./notfound.mjs', import.meta.url).href);
1313
assert.strictEqual(import.meta.resolve('./asset'), new URL('./asset', import.meta.url).href);
14-
try {
14+
assert.throws(() => {
1515
import.meta.resolve('does-not-exist');
16-
assert.fail();
17-
} catch (e) {
18-
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
19-
}
16+
}, {
17+
code: 'ERR_MODULE_NOT_FOUND',
18+
});
2019
assert.strictEqual(
2120
import.meta.resolve('../fixtures/empty-with-bom.txt'),
2221
fixtures + 'empty-with-bom.txt');
@@ -60,11 +59,11 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
6059
});
6160

6261
{
63-
const cp = spawn(execPath, [
62+
const { stdout } = await spawnPromisified(execPath, [
6463
'--input-type=module',
6564
'--eval', 'console.log(typeof import.meta.resolve)',
6665
]);
67-
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
66+
assert.match(stdout, /^function\r?\n$/);
6867
}
6968

7069
{
@@ -76,11 +75,11 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
7675
}
7776

7877
{
79-
const cp = spawn(execPath, [
78+
const { stdout } = await spawnPromisified(execPath, [
8079
'--input-type=module',
8180
'--eval', 'import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"',
8281
]);
83-
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
82+
assert.match(stdout, /^node:os\r?\n$/);
8483
}
8584

8685
{

test/es-module/test-esm-pkgname.mjs

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import { mustCall } from '../common/index.mjs';
2-
import { strictEqual } from 'assert';
1+
import '../common/index.mjs';
2+
import assert from 'node:assert';
33

44
import { importFixture } from '../fixtures/pkgexports.mjs';
55

6-
importFixture('as%2Ff').catch(mustCall((err) => {
7-
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
8-
}));
9-
10-
importFixture('as%5Cf').catch(mustCall((err) => {
11-
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
12-
}));
13-
14-
importFixture('as\\df').catch(mustCall((err) => {
15-
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
16-
}));
17-
18-
importFixture('@as@df').catch(mustCall((err) => {
19-
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
20-
}));
6+
await Promise.all([
7+
'as%2Ff',
8+
'as%5Cf',
9+
'as\\df',
10+
'@as@df',
11+
].map((specifier) => assert.rejects(importFixture(specifier), {
12+
code: 'ERR_INVALID_MODULE_SPECIFIER',
13+
})));

0 commit comments

Comments
 (0)