【问题标题】:Cannot find module 'react' from 'pure.js'?无法从“pure.js”中找到模块“react”?
【发布时间】:2020-06-22 08:57:49
【问题描述】:

package.json 中运行yarn test

CI=true react-app-rewired test --coverage

我收到以下错误

Test suite failed to run

    Cannot find module 'react' from 'pure.js'

    However, Jest was able to find:
        './pure.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
      at Object.<anonymous> (../../node_modules/@testing-library/react/dist/pure.js:28:37)

不知道为什么要从pure.js 得到react,知道吗?

【问题讨论】:

  • 你是否检查过 React 是否已安装?尝试使用 npm i react 安装并再次运行。如果它有效,那么首先反应没有正确安装。
  • @AJarofClay,我发现该项目有 2 个 package.jsons 顶层目录和内层目录。所以需要坚持内部级别的目录来做开发

标签: reactjs unit-testing jestjs


【解决方案1】:

我也遇到了同样的问题,然后我安装了"react-test-renderer": "^16.13.1",问题就解决了。 所以你可以试试这个也只是通过命令npm install react-test-renderer --save安装了这个依赖。

如下图所示:

【讨论】:

    【解决方案2】:

    Jest 无法找到 'react',因为它要么未安装,要么最接近的 package.json 没有 react 作为依赖项(如果它是 monorepo)。

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 2020-06-05
      • 2015-10-13
      • 2018-04-26
      • 1970-01-01
      • 2018-02-24
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多