0%

jest-troubleshooting

1. TypeError: configSet.processWithEsbuild is not a function

Solution: Update jest.config.js to use jest-angular-preset instead of ts-preset.

1
2
3
4
5
module.exports = {
transform: {
'^.+\\.(ts|js|html)$': 'ts-jest', // <-- update this line
},
};
1
2
3
4
5
module.exports = {
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular', // <-- update this line
},
};

2. SyntaxError: xxx.spec.ts: Missing semicolon. (26:42)

Look into the error message, the error occurs on this line.

1
const compiled = fixture.nativeElement as HTMLElement;

The reason is because as, it’s a keyword in TypeScript, but it’s not a keyword in JavaScript. So the root cause is Jest cannot understand the TypeScript syntax.
We need a preset to help Jest to understand TypeScript syntax. The ts-jest is the most popular one.

  • If you project is a pure TypeScript project, see here on how to config ts-jest.
  • If you project is an Angular project, Please use jest-preset-angular, see here for details.

3. jest: failed to cache transform results in: xxx/jest/jest-transform-cache.map, Failure message: ENOSPC: no space left on device, write

This is because Jest is trying to transform itself, so add the following to your jest.config.js file will resolve this issue. see here for details.

1
2
3
4
transformIgnorePatterns: [
'<rootDir>/node_modules/@babel',
'<rootDir>/node_modules/@jest',
],