我有一些关于Angular 应用程序的测试.

All tests are passing, when I run ng test and the browser launch here is the result: enter image description here

当我跑ng test --browsers=ChromeHeadless --watch=false米的时候,它仍然是成功的

Chrome Headless 108.0.5359.124 (Mac OS 10.15.7): Executed 136 of 136 SUCCESS (2.832 secs / 1.844 secs)
TOTAL: 136 SUCCESS

但当我查看日志(log)时,我看到一些错误,这似乎不会触发失败.下面是一个例子:

1. If 'p-skeleton' is an Angular component, then verify that it is a part of an @NgModule where this component is declared.

我知道要修复这个错误,我必须在SPEC文件中导入正确的模块.

但这里的问题是,如果我不查看日志(log),我就看不到错误,我认为修复这种错误将是一种很好的做法.因此,第一步将是警告存在错误.

我能想到的最好的方法是让测试因错误而失败,这样运行测试的任何人都被强制修复错误以使所有测试通过.

这是我的karma.conf.js,但我找不到一个配置行来实现这一点.

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example, you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
        random: false,
      },
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true // removes the duplicated traces
    },
    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/project'),
      subdir: '.',
      reporters: [
        {type: 'html'},
        {type: 'text-summary'}
      ]
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true
  });
};

那么,有没有可能让测试因错误而失败呢?为什么它没有在默认情况下失败?

推荐答案

这是由于您在配置configureTestModule呼叫时丢失了declaration.

调用时考虑将PSkeletonComponent添加到declarations属性

TestBed.configureTestingModule({…})

测试并没有失败,因为您正在测试的任何一段Java脚本都在正常工作.只是渲染引擎无法链接到提到的组件,因为您没有将它们添加到测试模块.


如果你使用14角或更高的Angular ,你可以try this out

// enabling the feature globally on the test.ts file
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
  { 
    errorOnUnknownElements: true,
    errorOnUnknownProperties: true
  }
);
// Enabling the feature per test suite
TestBed.configureTestingModule({
  declarations: [AppComponent],
  errorOnUnknownElements: true, 
  errorOnUnknownProperties: false
})

如果您使用的是Angular 13或更低的Angular ,人们所做的就是在他们的测试的beforeEach调用上"修补"console.error方法.

将原来的函数替换为从Karma调用fail.

beforeEach(() => {
  console.error = (...args) => fail(...args)
})

Angular相关问答推荐

PrimeNG侧栏清除primeNG消息.为什么?

Angular :将动态创建的零部件附加为目标元素的子项

我们应该在Angular 从14升级到15的过程中也升级茉莉花和业力相关的依赖吗?

我正在try 将一个带有模块联盟的Angular 8项目expose 到另一个Angular 项目,Angular 是15,这可能吗?如果是这样,有什么建议吗?

react 形式之间的循环关系

如何捕获生命周期方法中抛出的Angular组件错误?

Angular-每2个流式传输多个HTTP调用

Angular *ngIf 表达式中这么多冒号的作用是什么?

如何检测 Angular Universal 预渲染版本?

ngrx 效果等到从初始值以外的其他状态收到响应

NGX-Translate如何不得到404?

如何在 Angular14 中创建带有验证的自定义输入组件?

在ngOninit方法中直接VS初始化属性

Angular 获取视频持续时间并存储在外部变量中

如何有条件地更新 Angular 路由解析器数据?

如果Angular 数据为空,如何设置N/A

Angular 等待对话框未显示

为什么 HttpParams 在 Angular 4.3 中的多行中不起作用

Mat-autocomplete - 如何访问选定的选项?

Angular 2 router.navigate