81
votes

Receiving the following error when running Jest

Cannot find module 'src/views/app' from 'index.jsx'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
  at Object.<anonymous> (src/index.jsx:4:12)

index.jsx

import AppContainer from 'src/views/app';

package.json

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
    ],
    "testEnvironment": "node",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
    ],
    "moduleDirectories": [
        "node_modules",
        "src"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs"
    ]
  },

My tests that run files that only contain relative paths in the tree run correctly.

To Clarify, I'm looking for how to configure Jest to not fail on absolute paths.

5
try import AppContainer from './src/views/app'; - Metalik
I need to know how to run absolute paths so I don't have to back step multiple directories on imports or update as many files if I move files - Seth McClaine

5 Answers

52
votes

I think you're looking for: roots or modulePaths and moduleDirectories

You can add both relative and absolute paths.

I would make sure to include <rootDir> in the roots array, <rootDir> in the modulePaths array, and node_modules in the moduleDirectories array, unless you've got a good reason to exclude them.

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}
46
votes

Since in package.json you have:

"moduleDirectories": [
    "node_modules",
    "src"
]

Which says that each module you import will be looked into node_modules first and if not found will be looked into src directory.

Since it's looking into src directory you should use:

import AppContainer from 'views/app';

Please note that this path is absolute to the src directory, you do not have to navigate to locate it as relative path.

OR you can configure your root directory in moduleDirectories inside your pakcage.json so that all your components could be imported as you want it.

8
votes

Adding

"moduleDirectories": [
    "node_modules",
    "src"
]

should work if you have Jest's config in your package.json file.

If you have a jest.config.js file, you should add it there, otherwise package.json will be overriden (and ignored) by this config file. So in your jest.config.js file:

module.exports = {
// ... lots of props
  moduleDirectories: ["node_modules", "src"],
// ...
}
0
votes

Adding __esModule:true fixed this issue for me.

jest.mock('module',()=>({
  __esModule: true,                    // this makes it work
  default: jest.fn()
}));

Hope this helps somebody. Although this is not very specific to the question.

0
votes

For those who are building something from scratch with Webpack and Babbel. Try the following steps:

  1. Delete the node_modules folder and install again. (This was something that solved my issue).

  2. Here is a link with the necessary documentation to set up Webpack which in some cases will not be necessary. Jest Docs Webpack

  3. Here is a link to the docs that explains how to set up Jest with React (Without using Create-React-App). Jest React Docs

4. Here is an example with a simple setup with Jest. You can set this up in package.json or the Jest configuration file.

Disclaimer: This does not answer the OP question. But most people will end up here for the keywords used for this issue.

  "jest": {
"moduleFileExtensions": ["js", "jsx"],
"moduleDirectories": ["node_modules"],
"moduleNameMapper": {
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
  "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
}

},