我正在尝试通过
jest-preset-angular
用玩笑来测试 Angular 组件
它可以通过
karma
和 ng test
运行测试,
但通过 jest
运行相同的测试会给出错误
AppComponent › should create the app
Component 'AppComponent' is not resolved:
- templateUrl: ./app.component.html
- styleUrls: ["./app.component.scss"]
Did you run and wait for 'resolveComponentResources()'?
1-生成一个新的角度项目
ng new myproject
2-安装jest依赖项
3-创建jest配置文件
4- 复制并修改 src/app/app.component.spec.ts
为 app.component.spec.jest.ts
通过 karma 运行
app.component.spec.ts
或 npm t
来测试 ng test
文件
然后运行 npm run test:jest
通过 jest测试
app.component.spec.jest.ts
问题详述如下:https://github.com/thymikee/jest-preset-angular/issues/951
就我而言,当我在
jest.config.js
中添加以下内容时,它就起作用了:
module.exports = {
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular'
},
};
他们还建议在
jest.preset.js
中设置这个,但这对我来说没有必要......
const nxPreset = require('@nrwl/jest/preset');
module.exports = {
...nxPreset,
testEnvironment: 'jsdom',
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular'
}
};
就我而言,错误出现在 tsconfig.spec.ts 中。
{
"types": ["jest", "node"] <--- node shouldn't be here
}
删除节点选项设置正确的上下文。
{
"types": ["jest"]
}