Jest: modulePaths and/or moduleDirectories in configuration still don't make imported modules present

Viewed 928

I am trying to write some tests for my React components and am having a bit of trouble with module imports. My jest.config.js file is located in the root of my project. I have roots, modulePaths, and moduleDirectories all in my configuration, and have tried a few things I have read from examples and tutorials. No matter what I do though I keep getting this issue

Cannot find module '/path/to/moduleA' from '/path/to/moduleB/which/imports/moduleA

I'm not sure what I'm doing wrong, and have tried to do my due dilligence, reading the Jest documentation and looking around for tutorials articles quite some time without luck. I might be totally misunderstanding how module loading works in Jest as well.

jest.config.js

module.exports = {
  // All imported modules in your tests should be mocked automatically
  automock: false,

  // Automatically clear mock calls and instances between every test
  clearMocks: true,

  // The test environment that will be used for testing
  testEnvironment: "node",

  // The glob patterns Jest uses to detect test files
  testMatch: [
    "**/__tests__/**/*.[jt]s?(x)",
    "**/?(*.)+(spec|test).[tj]s?(x)"
  ],

  roots: [
    "<rootDir>",
  ],

  modulePaths: [
    "<rootDir>",
    "/path/to/moduleA"
  ],

  moduleDirectories: [
    "node_modules",
    "/path/to/moduleA"
  ],
};
0 Answers
Related