0
votes

I'm new to unit testing and I've just started writing unit tests for my react project. 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.

I've created a new file setupTests.js inside the src directory and add enzyme configurations.

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.

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 enter image description here enter image description here

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.

1
It looks like the @handsontable/react requires @babel/polyfill, then we install npm i @babel/polyfill would resolve the issue :)tmhao2005

1 Answers

0
votes

As suggested by @tmhao2005 after installing @babel/polyfill tests started running without any errors.