Skip to content

Commit

Permalink
test: fix some assumptions in tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aduh95 committed Jul 29, 2023
1 parent 8f0f17e commit 4397a74
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions test/es-module/test-esm-dynamic-import.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
const common = require('../common');
const { pathToFileURL } = require('url');
const assert = require('assert');

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

function expectModuleError(result, code, message) {
Promise.resolve(result).catch(common.mustCall((error) => {
Expand Down Expand Up @@ -41,7 +41,7 @@ function expectFsNamespace(result) {
// expectOkNamespace(import(relativePath));
expectOkNamespace(eval(`import("${relativePath}")`));
expectOkNamespace(eval(`import("${relativePath}")`));
expectOkNamespace(eval(`import("${targetURL}")`));
expectOkNamespace(eval(`import(${JSON.stringify(targetURL)})`));

// Importing a built-in, both direct & via eval
expectFsNamespace(import('fs'));
Expand Down
10 changes: 5 additions & 5 deletions test/es-module/test-esm-loader-spawn-promisified.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Loader hooks throwing errors', { concurrency: true }, () => {
fixtures.fileURL('/es-module-loaders/hooks-custom.mjs'),
'--input-type=module',
'--eval',
`import '${fixtures.fileURL('/es-modules/file.unknown')}'`,
`import ${JSON.stringify(fixtures.fileURL('/es-modules/file.unknown'))}`,
]);

assert.match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/);
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Loader hooks throwing errors', { concurrency: true }, () => {
`import assert from 'node:assert';
await Promise.allSettled([
import('nonexistent/file.mjs'),
import('${fixtures.fileURL('/es-modules/file.unknown')}'),
import(${JSON.stringify(fixtures.fileURL('/es-modules/file.unknown'))}),
import('esmHook/badReturnVal.mjs'),
import('esmHook/format.false'),
import('esmHook/format.true'),
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('${fixtures.fileURL('/es-module-loaders/js-as-esm.js')}')
await import(${JSON.stringify(fixtures.fileURL('/es-module-loaders/js-as-esm.js'))})
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
assert.strictEqual(parsedModule.namedExport, 'named-export');
Expand All @@ -191,7 +191,7 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('${fixtures.fileURL('/es-modules/file.ext')}')
await import(${JSON.stringify(fixtures.fileURL('/es-modules/file.ext'))})
.then((parsedModule) => {
assert.strictEqual(typeof parsedModule, 'object');
const { default: defaultExport } = parsedModule;
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('Loader hooks parsing modules', { concurrency: true }, () => {
'--input-type=module',
'--eval',
`import assert from 'node:assert';
await import('${fixtures.fileURL('/es-modules/stateful.mjs')}')
await import(${JSON.stringify(fixtures.fileURL('/es-modules/stateful.mjs'))})
.then(({ default: count }) => {
assert.strictEqual(count(), 1);
});`,
Expand Down

0 comments on commit 4397a74

Please sign in to comment.