从升级到 angular 15 升级到 angular 19 后,运行 test 出现下面的 warning
> node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js --config jest.config.mjs
This preset is DEPRECATED and will be removed in the future.
Please use "createCjsPreset" function instead. See documentation at https://thymikee.github.io/jest-preset-angular/docs/getting-started/presets#createesmpresetoptions
jest.config.mjs 中的配置代码如下
import { pathsToModuleNameMapper } from 'ts-jest';
import config from './tsconfig.json' with { type: 'json' };
globalThis.ngJest = {
skipNgcc: true,
tsconfig: 'tsconfig.spec.json', // this is the project root tsconfig
};
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
const jestConfig = {
preset: 'jest-preset-angular/presets/defaults-esm',
extensionsToTreatAsEsm: ['.ts', '.es6.js'],
globals: {
APPVERSION: 'test',
},
globalSetup: 'jest-preset-angular/global-setup.mjs',
testMatch: ['<rootDir>/src/**/*.spec.ts'],
moduleNameMapper: {
...pathsToModuleNameMapper(config.compilerOptions.paths, { prefix: '<rootDir>' }),
'^rxjs(/.*)?': '<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js',
'date-fns(/.*)?': '<rootDir>/node_modules/date-fns/index.js',
},
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
modulePaths: ['<rootDir>'],
cacheDirectory: '.jest-cache',
};
export default jestConfig;
jest.config.mjs
重命名为 jest.config.ts
"test": "node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js --config jest.config.ts"
"compilerOptions"
中添加"resolveJsonModule": true
import presets from 'jest-preset-angular/presets';
import { type JestConfigWithTsJest, pathsToModuleNameMapper } from 'ts-jest';
import { compilerOptions } from './tsconfig.json';
export default {
...presets.createCjsPreset(),
moduleNameMapper: {
...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>' })
},
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
} satisfies JestConfigWithTsJest;
使用 esm 的 jest.config.ts 配置代码
import ngPreset from 'jest-preset-angular/presets';
import { type JestConfigWithTsJest, pathsToModuleNameMapper } from 'ts-jest';
import tsconfig from './tsconfig.json';
const esmPreset = ngPreset.createEsmPreset();
export default {
...esmPreset,
moduleNameMapper: {
...esmPreset.moduleNameMapper,
...pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: '<rootDir>' }),
rxjs: '<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js',
},
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
transform: {
'^.+\\.(ts|js|html|svg)$': [
'jest-preset-angular',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
useESM: true,
},
],
},
} satisfies JestConfigWithTsJest;