用笑话测试角度组件给出了resolveComponentResources

问题描述 投票:0回答:2

我正在尝试通过

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()'?

复制

创建此复制品的步骤(请参阅 git 提交了解步骤)

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

angular unit-testing jestjs ts-jest jest-preset-angular
2个回答
4
投票

问题详述如下: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'
   }
};

0
投票

就我而言,错误出现在 tsconfig.spec.ts 中。

{
  "types": ["jest", "node"] <--- node shouldn't be here 
}

删除节点选项设置正确的上下文。

{
  "types": ["jest"]
}
© www.soinside.com 2019 - 2024. All rights reserved.