I've hit a roadblock with running my end-to-end tests in Nest.js using Jest. Every time I attempt to execute my e2e test, an error keeps popping up
Jest encountered an unexpected token
Even though all other test suites ran smoothly, this particular one fails consistently. Despite extensive online research, I haven't been able to resolve the issue.
Below is the jest configuration in my package.json file
"jest": {
"preset": "ts-jest",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": "(?<!.e2e).spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
The following snippet shows the content of my tsconfig.json file
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2019",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true,
"lib": ["es2019"]
}
}
Note: I am utilizing ts-jest instead of Babel
To run my e2e test, I use the following script from my package.json file
"test:e2e": "jest --config ./test/jest-e2e.json",
and below is the configuration contained in my jest-e2e.json file
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "../",
"testEnvironment": "node",
"testRegex": ".e2e.spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
Finally, here's the exact error message displayed in the console:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/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/en/configuration.html
Details:
/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/graphql-moment/lib/factory.coffee:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){{GraphQLScalarType} = require 'graphql'
^
SyntaxError: Unexpected token '='
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/graphql-moment/index.js:2:17)