angular - 从 Jasmine/Karma 迁移到 Jest 是否可行?
问题描述
我有一个包含 3300 多个测试的 Angular 7 应用程序。而且它们需要花费太多时间来运行,并且需要花费太多精力来维护。我听说过很多关于 Jest 的好消息,但不确定将所有这些测试从 Jasmine 迁移到 Jest 是否真的可行。
解决方案
您可以通过升级到 jest 来轻松回答这个问题。jest-preset-angular 团队在 Github 上有一个很棒的演练。
在您的项目根目录中添加一些文件:
setup.jest.ts
和:
import 'jest-preset-angular/setup-jest';
jest.config.js
和:
// Angular Ivy (9+ only for the next line)
require('jest-preset-angular/ngcc-jest-processor');
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
};
tsconfig.spec.json
和:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
修改tsconfig.json
:
"lib": [ "es2018", "dom" ],
"esModuleInterop": true
修改tsconfig.spec.json
:
"types": [ "jest" ] // Replace "jasmine"
调整package.json
"test": "jest -c ./jest.config.js ./src
添加所需的包:
npm install jest jest-preset-angular @types/jest @angular-builders/jest
安装后迁移
然后,您可以自动将您的测试(在某种程度上)从 Jasmine 转换为 Jest:
npx jest-codemods
现在您可以针对您的特定项目回答您的问题
跑:
npm run test
查看您需要手动修复多少失败的测试才能完成迁移。并决定这是否在您的时间表内。
故障排除
错误:无法设置基本提供程序,因为它已被调用
查找tests.ts
文件并将其删除。这是您在 Jest 中不需要的业力文件。
推荐阅读
- xcode - Spritekit FPS 掉落问题
- matlab - 如何在 Matlab 中使用 R-Tree 进行 knnsearch
- javascript - 根据选择框隐藏部分
- java - 获取 pdf 时播放资产交付不起作用
- docker - Kubernetes:来自服务器的错误(未找到):未找到 deployments.apps “kube-verify”
- jupyter-notebook - Jupyter 密码未经过哈希处理
- shell - 获取日期范围内所有行的列表
- apache - 无法在 Mac 上访问 localhost 文件
- python - 如何多次重复一个函数并在python中对输出求和?
- r - R函数将列类型调整为日期格式