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

Running jest with ES Modules on windows fails #9544

Closed
jonerer opened this issue Feb 9, 2020 · 6 comments Β· Fixed by #9558
Closed

Running jest with ES Modules on windows fails #9544

jonerer opened this issue Feb 9, 2020 · 6 comments Β· Fixed by #9558

Comments

@jonerer
Copy link

jonerer commented Feb 9, 2020

πŸ› Bug Report

I can't run jest with a project using ES Modules on windows

To Reproduce

Steps to reproduce the behavior:

  1. mkdir jestgrej
  2. cd jestgrej
  3. yarn add --dev yest
  4. jest --init
    √ Would you like to use Jest when running "test" script in "package.json"? ... yes
    √ Choose the test environment that will be used for testing » node
    √ Do you want Jest to add coverage reports? ... no
    √ Automatically clear mock calls and instances between every test? ... no
  5. jest
C:\prog\node\jestgrej>jest
(node:9068) ExperimentalWarning: The ESM module loader is experimental.
Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:33:11)
    at Loader.resolve (internal/modules/esm/loader.js:85:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:188:40)
    at Loader.import (internal/modules/esm/loader.js:163:28)
    at importModuleDynamically (internal/modules/cjs/loader.js:1094:27)
    at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:37:14)
    at _default (C:\prog\node\jestgrej\node_modules\jest-config\build\importMjs.js:16:29)
    at readConfigFileAndSetRootDir (C:\prog\node\jestgrej\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:105:61)
    at readConfig (C:\prog\node\jestgrej\node_modules\jest-config\build\index.js:217:65)
    at readConfigs (C:\prog\node\jestgrej\node_modules\jest-config\build\index.js:401:32)

C:\prog\node\jestgrej>

I tried to do some debugging and found the cause. With a breakpoint in node_modules\jest-config\build\importMjs.js:16:29 we can see that this line is run:

var _default = specifier => import(specifier);

With a "specifier" value of C:\prog\node\jestgrej\jest.config.mjs. Doing imports of ES Modules with absolute paths doesn't work on windows. See this other issue I made: nodejs/node#31710

A workaround would be to use relative imports. ./jest.config.mjs would work in this case.

Expected behavior

It should be able to start up and run some tests

envinfo

C:\prog\node\jestgrej>npx envinfo --preset jest
npx: installed 1 in 4.218s

  System:
    OS: Windows 10 10.0.17763
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
  Binaries:
    Node: 13.8.0 - C:\tools\nodejs\node.EXE
    Yarn: 1.6.0 - C:\Users\jon\AppData\Roaming\npm\yarn.CMD
    npm: 6.4.0 - C:\tools\nodejs\npm.CMD
  npmPackages:
    jest: ^25.1.0 => 25.1.0

I guess this is related to #9430 . But I didn't see any mention of windows there.

@SimenB
Copy link
Member

SimenB commented Feb 10, 2020

Oh, interesting! We run this on windows CI, odd it didn't pick it up... Does it work if you pass the full path through require('url').pathToFileURL(specifier).href?

@jonerer
Copy link
Author

jonerer commented Feb 10, 2020

Unsure. Could be. On the node issue they replied that I need to put "file:///" at the start. Is that what your code does? (cant test at the moment)

@SimenB
Copy link
Member

SimenB commented Feb 10, 2020

Yup, but I'm not sure if that function works without there being a file:// at the start already

@jonerer
Copy link
Author

jonerer commented Feb 11, 2020

Great, thanks @SimenB !

@SimenB
Copy link
Member

SimenB commented Feb 11, 2020

Thanks for reporting πŸ‘

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants