reactjs - 开玩笑:配置中的 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"
],
};
解决方案
推荐阅读
- python - 有没有一种优雅的方式来定义关系?
- phpmyadmin - phpmyadmin:无法登录 MySQL 服务器
- javascript - 使用 JavaScript 循环浏览图像的问题
- c# - 玩家卡在墙上 | Unity2D
- swift - 为什么从父视图调用子视图的方法不更新子视图?
- asp.net-core - Asp.Net Core 3.1 Identity LinqToDB.Identity UserStore 问题
- python - 如何从另一个程序主动写入的文件中读取?
- python - 拆分DataFrame中特定列中的数据
- javascript - 学习 ES2015 语法有困难,尤其是对象方法
- c# - C# UWP 设置 MP3 缩略图