Skip to content

Running jest with ES Modules on windows failsΒ #9544

@jonerer

Description

@jonerer

πŸ› 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions