Skip to content

Commit 4397a74

Browse files
committed
test: fix some assumptions in tests
Some tests are assuming they will be run from a directory that do not contain any quote or special character in its path. That assumption is not necessary, using `JSON.stringify` or `pathToFileURL` ensures the test can be run whatever the path looks like.
1 parent 8f0f17e commit 4397a74

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

test/es-module/test-esm-dynamic-import.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22
const common = require('../common');
3+
const { pathToFileURL } = require('url');
34
const assert = require('assert');
45

56
const relativePath = '../fixtures/es-modules/test-esm-ok.mjs';
6-
const absolutePath = require.resolve('../fixtures/es-modules/test-esm-ok.mjs');
7-
const targetURL = new URL('file:///');
8-
targetURL.pathname = absolutePath;
7+
const absolutePath = require.resolve(relativePath);
8+
const targetURL = pathToFileURL(absolutePath);
99

1010
function expectModuleError(result, code, message) {
1111
Promise.resolve(result).catch(common.mustCall((error) => {
@@ -41,7 +41,7 @@ function expectFsNamespace(result) {
4141
// expectOkNamespace(import(relativePath));
4242
expectOkNamespace(eval(`import("${relativePath}")`));
4343
expectOkNamespace(eval(`import("${relativePath}")`));
44-
expectOkNamespace(eval(`import("${targetURL}")`));
44+
expectOkNamespace(eval(`import(${JSON.stringify(targetURL)})`));
4545

4646
// Importing a built-in, both direct & via eval
4747
expectFsNamespace(import('fs'));

test/es-module/test-esm-loader-spawn-promisified.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Loader hooks throwing errors', { concurrency: true }, () => {
2828
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
2929
'--input-type=module',
3030
'--eval',
31-
`import '${fixtures.fileURL('/es-modules/file.unknown')}'`,
31+
`import ${JSON.stringify(fixtures.fileURL('/es-modules/file.unknown'))}`,
3232
]);
3333

3434
assert.match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/);
@@ -142,7 +142,7 @@ describe('Loader hooks throwing errors', { concurrency: true }, () => {
142142
`import assert from 'node:assert';
143143
await Promise.allSettled([
144144
import('nonexistent/file.mjs'),
145-
import('${fixtures.fileURL('/es-modules/file.unknown')}'),
145+
import(${JSON.stringify(fixtures.fileURL('/es-modules/file.unknown'))}),
146146
import('esmHook/badReturnVal.mjs'),
147147
import('esmHook/format.false'),
148148
import('esmHook/format.true'),
@@ -170,7 +170,7 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
170170
'--input-type=module',
171171
'--eval',
172172
`import assert from 'node:assert';
173-
await import('${fixtures.fileURL('/es-module-loaders/js-as-esm.js')}')
173+
await import(${JSON.stringify(fixtures.fileURL('/es-module-loaders/js-as-esm.js'))})
174174
.then((parsedModule) => {
175175
assert.strictEqual(typeof parsedModule, 'object');
176176
assert.strictEqual(parsedModule.namedExport, 'named-export');
@@ -191,7 +191,7 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
191191
'--input-type=module',
192192
'--eval',
193193
`import assert from 'node:assert';
194-
await import('${fixtures.fileURL('/es-modules/file.ext')}')
194+
await import(${JSON.stringify(fixtures.fileURL('/es-modules/file.ext'))})
195195
.then((parsedModule) => {
196196
assert.strictEqual(typeof parsedModule, 'object');
197197
const { default: defaultExport } = parsedModule;
@@ -258,7 +258,7 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
258258
'--input-type=module',
259259
'--eval',
260260
`import assert from 'node:assert';
261-
await import('${fixtures.fileURL('/es-modules/stateful.mjs')}')
261+
await import(${JSON.stringify(fixtures.fileURL('/es-modules/stateful.mjs'))})
262262
.then(({ default: count }) => {
263263
assert.strictEqual(count(), 1);
264264
});`,

0 commit comments

Comments
 (0)