Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest moduleNameMapper to find files: "resolver": undefined

Tags:

I have a text file (amongst others) in the component folder under the path: src/components/text

However, Jest does not find this file when the webpack alias import Text from "components/text"; is used.

I tried adding to package.json:


"jest": {
    "globals": {
      "NODE_ENV": "test"
    },
    "transform": {
      "\\.[jt]sx?$": "babel-jest"
    },
    "verbose": false,
    "rootDir": ".",
    "collectCoverageFrom": [
      "**/*.{js,jsx,ts,tsx}",
      "!**/*.d.ts"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx",
      "ts",
      "tsx"
    ],
    "moduleNameMapper": {
      "\\.(css|less|scss|sass|svg)$": "identity-obj-proxy",
      "^components/(.*)$": "<rootDir>/src/components/$1",
      "^assets/(.*)$": "<rootDir>/src/assets/$1",
      "^utils/(.*)$": "<rootDir>/src/utils/$1",
      "^styles/(.*)$": "<rootDir>/src/styles/$1"
      "/^locales\/(.*)$/": "<rootDir>/src/locales/$1",
    },
    "testMatch": [
      "**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "modulePathIgnorePatterns": [
      "./dist"
    ],
    "transformIgnorePatterns": [
      "/node_modules/(?!(@opt-ui|@equinor))"
    ],
    "coverageDirectory": "<rootDir>/tests/coverage/"
  }

But I'm getting the error:

Test suite failed to run

    Configuration error:
    
    Could not locate module components/text mapped as:
    /Users/olahalvorsen/cssu-dashboard-client/src/components$1.
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^components\/(.*)$/": "/Users/olahalvorsen/cssu-dashboard-client/src/components$1"
      },
      "resolver": undefined
    }

So, "^components/(.*)$": "<rootDir>/src/components/$1"in the moduleNameMapper solved the first issue above:)

But now I'm getting another error:

FAIL  src/pages/errorpage/tests/error.test.jsx
  ● Test suite failed to run

    Cannot find module 'locales' from 'src/utils/helpers/helpers.js'

    Require stack:
      src/utils/helpers/helpers.js
      src/components/text/index.jsx
      src/pages/errorpage/error.jsx
      src/pages/errorpage/tests/error.test.jsx

      29 | import { nb, enGB } from "date-fns/locale";
      30 | 
    > 31 | import translations from "locales";
         | ^
      32 | 
      33 | export const capitalize = string => {
      34 |   if (typeof string === "string") {


I updated the package.json above. The relative directory of locales is src/locales. Shouldn't this work:

  "moduleNameMapper": {
      "^locales/(.*)$": "<rootDir>/src/locales$1",

I tried using: "/^locales\/(.*)$/": "<rootDir>/src/locales/$1"

The solution was to use: "^locales(.*)$": "<rootDir>/src/locales/$1"

like image 366
meerkat Avatar asked Nov 13 '20 07:11

meerkat


People also ask

What is rootDir in Jest?

rootDir [string] Default: The root of the directory containing the package.json or the pwd if no package.json is found. Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

What is Jest config?

The jest. config. js file is used for configuring Jest, the JavaScript testing library used for writing unit and integration tests in Pup. The modulePaths property tells Jest where it can resolve NPM modules used inside of the code you're testing.


1 Answers

Based on the log I guess your configuration is /^components\/(.*)$/: "<rootDir>/src/components$1" which is different from your given code is ^components(.*)$.

Assuming the above is the right one, so you might need to change to as following to make it work properly:

{
  "moduleNameMapper": {
    "/^components\/(.*)$/": "<rootDir>/src/components/$1" // You missed out `/` before the rest value `$1`
  },
}
like image 61
tmhao2005 Avatar answered Oct 12 '22 23:10

tmhao2005