I ran node --experimental-vm-modules node_modules/.bin/jest --no-cache and got the below exception.
I've tried few hours and google all the possible solutions and seems jest/babel/webpack is a big mess. It could not eval the ES6 syntax which should be basic and common but no available on Jest?
In our project, we use our own module @customPkg a lot and host in our NPM server.
And source code import @customPkg everywhere in our codebase.
Any idea to to make jest run without making changing for the source code?
FAIL Functional tests/unit/Sample.mocha.jsx
● 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.
• 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:
/packages/my_app/node_modules/@customPkg/customPkg/index.js:14
export * from "@customPkg/application";
^^^^^^
SyntaxError: Unexpected token 'export'
Sample.mocha.jsx
import * as React from 'react';
import { MyApp } from '../../../../src/MyApp';
MyApp
/**
* @prettier
*/
14. import * as customPkg from '@customPkg/customPkg';
jest.config.js (the customPkg is in noder_modules)
module.exports = {
testEnvironment: 'jsdom',
'transformIgnorePatterns': [
'node_modules/?!(@customPkg)'
],
moduleNameMapper: {
'^@customPkg': '<rootDir>/node_modules/@customPkg',
},
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
modulePaths: [
'src/',
'node_modules/'
],
};
Package.json
"jestTest": "NODE_OPTIONS=--experimental-vm-modules jest --config=jest.config.js ",
"jest": {
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules",
"bower_components",
"src"
]
},
devDependencies
"@jest/globals": "^27.0.3",
"@testing-library/dom": "^7.31.2",
"@testing-library/jest-dom": "^5.14.1",
"@types/jest": "^26.0.23",
"babel-jest": "^27.0.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"global-jsdom": "^8.1.0",
"jest": "^27.0.4",
"jest-canvas-mock": "^2.3.1",