Skip to content

Jest cannot find module 'zx/globals' #308

Discussion options

You must be logged in to vote

Reference for anyone who stumbles upon this. @antonmedv seems like the way jest resolves imports for the chalk lib is rather wonky since the chalk lib aliases some imports in its package.json, which jest doesn't know about.

Here is the solution.

TLDR

Add the following to your jest config

  moduleFileExtensions: [...otherFileExtensionsInYourProject, 'mjs'],
  moduleNameMapper: {
    "#ansi-styles": "<rootDir>/node_modules/zx/node_modules/chalk/source/vendor/ansi-styles/index.js",
    "#supports-color": "<rootDir>/node_modules/zx/node_modules/chalk/source/vendor/supports-color/index.js"
  }

For the curious

  1. First, jest by default does not resolve .mjs modules. This is easy to fix using the m…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by OlaoluwaM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #307 on February 20, 2022 06:33.