首页 新闻 会员 周边 捐助

升级到 angular 19 后运行测试出现警告 "This preset is DEPRECATED"

0
[已解决问题] 浏览: 29次 解决于 2024-12-24 08:26

从升级到 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;
dudu的主页 dudu | 高人七级 | 园豆:27830
提问于:2024-12-24 07:19
< > 人人可用的开源BI工具
分享
最佳答案
0
  • jest.config.mjs 重命名为 jest.config.ts
  • package.json 在 test 改为使用 jest.config.ts
"test": "node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js --config jest.config.ts"
  • 在 tsconfig.json 的 "compilerOptions" 中添加
"resolveJsonModule": true
  • jest.config.ts 改为下面的配置代码
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;
dudu | 高人七级 |园豆:27830 | 2024-12-24 08:25

使用 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;

来自 example-app-v19

dudu | 园豆:27830 (高人七级) | 2024-12-24 10:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册
Top