Jest.js 无法从“pure.js”中找到模块“react”?

mspsb9vt  于 2023-06-20  发布在  Jest
关注(0)|答案(3)|浏览(184)

当在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,有什么想法吗?

mwkjh3gx

mwkjh3gx1#

我也遇到了同样的问题,然后我安装了"react-test-renderer": "^16.13.1",问题解决了。因此,您可以尝试通过命令**npm install react-test-renderer --save**安装此依赖项。

如下图所示:

eaf3rand

eaf3rand2#

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

qmb5sa22

qmb5sa223#

与@testing-library/react版本不兼容有关

作为mentioned here
我想这是因为你使用的是更新版本的@testing-library/react,试着安装版本12.1.2(或12.x)
[引用由我编辑]
Kalm mentioned here too
如果它是一个mono repo项目,你必须处理好每个package.json文件。
在我的例子中(在一个monorepo项目中),有人安装了一个更高版本的@testing-library/react,它不能与我当前的react版本一起工作,所以解决方案是跟踪每个package.json并检查我使用的是什么版本的@testing-library/react,然后确保它使用的是较低版本

相关问题