I updated the Axios from “axios”: “^0.25.0” to “axios”: “^1.8.2”, and Jest tests are not working anymore and app fails to start. Even after I add “jest”: { “transformIgnorePatterns”: [“node_modules/(?!axios)/”] } in package.json, jest still fails on importing axios.
1. Jest tests are not working with the following error:
FAIL src/domain/package/package-confirmation/__tests__/FileListItem.test.js
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
C:devjagjag-file-submission-devsrcfrontendefiling-frontendnode_modulesaxiosindex.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js'; // This module is intended to unwrap Axios default export as named.
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import axios from "axios";
| ^
2 | import FileSaver from "file-saver";
3 |
4 | /**
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/domain/documents/DocumentService.js:1:1)
2. yarn start fails with the following error:
ERROR in ./node_modules/axios/lib/utils.js 714:91-98
Module not found: Error: Can't resolve 'process/browser' in 'C:devjagjag-file-submission-devsrcfrontendefiling-frontendnode_modulesaxioslib'
Did you mean 'browser.js'?
BREAKING CHANGE: The request 'process/browser' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.
ERROR in ./node_modules/axios/lib/utils.js
Cannot read properties of undefined (reading 'module')
webpack compiled with 2 errors and 2 warnings
3. package.json
{
"name": "efiling-frontend",
"version": "1.5.1",
"private": true,
"homepage": "/efilinghub",
"dependencies": {
"@babel/plugin-syntax-jsx": "^7.16.5",
"@babel/preset-react": "^7.18.6",
"@bcgov/bootstrap-theme": "github:bcgov/bootstrap-theme",
"assert": "^2.0.0",
"axios": "^0.25.0",
"axios-auth-refresh": "^2.2.8",
"babel-plugin-transform-export-extensions": "^6.22.0",
"bootstrap": "^4.5.3",
"browserslist": "^4.19.1",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"dinero.js": "^1.8.1",
"file-saver": "^2.0.2",
"glob-parent": "^5.1.2",
"history": "^5.0.0",
"https-browserify": "^1.0.0",
"install-peerdeps": "^3.0.3",
"jsonwebtoken": "^9.0.0",
"jwa": "^2.0.0",
"jws": "^4.0.0",
"keycloak-js": "^25.0.6",
"moment": "^2.29.4",
"object-hash": "^3.0.0",
"object-path": "0.11.8",
"os-browserify": "^0.3.0",
"postcss-normalize": "^10.0.1",
"process": "^0.11.10",
"prop-types": "^15.7.2",
"query-string": "^7.1.1",
"react": "^16.13.1",
"react-app-polyfill": "^3.0.0",
"react-dom": "^16.13.1",
"react-dropzone": "^11.3.2",
"react-icons": "^4.1.0",
"react-moment": "^1.1.1",
"react-router-dom": "^6.1.1",
"react-scripts": "^5.0.1",
"reactstrap": "^8.9.0",
"regenerator-runtime": "^0.13.7",
"shared-components": "https://github.com/bcgov/react-shared-components/releases/download/v1.3.0/shared-components-1.3.0.tgz",
"stream": "^0.0.2",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"typescript": "^4",
"url": "^0.11.0",
"util": "^0.12.4",
"validator": "^13.7.0"
},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "jest --env='jsdom'",
"eject": "react-app-rewired eject",
"storybook": "start-storybook -p 9009 -s public",
"build-storybook": "build-storybook -s public",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"coverage": "jest --coverage --env='jsdom'",
"precommit": "pretty-quick --staged"
},
"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": {
"@babel/core": "^7.14.3",
"@babel/plugin-syntax-typescript": "^7.12.13",
"@babel/preset-env": "^7.14.4",
"@storybook/addon-actions": "^6.4.19",
"@storybook/addon-links": "^6.4.19",
"@storybook/addon-storyshots": "^6.4.19",
"@storybook/addon-viewport": "^6.4.19",
"@storybook/addons": "^6.4.19",
"@storybook/preset-create-react-app": "^4.1.2",
"@storybook/react": "^6.4.19",
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.7",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "^12.8.1",
"axios-mock-adapter": "^1.19.0",
"babel-loader": "8.2.3",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"enzyme-to-json": "^3.6.1",
"eslint-config-airbnb": "19.0.4",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-airbnb": "^0.0.1-security",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-react": "^7.31.11",
"eslint-plugin-react-hooks": "^4.3.0",
"fetch-mock-jest": "^1.5.1",
"jest-environment-jsdom-sixteen": "^2.0.0",
"jest-sonar-reporter": "^2.0.0",
"moment-timezone": "^0.5.40",
"prettier": ">=2.0.0",
"pretty-quick": "^3.1.0",
"react-app-rewired": "^2.2.1",
"react-test-renderer": "^16.12.0",
"sass": "^1.44.0"
},
"resolutions": {
"immer": "^9.0.6",
"trim": "^0.0.3",
"glob-parent": "^5.1.2",
"loader-utils": "^2.0.4",
"nth-check": "^2.0.1",
"terser": "^5.14.2",
"json5": "^2.2.2",
"minimatch": "^3.0.5",
"webpack": "^5.76.0",
"braces": "^3.0.3",
"webpack-dev-middleware": "^5.3.4",
"browserify-sign": "4.2.2",
"ws": "^8.17.1",
"rollup": "^2.79.2",
"body-parser": "^1.20.3",
"path-to-regexp": "^0.1.10",
"http-proxy-middleware": "^2.0.7",
"cross-spawn": "^7.0.5",
"path-to-regexp": "^0.1.12",
"elliptic": "^6.6.1",
"semver": "^7.5.2"
},
"jest": {
"watchPathIgnorePatterns": [
"<rootDir>/node_modules/"
],
"collectCoverageFrom": [
"src/**/*.js",
"!src/*.js",
"!src/domain/authentication/*.js",
"!**/node_modules/**",
"!**/vendor/**",
"!src/modules/test-data/confirmationPopupTestData.js",
"!src/modules/helpers/handleBackEvent.js",
"!src/components/input/Input.js"
],
"transform": {
"^.+\.[t|j]sx?$": "babel-jest"
},
"setupFilesAfterEnv": [
"<rootDir>/src/setupTests.js",
"./src/setupTests.js"
],
"moduleNameMapper": {
"\.(jpg|jpeg|png|PNG|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|pdf)$": "<rootDir>/src/AssetsTransformer.js",
"\.(css|less|scss)$": "<rootDir>/src/AssetsTransformer.js"
},
"verbose": true,
"testResultsProcessor": "jest-sonar-reporter",
"collectCoverage": true,
"coverageReporters": [
"text",
"lcov"
],
"coverageDirectory": "coverage",
"transformIgnorePatterns": ["node_modules/(?!axios)/"]
},
"jestSonar": {
"reportPath": "coverage",
"reportFile": "test-report.xml",
"indent": 4
}
}