Error in moduleNameMapper when I use ^constants(.*)$
See original GitHub issue🐛 Bug Report
In moduleNameWrapper if I try to use “constants” as an alias I receive an error when I try to run my tests
To Reproduce
Steps to reproduce the behavior:
- in jest.config.js input this configuration
module.exports = {
transform: {
"^.+\\.jsx?$": '<rootDir>/jest-preprocess.js',
},
moduleNameMapper: {
".+\\.(css|styl|less|sass|scss)$": 'identity-obj-proxy',
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"^constants(.*)$": "<rootDir>/src/shared/constants$1",
},
testPathIgnorePatterns: ['node_modules', '\\.cache', '<rootDir>.*/public'],
transformIgnorePatterns: ['node_modules/(?!(gatsby)/)'],
globals: {
__PATH_PREFIX__: '',
},
testURL: 'http://localhost',
setupFiles: ['<rootDir>/loadershim.js'],
};
- create a folder
src/shared/constants
- populate with some constant file like colors
- create an empty test file just to run it (need to import colors)
- you will get this error , for example
yarn run v1.17.3
$ jest
FAIL src/components/Plans/useBehavior.test.js
● Test suite failed to run
Configuration error:
Could not locate module constants mapped as:
/Users/cliente/Development/w.dental-pj/src/shared/constants$1.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^constants(.*)$/": "/Users/cliente/Development/w.dental-pj/src/shared/constants$1"
},
"resolver": undefined
}
at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:553:17)
at Object.<anonymous> (node_modules/graceful-fs/polyfills.js:1:17)
Expected behavior
When I use "^constants/(.*)$": "<rootDir>/src/shared/constants/$1",
instead its fine. I don’t know why 😕
envinfo
System:
OS: macOS 10.15.4
CPU: (4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
Binaries:
Node: 12.17.0 - ~/.nvm/versions/node/v12.17.0/bin/node
Yarn: 1.17.3 - ~/.yarn/bin/yarn
npm: 6.14.4 - ~/.nvm/versions/node/v12.17.0/bin/npm
npmPackages:
jest: ^26.0.1 => 26.0.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:8
Top Results From Across the Web
`moduleNameMapper` settings in jest.config.js doesn't work ...
According to this issue, I use tsconfig-paths and tsconfig-paths-jest . ... tsconfig.json"); const moduleNameMapper ...
Read more >配置 Jest
A global setup module configured in a project (using multi-project runner) will be triggered only when you run at least one test from...
Read more >Configuring package.json · Jest
By default, Jest runs all tests and produces all errors into the console upon completion. The bail config option can be used here...
Read more >Solve Module Import Aliasing for Webpack, Jest, and VSCode
import { someConstant } from 'Config/constants' ... Configure Jest in your package.json file using the moduleNameMapper property. "jest": {
Read more >9 JEST Configuration Parameters for testing your React App
moduleNameMapper [Object] (Default: null). This configuration holds the key to file mocking. By using this configuration all the external asset files like ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any news on this?
EDIIT: Turns out that in my case, it was the dreaded case insensitive paths on MacOS.
We’re now getting the same error with
"^constants(.*)$": "<rootDir>/constants$1"
as of this PR: https://github.com/amido/stacks-webapp-template/pull/445It seems to have been introduced between
25.4.0
which we’re on and26.2.2
, which is what the PR is upgrading to.EDIT: @mathvaleriano’s workaround works for us, but hopefully the information above helps with getting an actual fix for this.