I am using Jest with React to write unit test cases for my project with the following specs. When I run yarn test (Jest –coverage). It gives me below error.
Error: Test suite failed to run
Jest encountered an unexpected token.....
Details:
<root dir>/node_modules/cheerio/dist/browser/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { contains, merge } from './static.js';
^^^^^^
SyntaxError: Unexpected token 'export'
3 | import { configure } from 'enzyme';
4 |
> 5 | global.ResizeObserver = jest.fn().mockImplementation(() => ({
| ^
6 | observe: jest.fn(),
7 | unobserve: jest.fn(),
8 | disconnect: jest.fn(),
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (node_modules/enzyme/build/Utils.js:93:16)
at Object.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:33:14)
at Object.<anonymous> (node_modules/enzyme/build/index.js:3:21)
at Object.<anonymous> (node_modules/@wojtekmaj/enzyme-adapter-react-17/build/ReactSeventeenAdapter.js:11:15)
at Object.<anonymous> (node_modules/@wojtekmaj/enzyme-adapter-react-17/build/index.js:3:18)
at Object.<anonymous> (test/setup.js:5:50)
My Jest and Babel versions respectively are 29.7.0 and 7.24.0.
Here is my Jest.config.js file.
module.exports = {
testEnvironment: 'jsdom',
setupFiles: ["./test/setup.js"],
testURL: "http://localhost/",
moduleNameMapper: {
"^.+\.(less|css)$": "babel-jest",
},
transformIgnorePatterns: [
"/node_modules/?!(cheerio|jest-runtime|enzyme|@wojtekmaj)"
],
testTimeout: 30000,
};
Here is my setup.js file:
import 'raf/polyfill';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { configure } from 'enzyme';
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}))
global.CSS = {
escape: (str) => str,
};
configure({
adapter: new Adapter(),
});
I tried multiple online solutions tweaking the regexp for transformIgnorePattern field but no luck. Please suggest on the right regexp to use here to get rid of this error or anything else that would be needed.