首页 > 解决方案 > 开玩笑:配置中的 modulePaths 和/或 moduleDirectories 仍然不会使导入的模块出现

问题描述

我正在尝试为我的 React 组件编写一些测试,并且在导入模块时遇到了一些麻烦。我的jest.config.js文件位于我的项目的根目录中。我的配置中有根、modulePaths 和 moduleDirectories,并尝试了一些我从示例和教程中阅读的内容。不管我做什么,尽管我一直遇到这个问题

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

我不确定自己做错了什么,并尝试尽我所能,阅读 Jest 文档并四处寻找教程文章,但很长一段时间都没有运气。我可能完全误解了模块加载在 Jest 中的工作方式。

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

标签: reactjsmeteormodulejestjs

解决方案


推荐阅读