[英]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.