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

[Bug?]: jest fails with Cannot find module 'source-map-support' when using pnpm linker #3741

Closed
1 task
AlCalzone opened this issue Nov 18, 2021 · 8 comments
Closed
1 task
Labels
bug Something isn't working

Comments

@AlCalzone
Copy link

Self-service

  • I'd be willing to implement a fix

Describe the bug

I'm currently trying to migrate from the PnP linker to pnpm. However, jest now fails every test with the following error:

Test suite failed to run

    Cannot find module 'source-map-support' from 'node_modules/.store/source-map-support-npm-0.5.19-65b33ae61e/source-map-support.js'

      at Resolver.resolveModule (node_modules/.store/jest-resolve-npm-26.6.2-6ad3c87f85/build/index.js:306:11)

Both files exist on disk.

To reproduce

This seems to happen in a specific monorepo setup, so I'm not quite sure if this is a yarn issue or a jest issue or just an incorrect configuration. You can see it in action by

  1. cloning https://github.com/zwave-js/node-zwave-js/tree/059b44d5060880a2131f9d0feb5e8f31141ca980
  2. yarn
  3. yarn test

Environment

System:
    OS: Windows 10 10.0.19043
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
  Binaries:
    Node: 16.10.0 - ~\AppData\Local\Temp\xfs-2a6d2a17\node.CMD
    Yarn: 3.1.0 - ~\AppData\Local\Temp\xfs-2a6d2a17\yarn.CMD
    npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^26.6.3 => 26.6.3

Additional context

No response

@AlCalzone AlCalzone added the bug Something isn't working label Nov 18, 2021
@AlCalzone
Copy link
Author

Nevermind, I figured it out. The tests are run from the root workspace which didn't have the source-map-support dependency (only sub workspaces have it because they use it).

For some reason this worked with PnP and node_modules linkers but not in pnpm.

@Adjective-Object
Copy link

Ran into this same problem in a monorepo where tests are run in each package individually.

Was able to get past it by installing source-map-support in the repo root as well, but started getting OOMs before my code runs during module resolution.

Breakpointing inside jest, it's OOMing trying to build a require stack where a module fails to find itself
image

This looks related to the error you were hitting, since node_modules\.store\source-map-support-npm-0.5.19-65b33ae61e\node_modules\source-map-support\source-map-support.js imports source-map-support, which would presumably resolve to itself?

What's weird is that that source file doesn't import source-map-support at all.. Is there some kind of internal thing in jest that prepends an import to source-map-support to sources? If so, it would reason that somehow source-map-support is being loaded by jest in a way that an import to itself is being prepended.

@Adjective-Object
Copy link

Adjective-Object commented Apr 23, 2022

Strike that, the internal error is here:

Error: Cannot find module 'source-map' from '../../../node_modules/.store/source-map-support-npm-0.5.19-65b33ae61e/node_modules/source-map-support/source-map-support.js'

the overflow is because this loop in jest repeats infinitely when (module.parent === module).
https://github.com/facebook/jest/blob/811228d6ae73a6563a98b0ee36b73f453e644f2f/packages/jest-runtime/src/index.ts#L2252-L2258

Which it is in source-map-support.

The underlying issue is that when falling back to native resolve:
https://github.com/facebook/jest/blob/0208815654e54814c3a4477fb366a5f1e1d8b997/packages/jest-resolve/src/resolver.ts#L236

Jest resolves from the current working directory, rather than from the directory of the file. Which means in a pnpm-style install, you need to have both source-maps and source-map-support installed somewhere that native resolve can find them, which is why installing them at the root of your repo works.

I think that means in order to be properly specified, jest should be setting source-maps and source-map-support as dependencies. I wonder why they don't resolve from the importer at right now?

You also need to add buffer-from at a compatible version to your repo root, since source-map-support imports that.

@SimenB
Copy link

SimenB commented Apr 26, 2022

This might have been fixed in Jest 28 via jestjs/jest#12706

@Voltra
Copy link

Voltra commented Mar 16, 2023

Still having this issues with jest@^29.5.0

@luketych
Copy link

luketych commented Jan 7, 2024

Yes still have this issue... Please advise..

@SimenB
Copy link

SimenB commented Jan 8, 2024

Please create a new issue with a reproduction

@Slicefresh22
Copy link

i ran into the same issue.

Check your jest.config you probably have the wrong moduleDirectories path definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants