This is what I found after reading your answers from here and the link provided by @NJCodeMonkey
The error I was getting was this one:
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:
/Users/fgabriel/nx-5/node_modules/flat/index.js:12
export function flatten (target, opts) {
^^^^^^
SyntaxError: Unexpected token 'export'
2 | import { RouterModule } from '@angular/router';
3 | import { NxWelcomeComponent } from './nx-welcome.component';
> 4 | import { flatten } from 'flat'
| ^
5 |
6 | @Component({
7 | imports: [NxWelcomeComponent, RouterModule],
at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (src/app/app.component.ts:4:1)
at Object.<anonymous> (src/app/app.component.spec.ts:2:1)
Now, first of all I would like to say that this started happening when I migrated to Nx latest version, to the moment when I'm writing this comment: 20.4.0
My previous setup was working and it stop working when upgrading Nx (this is just for context)
So I cloned the jest repo and looked into how they handle the transformIgnorePatterns:
return new RegExp(config.transformIgnorePatterns.join('|'));
For anyone wanting to check if the regex added in transformIgnorePatterns is correct, you can use the above code
This helped me to understand that the Regex was not the issue, so something had to be failing when the the transformer (alias transpiler) was trying to transpile this flat/index.js to be used in my tests
So, I took the babel path this time and the following configuration worked:
Files:
nx-workspace/apps/nx-5/babel.config.js
nx-workspace/apps/nx-5/jest.config.ts
Here is my babel.config.js:
module.exports = {
"presets": [
"@babel/preset-env"
]
};
and my jest.config.ts from a recently created Nx workspace for reproduction:
const config = {
displayName: 'nx-5',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
coverageDirectory: '../../coverage/apps/nx-5',
transform: {
'^.+\\.js$': 'babel-jest', <---------
'^.+\\.(ts|html|mjs)$': [
'jest-preset-angular', <---------
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
],
// '^.+\\.(ts|mjs|js|html)$': [
},
transformIgnorePatterns: [
'node_modules/(?!(.*\\.mjs$)|(flat/index\\.js$))', <----
],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
};
export default config;
It was very helpful to me to create a new Nx workspace to work on something smaller where I could reproduce and fix it