Using with Babel
If you wish to use Babel
, you need to say jest to transpile such files manually.
-
Install dependencies required by the official Jest documentation for Babel integration.
-
Install
@babel/preset-env
and addbabel.config.js
(or modify existing if needed) with the following content:
babel.config.js
module.exports = function (api) {
api.cache(true);
const presets = ['@babel/preset-env'];
const plugins = [];
return {
presets,
plugins,
};
};
Note: do not use a .babelrc
file otherwise the packages that you specify in the next step will not be picked up. CF Babel documentation and the comment You want to compile node_modules? babel.config.js is for you!
.
- Update Jest configuration (by default TypeScript process untranspiled JS files which is source of the problem):
- TypeScript CJS
- TypeScript ESM
jest.config.ts
import type { Config } from 'jest';
const jestConfig: Config = {
//...
transform: {
'^.+\\.(ts|html)$': 'jest-preset-angular',
'^.+\\.js$': 'babel-jest',
},
};
export default jestConfig;
jest.config.mts
import type { Config } from 'jest';
const jestConfig: Config = {
//...
transform: {
'^.+\\.(ts|html)$': 'jest-preset-angular',
'^.+\\.js$': 'babel-jest',
},
};
export default jestConfig;