首页 > 解决方案 > 如何修复抛出“找不到模块”错误的 Jest 测试?

问题描述

对于我在运行 Jest 测试时遇到的错误,我将不胜感激。

当我在 Mozilla 的 Devtools 上工作时,可以在这里找到我正在运行的代码:

https://hg.mozilla.org/mozilla-central/file

我在文件 devtools/client/debugger/src/components/Editor/SearchBar.js 中添加了一个导入语句。文件在这里:

 https ://hg.mozilla.org/mozilla-central/file/tip/devtools/client/debugger/src/components/Editor/SearchBar.js

导入语句是:

从“devtools/shared/plural-form”导入 { PluralForm };

我在代码更改中使用此功能。出于某种原因,这会在测试“Editor.spec.js”和“SearchBar.spec.js”中产生错误。

可以在这里找到测试:https ://hg.mozilla.org/mozilla-central/file/tip/devtools/client/debugger/src/components/Editor/tests

错误是:无法从“SearchBar.js”中找到模块“devtools/shared/plural-form”

当我运行代码时,该函数运行良好,但 Jest 很难解析模块。

任何帮助,将不胜感激!

我尝试在配置文件中添加到 modulePaths。

无法从“SearchBar.js”中找到模块“devtools/shared/plural-form”

  37 | import type SourceEditor from "../../utils/editor/source-editor";
  38 | 
> 39 | const { PluralForm } = require("devtools/shared/plural-form");
     |                        ^
  40 | // import { PluralForm } from "devtools/shared/plural-form";
  41 | 
  42 | function getShortcuts() {

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
  at Object.<anonymous> (src/components/Editor/SearchBar.js:39:24)

标签: jestjs

解决方案


推荐阅读