Jest: Error in moduleNameMapper when I use ^constants(.*)$

Created on 3 Jun 2020  ยท  6Comments  ยท  Source: facebook/jest

๐Ÿ› 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 
Bug Report Needs Repro Needs Triage

Most helpful comment

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.

All 6 comments

sure ๐Ÿ˜„ but in the tests we just have a check with this pattern /^constants$/ and none for something like /^constants(.*)$/ ๐Ÿค”

i've changed the current test to this pattern and it has been passed ๐Ÿค” ๐Ÿ˜…

Folders with the name "core" might also cause a similar error. @mathvaleriano's workaround fixed it.

Could not locate module core-js-pure/stable/object/define-property mapped as:
    C:\Code\my-project\src\core\$1.

^core(.*)$ -> /^core(.*)$/

I did something like this at the same day, but thank you for this example

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Secretmapper picture Secretmapper  ยท  3Comments

StephanBijzitter picture StephanBijzitter  ยท  3Comments

kentor picture kentor  ยท  3Comments

hramos picture hramos  ยท  3Comments

kgowru picture kgowru  ยท  3Comments