简体   繁体   English

用酶和玩笑测试 React 给出错误无法从“index.js”中找到模块“@babel/polyfill/lib/noConflict”

[英]Testing React with enzyme and jest giving error Cannot find module '@babel/polyfill/lib/noConflict' from 'index.js'

I'm new to unit testing and I've just started writing unit tests for my react project.我是单元测试的新手,我刚刚开始为我的 React 项目编写单元测试。 My project is created using create-react-app and I've installed required testing libraries such as enzyme, enzyme-adapter-react-16, and jest-enzyme.我的项目是使用 create-react-app 创建的,我已经安装了所需的测试库,例如酶、酶适配器反应 16 和开玩笑酶。

I've created a new file setupTests.js inside the src directory and add enzyme configurations.我在 src 目录中创建了一个新文件 setupTests.js 并添加酶配置。

When I'm trying to run the unit test using the command npm test I'm receiving the below error and because of that my test is failing.当我尝试使用命令npm test运行单元测试时,我收到以下错误,因此我的测试失败。

src/App.test.js
● Test suite failed to run

Cannot find module '@babel/polyfill/lib/noConflict' from 'index.js'

However, Jest was able to find:
    './ComponentName.jsx'

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

Below is the screenshot of the error下面是错误截图在此处输入图片说明 在此处输入图片说明

Below is the setupTests.js file looks like inside the src directory下面是 src 目录中的 setupTests.js 文件

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'jest-enzyme';

configure({ adapter: new Adapter() });

Below is my test App.test.js file looks like where I'm just trying to console.log and here it's failing.下面是我的测试 App.test.js 文件看起来像我只是尝试 console.log 而这里它失败了。 If I remove App's import it will work.如果我删除应用程序的导入,它将起作用。

import React from 'react';
import App from './App'
import { shallow } from 'enzyme';

test("this is my first test", () => {
    console.log('this is my first test')
});

I'm trying this on WSL Ubuntu 18.04 LTS and Node 12.8.4我正在 WSL Ubuntu 18.04 LTS 和 Node 12.8.4 上尝试这个

Can anyone please tell me where I'm making mistake and correct me and Kindly excuse if the question is repeated.任何人都可以告诉我我在哪里犯了错误并纠正我,如果问题重复,请原谅。

Thank you in advance.先感谢您。

Update更新

Here is the GitHub link where I tried to reproduce the error https://github.com/aashayamballi/react-jest-enzyme-testing-error这是我试图重现错误的 GitHub 链接https://github.com/aashayamballi/react-jest-enzyme-testing-error

I'm assuming that inside components/TestComponent1.jsx I'm importing import { HotTable } from "@handsontable/react";我假设在 components/TestComponent1.jsx 中我正在import { HotTable } from "@handsontable/react";导入import { HotTable } from "@handsontable/react"; Because of this the test suite is failing and giving an error.因此,测试套件失败并给出错误。

正如@tmhao2005 所建议的,安装@babel/polyfill polyfill 测试开始运行后没有任何错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):错误:找不到模块“babel-preset-react” - Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-react' 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):错误:找不到模块“./src/data” - Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module './src/data' 模块构建失败(来自./node_modules/babel-loader/lib/index.js):错误:找不到模块'babel-core' - Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-core' 错误:index.js: [BABEL]: 在 react-native 中找不到模块 'node:fs' - error: index.js: [BABEL]: Cannot find module 'node:fs' in react-native 错误:无法从“index.js”中找到模块“entities/lib/escape.js” - ERROR: Cannot find module 'entities/lib/escape.js' from 'index.js' React 构建问题,ERROR in./src/index.js 模块构建失败(来自./node_modules/babel-loader/lib/index.js): - React build issue, ERROR in ./src/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js): 如何解决 React 错误模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): - how to resolve React error Module build failed (from ./node_modules/babel-loader/lib/index.js): 反应构建错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): - React build error: Module build failed (from ./node_modules/babel-loader/lib/index.js): 如何解决 React 项目中的酶/Jest 错误“无法从 'react-router.js' 中找到模块 'react'” - How to solve an Enzyme/Jest error "Cannot find module 'react' from 'react-router.js'" in a React project 错误 in./src/index.js 模块构建失败(来自./node_modules/babel-loader/lib/index.js): - ERROR in ./src/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM