Jest gives `Cannot find module` when importing components with absolute paths

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"],
// ...
}

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"
  ],
}

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.