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

`qunit --require` does not find local module if qunit is globally installed #1372

Open
jeremiahlee opened this issue Jan 24, 2019 · 8 comments

Comments

5 participants
@jeremiahlee
Copy link

commented Jan 24, 2019

Tell us about your runtime:

  • QUnit version: 2.9.1
  • What environment are you running QUnit in? (e.g., browser, Node): Node v11.7.0
  • How are you running QUnit? (e.g., script, testem, Grunt): CLI

What are you trying to do?

Pull request #1271 added the --require option, but it does not seem to work with esm, as @jdalton hoped.

I wrote a test of using esm with qunit --require. Perhaps I am not doing it correctly. If you can help me figure this out, I would happily contribute to qunitjs/qunitjs.com#144.

npm install to get esm package.

npm install -g qunit to get qunit globally.

qunit --require esm test.js to run the test.

What actually happened?

TAP version 13
not ok 1 test.js > Failed to load the test file with error:
/Users/jeremiah/Projects/esm-qunit-experiment/test.js:1
(function (exports, require, module, __filename, __dirname) { import {bar} from './foo.js';
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:684:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)
  ---
  message: "should be able to load file"
  severity: failed
  actual: false
  expected: true
  stack:     at Object.<anonymous> (/usr/local/lib/node_modules/qunit/src/cli/run.js:60:13)
  ...
1..1
# pass 0
# skip 0
# todo 0
# fail 1

What did you expect to happen?

TAP version 13
ok 1 Hinkle finkle dinkle doo
1..1
# pass 1
# skip 0
# todo 0
# fail 0
@platinumazure

This comment has been minimized.

Copy link
Contributor

commented Jan 24, 2019

Hi @jeremiahlee, thanks for the issue.

Just out of curiosity, does it work if both QUnit and esm are installed locally or globally? Asking because ESLint has a similar issue where a global ESLint can't find local plugins.

@jeremiahlee

This comment has been minimized.

Copy link
Author

commented Jan 24, 2019

Good question, @platinumazure. I hadn't tried those variations.

I tried all globally first:

npm uninstall esm
npm install -g esm
qunit --require esm test.js

I got the same error.

Then I tried installing esm and qunit locally:

npm uninstall -g esm
npm uninstall -g qunit
npm install esm
npm install qunit
npx qunit --require esm test.js

and it worked!

TAP version 13
ok 1 Hinkle finkle dinkle doo
1..1
# pass 1
# skip 0
# todo 0
# fail 0

So good news! I'm unblocked. But is this expected behavior?

@jdalton

This comment has been minimized.

Copy link

commented Jan 24, 2019

Hi @jeremiahlee 👋

Thanks for trying esm with QUnit!
I can speak a little on the resolution front. I believe --require works for local modules, so your npm i esm use is ok, because Node itself cannot find globally installed packages using require().

@jeremiahlee

This comment has been minimized.

Copy link
Author

commented Jan 24, 2019

@jdalton Thanks for jumping in! I first tried esm locally with global qunit. Are you aware of anything with Node that would prevent global qunit resolving local esm?

@jdalton

This comment has been minimized.

Copy link

commented Jan 24, 2019

Are you aware of anything with Node that would prevent global qunit resolving local esm?

Without QUnit doing some coding require() use in a globally installed package would not resolve local packages. So this may be something QUnit may extend support for using require.resolve with the options param (added in Node 8.9.0) to resolve packages from the process.cwd().

@martypdx

This comment has been minimized.

Copy link

commented Jan 25, 2019

I'm not able to get it work with a pure local install:

~/classes/vanilla-web/coin-toss > npm ls -g esm
/Users/marty/.node/lib
└── (empty)

~/classes/vanilla-web/coin-toss > npm ls -g qunit
/Users/marty/.node/lib
└── (empty)

~/classes/vanilla-web/coin-toss > npm ls esm qunit
/Users/marty/classes/vanilla-web/coin-toss
├── esm@3.1.1 
└── qunit@2.9.1 

~/classes/vanilla-web/coin-toss > npx qunit --require esm test/tests.js
TAP version 13
not ok 1 test/tests.js > Failed to load the test file with error:
/Users/marty/classes/vanilla-web/coin-toss/test/tests.js:2
import getHeadsOrTails from '../src/get-heads-or-tails.js';
       ^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
  ---
  message: "should be able to load file"
  severity: failed
  actual: false
  expected: true
  stack:     at Object.<anonymous> (/Users/marty/classes/vanilla-web/coin-toss/node_modules/qunit/src/cli/run.js:60:13)
  ...
1..1
# pass 0
# skip 0
# todo 0
# fail 1
@martypdx

This comment has been minimized.

Copy link

commented Jan 29, 2019

Work around is to use an intermediate file index.js file and use the "manual" esm method:

/* eslint-disable */
require = require('esm')(module);
module.exports = require("./tests.js");

Then launch that file:

> npx qunit tests/index.js

@Krinkle Krinkle changed the title `qunit --require esm` does not work `qunit --require` does not find local module if qunit is globally installed Apr 13, 2019

@Krinkle

This comment has been minimized.

Copy link
Member

commented Apr 13, 2019

I think a locally installed qunit should not find a globally installed module. Aside from not having a clear use case, it also seems unexpected. Might obscure issues locally you'd then find elsewhere.

However, the case of globally installing qunit and finding a local module seems one I expect to work.

I do generally recommend for project management to keep versioning of dependencies local to a project (and run via npx or npm run). Nonetheless, installing qunit globally is something we support and I'd expect that to work with local modules.

I've updated the task summary to reflect that use case to distinguish it from the case where both are local, which appears to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.