Skip to content

Commit

Permalink
test: refactor some esm tests
Browse files Browse the repository at this point in the history
PR-URL: nodejs#55472
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
  • Loading branch information
aduh95 authored and tpoisseau committed Nov 21, 2024
1 parent 940acef commit 2d09f5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
23 changes: 11 additions & 12 deletions test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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$/);
}

{
Expand All @@ -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$/);
}

{
Expand Down
27 changes: 10 additions & 17 deletions test/es-module/test-esm-pkgname.mjs
Original file line number Diff line number Diff line change
@@ -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',
})));

0 comments on commit 2d09f5e

Please sign in to comment.