question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

  1. 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'],
};
  1. create a folder src/shared/constants
  2. populate with some constant file like colors
  3. create an empty test file just to run it (need to import colors)
  4. 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:open
  • Created 3 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

3reactions
devdpontescommented, May 13, 2021

Any news on this?

EDIIT: Turns out that in my case, it was the dreaded case insensitive paths on MacOS.

2reactions
ElvenSpellmakercommented, Aug 3, 2020

We’re now getting the same error with "^constants(.*)$": "<rootDir>/constants$1" as of this PR: https://github.com/amido/stacks-webapp-template/pull/445

It seems to have been introduced between 25.4.0 which we’re on and 26.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found