'Jest TypeScript tests failing with: export default data; ^^^^^^ SyntaxError: Unexpected token 'export'
I'm trying to add tests to an existing React TypeScript App.
But every time I run the tests, it's failing with this error:
/node_modules/@iconify/icons-eva/menu-2-fill.js:6
export default data; ^^^^^^
SyntaxError: Unexpected token 'export'
1 | import { Icon } from "@iconify/react"; > 2 | import menu2Fill from "@iconify/icons-eva/menu-2-fill"; | ^ 3 | import { alpha, styled } from "@mui/material/styles"; 4 | import { Box, Stack, AppBar, Toolbar, IconButton } from "@mui/material"; 5 | at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14) at Object.<anonymous> (src/layouts/dashboard/DashboardNavbar.tsx:2:1)
Referred to Jest setup "SyntaxError: Unexpected token export" and tried to add moduleNameMapper and transformIgnorePatterns to my jest.config.js file. This is how it looks.
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
verbose: true,
transform: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js",
"^.+\\.tsx?$": "ts-jest",
"^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
},
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
setupTestFrameworkScriptFile: "<rootDir>/src/setupTests.ts",
moduleNameMapper: {
"@iconify/icons-eva/(.*)$": "@iconify/react/dist/$1",
},
transformIgnorePatterns: ["<rootDir>/node_modules/(?!@iconify)"],
};
It stills fails.
This is what my package.json file looks like in case that helps:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",
"@fontsource/roboto": "^4.5.3",
"@iconify/icons-eva": "^1.2.0",
"@iconify/icons-ic": "^1.2.0",
"@iconify/react": "^3.1.3",
"@mui/icons-material": "^5.4.1",
"@mui/lab": "^5.0.0-alpha.68",
"@mui/material": "^5.4.1",
"@mui/styled-engine-sc": "^5.4.1",
"@reduxjs/toolkit": "^1.7.2",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"@types/node": "^12.20.46",
"@types/react": "^16.14.23",
"@types/react-dom": "^16.9.14",
"@types/react-redux": "^7.1.22",
"axios": "^0.26.0",
"change-case": "^4.1.2",
"formik": "^2.2.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet-async": "^1.2.2",
"react-redux": "^7.2.6",
"react-router-dom": "^6.2.1",
"react-scripts": "5.0.0",
"simplebar-react": "^2.3.6",
"styled-components": "^5.3.3",
"typescript": "^4.1.6",
"yup": "^0.32.11"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/jest": "^24.9.1"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
