Skip to content

Commit

Permalink
CLI: Fix ESM file imports on Windows to use file-protocol URLs
Browse files Browse the repository at this point in the history
Fixes #1667.
  • Loading branch information
Krinkle committed Feb 15, 2022
1 parent 4ade621 commit f3e4b24
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/cli/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const path = require( "path" );
const url = require( "url" );

const requireFromCWD = require( "./require-from-cwd" );
const requireQUnit = require( "./require-qunit" );
Expand Down Expand Up @@ -83,7 +84,11 @@ async function run( args, options ) {
( e instanceof SyntaxError &&
e.message === "Cannot use import statement outside a module" ) ) &&
( !nodeVint || nodeVint >= 72 ) ) {
await import( filePath ); // eslint-disable-line node/no-unsupported-features/es-syntax

// filePath is an absolute file path here (per path.resolve above).
// On Windows, Node.js enforces that absolute paths via ESM use valid URLs,
// e.g. file-protocol) https://github.com/qunitjs/qunit/issues/1667
await import( url.pathToFileURL( filePath ) ); // eslint-disable-line node/no-unsupported-features/es-syntax
} else {
throw e;
}
Expand Down
4 changes: 1 addition & 3 deletions test/cli/cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,7 @@ HOOK: BCD1 @ B after`;
} );

if ( semver.gte( process.versions.node, "12.0.0" ) ) {

// TODO: Import paths broken on Windows. https://github.com/qunitjs/qunit/issues/1667
QUnit[ skipOnWinTest ]( "run ESM test suite with import statement", async assert => {
QUnit.test( "run ESM test suite with import statement", async assert => {
const command = [ "qunit", "../../es2018/esm.mjs" ];
const execution = await execute( command );

Expand Down

0 comments on commit f3e4b24

Please sign in to comment.