test: refactor some esm tests

PR-URL: https://github.com/nodejs/node/pull/55472
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
Antoine du Hamel 2024-11-05 23:40:39 +00:00 committed by GitHub
parent 9c40cd7a9d
commit ecc62381a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 29 deletions

View file

@ -1,22 +1,21 @@
// Flags: --experimental-import-meta-resolve
import { spawnPromisified } from '../common/index.mjs';
import { fileURL as fixturesFileURL } from '../common/fixtures.mjs';
import assert from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
const dirname = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + 1) + 'fixtures/';
const fixtures = `${fixturesFileURL()}/`;
assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'),
dirname + 'test-esm-import-meta.mjs');
new URL('./test-esm-import-meta.mjs', import.meta.url).href);
assert.strictEqual(import.meta.resolve('./notfound.mjs'), new URL('./notfound.mjs', import.meta.url).href);
assert.strictEqual(import.meta.resolve('./asset'), new URL('./asset', import.meta.url).href);
try {
assert.throws(() => {
import.meta.resolve('does-not-exist');
assert.fail();
} catch (e) {
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
}
}, {
code: 'ERR_MODULE_NOT_FOUND',
});
assert.strictEqual(
import.meta.resolve('../fixtures/empty-with-bom.txt'),
fixtures + 'empty-with-bom.txt');
@ -60,11 +59,11 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
});
{
const cp = spawn(execPath, [
const { stdout } = await spawnPromisified(execPath, [
'--input-type=module',
'--eval', 'console.log(typeof import.meta.resolve)',
]);
assert.match((await cp.stdout.toArray()).toString(), /^function\r?\n$/);
assert.match(stdout, /^function\r?\n$/);
}
{
@ -76,11 +75,11 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
}
{
const cp = spawn(execPath, [
const { stdout } = await spawnPromisified(execPath, [
'--input-type=module',
'--eval', 'import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"',
]);
assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/);
assert.match(stdout, /^node:os\r?\n$/);
}
{

View file

@ -1,20 +1,13 @@
import { mustCall } from '../common/index.mjs';
import { strictEqual } from 'assert';
import '../common/index.mjs';
import assert from 'node:assert';
import { importFixture } from '../fixtures/pkgexports.mjs';
importFixture('as%2Ff').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
importFixture('as%5Cf').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
importFixture('as\\df').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
importFixture('@as@df').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
await Promise.all([
'as%2Ff',
'as%5Cf',
'as\\df',
'@as@df',
].map((specifier) => assert.rejects(importFixture(specifier), {
code: 'ERR_INVALID_MODULE_SPECIFIER',
})));