我是单元测试的新手,我刚刚开始为我的 React 项目编写单元测试。我的项目是使用 create-react-app 创建的,我已经安装了所需的测试库,例如酶、酶适配器反应 16 和开玩笑酶。
我在 src 目录中创建了一个新文件 setupTests.js 并添加酶配置。
当我尝试使用命令运行单元测试时,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 setupTests.js file looks like inside the src directory
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. 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
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
I'm assuming that inside components/TestComponent1.jsx I'm importing import { HotTable } from "@handsontable/react";
Because of this the test suite is failing and giving an error.
As suggested by @tmhao2005 after installing @babel/polyfill
tests started running without any errors.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句