angular - 用 jest 测试 Angular 组件给出了resolveComponentResources

标签 angular unit-testing jestjs ts-jest jest-preset-angular

我正在尝试通过 jest-preset-angular 使用 jest 测试 Angular 组件

它可以通过 karmang 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()'?

reproduction

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

1-生成一个新的 Angular 项目ng new myproject 2-安装jest依赖项 3-创建jest配置文件 4-复制并修改src/app/app.component.spec.tsapp.component.spec.jest.ts

运行复制

通过 karma 运行 npm tng test 测试 app.component.spec.ts 文件 然后运行 ​​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'
   }
};

关于angular - 用 jest 测试 Angular 组件给出了resolveComponentResources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68854700/

相关文章:

unit-testing - NHibernate/Npgsql 在调试 UnitTest 时在 CreateSchema 期间抛出异常

javascript - 如何使用 Karma 测试 AngularJS Controller 中的 $element?

python - 使用 Twisted 和 inlineCallbacks 进行测试

typescript - 如何使用 Jest 在 TypeScript 中模拟导入的函数?

javascript - 使用 Typescript 在 Jest 中模拟类时引发 "Missing semicolon"错误

css - 即使未在任何地方声明,响应式元也采用固定宽度,刷新后显示。一次加载工作正常

当从 api 获取数据时,Angular 2+ 用 ngFor 填充 html 表

angular - 在 Angular 4 中编辑表格数据

javascript - 是否可以编写一段在产品版本中被省略的代码

reactjs - 测试 child 的 child 最好的方法是什么?