Skip to content

ERR_INVALID_MODULE_SPECIFIER with dynamic import via jest-light-runner --runInBand #1747

Description

@cjroebuck

Search Terms

ERR_INVALID_MODULE_SPECIFIER
jest-light-runner
typescript esm

Expected Behavior

It should transpile and run the typescript file

Actual Behavior

ERR_INVALID_MODULE_SPECIFIER when running the command

Steps to reproduce the problem

See the repro below

npm run test gives TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file:///path/to/ts-node-repros/src/tests/foo.ts"

npm run test:working runs the tests successfully

The only difference is the --runInBand option which is implemented like this in jest-light-runner:

nicolo-ribaudo/jest-light-runner@fb70325

I think it's something to do with dynamic import and perhaps the ts-node/esm loader is not being invoked in this case or something?

Minimal reproduction

https://github.com/cjroebuck/ts-node-repro

Specifications

ts-node v10.7.0
node v16.14.2
compiler v4.6.4
tsconfig.json, if you're using one:

{
    "extends": "ts-node/node16/tsconfig.json",
    "ts-node": {
        "swc": true
    },
    "compilerOptions": {
        "module": "ESNext"
    }
}
  • Operating system and version: mac os monterey 12.0.1 (Apple M1 Max)

Activity

  1. cspotcode commented on May 11, 2022

    @cspotcode
    Collaborator
  2. cjroebuck commented on May 11, 2022

    @cjroebuck
    Author

    Yes, it seems that esmock explicitly checks for --loader=esmock in argv, so it is not currently possible to use the NODE_OPTIONS env variable to enable esmock at the same time as ts-node/esm.

    I'm also using your multiloader package for this so thank you for that too :)

    The way I got this working in the end, with esmock and ts-node/esm loaders, was to change the command from:

    NODE_OPTIONS='--experimental-specifier-resolution=node --loader @cspotcode/multiloader/compose?esmock,ts-node/esm' node --loader=esmock node_modules/.bin/jest --watch --runInBand --testPathPattern __tests__/*

    to:

    node --experimental-specifier-resolution=node --loader=esmock --loader @cspotcode/multiloader/compose?esmock,ts-node/esm node_modules/.bin/jest --watch --runInBand --testPathPattern __tests__/*

    The first --loader=esmock keeps esmock happy, but I'm assuming just gets ignored by node, because if I switch the order of the --loader's then I'm back to the original error (ERR_INVALID_MODULE_SPECIFIER).

  3. cspotcode commented on May 11, 2022

    @cspotcode
    Collaborator

    Very clever, I'm glad it's working. I expect things may change as soon as node releases support for chaining multiple loaders. They have already merged the PR, so I expect that will be available in the next node release. Then you'll be able to pass --loader multiple times, and node will take care of chaining them together.

    Is this a bug in esmock? It should support NODE_OPTIONS. If you agree, I recommend opening an issue with them.

    Given that this is not a bug report nor a feature request, but is rather a useful how-to guide, I'm going to convert it to a Discussion.

  4. locked and limited conversation to collaborators on May 11, 2022
  5. converted this issue into a discussion #1748 on May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions