React应用Jest单元测试失败:找不到node:stream模块求助
React应用Jest单元测试报错:找不到
node:stream模块 报错信息
Test suite failed to run Cannot find module 'node:stream' from 'index.js' However, Jest was able to find: './Utils.js' './Utils.js.map' You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node']. See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string However, Jest was able to find: './utils.js' You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node']. See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string However, Jest was able to find: './createSerializer.js' You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', '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/parse5-parser-stream/dist/cjs/index.js:4:23)
解决方案
- 升级Jest版本:Jest 27.x及以上版本才支持Node.js的
node:协议导入,直接升级Jest到最新版即可解决。执行命令:npm install --save-dev jest@latest # 或使用yarn yarn add --dev jest@latest - 添加模块映射(兼容旧版Jest):如果暂时无法升级Jest,在
jest.config.js中配置模块映射,将node:开头的模块重定向到对应原生模块:module.exports = { // 保留原有配置 moduleNameMapper: { '^node:(.*)$': '$1' } }; - 检查Node.js版本:确保Node.js版本在14.13.0+或12.20.0+,过低版本不支持
node:协议,升级Node.js到符合要求的版本即可。
内容的提问来源于stack exchange,提问作者Srikanth Kuriyala
相关产品推荐
相关产品推荐

