Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_UNSUPPORTED_ESM_URL_SCHEME error while running qunit test in cli (nodejs) mode #1667

Closed
abhinath84 opened this issue Dec 20, 2021 · 2 comments · Fixed by #1675
Closed
Assignees
Labels
Component: CLI Type: Bug Something isn't working right.

Comments

@abhinath84
Copy link

abhinath84 commented Dec 20, 2021

Tell us about your runtime:

  • OS: Windows 10
  • QUnit version: 2.17.2
  • Which environment are you using? (e.g., browser, Node): Node v14.16.1
  • How are you running QUnit? (e.g., QUnit CLI, Grunt, Karma, manually in browser): QUnit CLI

What are you trying to do?

Want to explore QUnit CLI and created a sample project to capture basic test cases.
To import module I used ES6 module import mechanism.
To support this, I modified package.json by adding "type": "module":

{
  "name": "es6-module",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "qunit": "^2.17.2"
  }
}

In test.js file import a module which having all test cases:

import { testRun } from './test-cases.js';

testRun();

Here is the content of test-cases.js:

import { Math } from './math.js';

function testRun() {
  QUnit.module('setup', (hooks) => {
    
    hooks.before((assert) => {
      console.log(`Hook before`);
    });
    
    QUnit.module('add');
    QUnit.test('two numbers', assert => {
      assert.equal(Math.add(1, 2), 3);
    });

    QUnit.test('two negative numbers', assert => {
      assert.equal(Math.add(-11, -2), -13);
    });


    QUnit.module('sub');
    QUnit.test('two numbers', assert => {
      assert.equal(Math.sub(5, 1), 4);
    });
  });
}

export { testRun };

Here is the content of math.js:

const Math = {
  add: function(a, b) {
    hello();
    return a + b;
  },
  sub: function(a, b) {
    return a - b;
  }
};

export { Math };

I executed following command to run tests & it's gives 'ERR_UNSUPPORTED_ESM_URL_SCHEME error' as a result:
npx qunit ./test.js

What did you expect to happen?

I expected to execute all test cases & output the result.

What actually happened?

I'm getting following error message:

not ok 1 global failure
  ---
  message: |+
    Error: Failed to load file ./test.js
    Error: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'
  severity: failed
  stack: |
    Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'
        at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:782:11)
        at Loader.resolve (internal/modules/esm/loader.js:86:40)
        at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
        at Loader.import (internal/modules/esm/loader.js:165:28)
        at importModuleDynamically (internal/modules/cjs/loader.js:1006:27)
        at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:30:14)
        at run (D:\Pool\nodejs\es6-module issue\node_modules\qunit\src\cli\run.js:86:6)
        at Object.<anonymous> (D:\Pool\nodejs\es6-module issue\node_modules\qunit\bin\qunit.js:55:2)
        at Module._compile (internal/modules/cjs/loader.js:1063:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
  ...
Bail out! Error: Failed to load file ./test.js

Please help me to resolve the issue.
I need to import module in ES6 way and not in CommonJS way.

NOTE

If I modify line no: 87 in the file node_modules\qunit\src\cli\run.js with following code, problem is getting resolved:

if ( path.isAbsolute(filePath) ) {
  await import( pathToFileURL(filePath) );
} else {
  await import( filePath ); // eslint-disable-line node/no-unsupported-features/es-syntax
}
@Mugen87
Copy link

Mugen87 commented Feb 8, 2022

The three.js project migrated its unit tests to ES6 modules lately. They still work on macOS but fail with the above error message on Windows now. Tested with node.js > 16.

@Krinkle Krinkle self-assigned this Feb 12, 2022
@Krinkle
Copy link
Member

Krinkle commented Feb 14, 2022

@Mugen87 Thanks, I'll get this fixed and released shortly.

Krinkle added a commit that referenced this issue Feb 15, 2022
* For subprocesses in the CLI tests, use `process.execPath` to
  use the same node program for subprocesses; regardless of its
  name, extension or location (e.g. '`odejs` or `node.exe` etc.)

* Use `node bin/qunit.js` instead of `bin/qunit.js` in package.json,
  because the hashbang (`#!/usr/bin/env`) atop the executable is not
  supported on Windows.

* Avoid a problem with quoted arguments in the test runner's
  creation of sub processes. Instead of going through a shell
  (and thus cross-platform differences), pass arguments explicitly
  using Node's built-in mechanism for child processes.
  I've removed the `exaca` dependency in the process and just the
  Node.js API directly.

* Improve output normalization to accomodate Windows' backslashes.

* Skip a handful of tests that don't yet pass, let's make iterative
  progress going-forward to avoid further regressions.

Ref #1667.
Closes #1359.
Krinkle added a commit that referenced this issue Feb 15, 2022
* For subprocesses in the CLI tests, use `process.execPath` to
  use the same node program for subprocesses; regardless of its
  name, extension or location (e.g. '`odejs` or `node.exe` etc.)

* Use `node bin/qunit.js` instead of `bin/qunit.js` in package.json,
  because the hashbang (`#!/usr/bin/env`) atop the executable is not
  supported on Windows.

* Avoid a problem with quoted arguments in the test runner's
  creation of sub processes. Instead of going through a shell
  (and thus cross-platform differences), pass arguments explicitly
  using Node's built-in mechanism for child processes.
  I've removed the `exaca` dependency in the process and just the
  Node.js API directly.

* Improve output normalization to accomodate Windows' backslashes.

* Skip a handful of tests that don't yet pass, let's make iterative
  progress going-forward to avoid further regressions.

Ref #1667.
Closes #1359.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: CLI Type: Bug Something isn't working right.
Development

Successfully merging a pull request may close this issue.

3 participants