繁体   English   中英

角度测试被认为是成功的,即使有错误

[英]Angular tests are considered a success, even when there is errors

我对角度应用程序进行了一些测试。

所有测试都通过了,当我运行ng test并且浏览器在此处启动时,结果是: 在此处输入图像描述

当我运行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

但是当我查看日志时,我看到了一些错误,这似乎不会触发失败。 这是一个例子:

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

我知道要修复此错误,我必须在规范文件中导入正确的模块。

但这里的问题是,如果我不查看日志,我将看不到错误,我认为修复此类错误是一个很好的做法。 所以第一步是警告有错误。

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

这是我的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({…})

而且测试不会失败,因为您正在测试的任何 javascript 都可以正常工作。 只是渲染引擎无法链接提到的组件,因为您没有将它们添加到测试模块中。


如果你使用的是 Angular 14 或更高版本,你可以试试这个

// 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 或更低版本,人们一直在做的是在他们的测试的beforeEach调用上“猴子修补” console.error方法。

用因果报应fail的调用替换原始函数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM