Jest 无法从 node_modules 中找到模块 'got'

Jest cannot find module 'got' from node_modules

当我 运行 yarn jest:

时,我的一项(之前工作的)测试出现错误

Cannot find module 'got' from 'src/rss/queries.ts'

我在 package.json 中添加了 got:

"devDependencies": {
    "got": "^12.0.0",
}

我的jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testMatch: ['**/*.test.ts(|x)'],
  collectCoverageFrom: ['**/*.ts', '!.webpack/**/*'],
  verbose: true,
}

我在 ./src/rss/queries.ts 中使用 got:

import * as got from 'got'

我也试过:

import { got, RequestError } from 'got'

在这两种情况下,应用程序都能正常工作——我可以看到 got 通过应用程序日志发出请求(当我悬停时,vscode 指示 node_modules 中模块的正确路径以上)。所以它肯定在那里,并且在工作。

但是jest找不到。为什么?这不是一个不常见的 Whosebug 问题,但它们似乎都与通过相对路径等导入自定义本地模块有关。我只是想使用 node_modules...

中的一个

无法找到此问题的原因或解决方案。最终将 got 换成 axios,这很遗憾,因为我喜欢许多 got 功能。

got 的第 12 版不适用于 jest。目前最好使用版本 11。请参阅发行说明中的​​详细信息:https://github.com/sindresorhus/got/releases/tag/v12.0.0