Jest encountered an unexpected token
For anyone using create-react-app, only certain jest configurations can be changed in package.json when using create-react-app.
I have issues with Jest picking up an internal library, Jest would display 'unexpected token' errors wherever I had my imports from this library.
To solve this, you can change your test script to the below:
"test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!(<your-package-goes-here>)/)'",
Add this in your package.json
jest config.
"transform": {
"\\.js$": "<rootDir>/node_modules/babel-jest"
},
Let me know if the issue still persists.
I also encountered the same error while setting up Jest in my React app created using Webpack. I had to add @babel/preset-env
and it was fixed. I have also written a blog article about the same.
npm i -D @babel/preset-env
And then add this in "presets" in .babelrc
file. E.g.
{
"presets": ["@babel/react", "@babel/env"]
}
https://medium.com/@shubhgupta147/how-i-solved-issues-while-setting-up-jest-and-enzyme-in-a-react-app-created-using-webpack-7e321647f080?sk=f3af93732228d60ccb24b47ef48d7062
for anyone who struggled with this issue and non of the above answers worked for him/her.
after about a long time of searching, I reached for this solution
edit your jest.config.js to add transformIgnorePatterns
//jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testMatch: ["**/__tests__/**/*.ts?(x)", "**/?(*.)+(test).ts?(x)"],
transform: {
"^.+\\.(js|ts)$": "ts-jest",
},
transformIgnorePatterns: [
"/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.js$",
"/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.ts$",
"/node_modules/(?![@autofiy/autofiyable|@autofiy/property]).+\\.tsx$",
],
}
put the packages that you want to ignore inside []
and separate them by |
in my case [@autofiy/autofiyable|@autofiy/property]