【问题标题】:Angular 6 update specs [object ErrorEvent] thrownAngular 6 更新规范 [object ErrorEvent] 抛出
【发布时间】:2018-11-02 15:11:47
【问题描述】:

我正在将 Angular 应用程序从版本 4 升级到版本 6。使用 ng serve 一切正常运行。现在的问题是我们的一堆规范文件以[object ErrorEvent] thrown 失败。我以前见过这种情况,通常可以使用--sourceMap=false 标志运行测试以查看更详细的报告,但现在我仍然得到这个模棱两可的错误。我已经检查过没有任何组件有任何未定义的输入。经过一些研究,我发现有些人在使用 jasmine-core 3.0.0 时遇到了问题,并在他们回到 2.99.0 之前收到了这个错误,但这对我也没有任何帮助。

关于这一切的最奇怪的部分是,我可以 xdescribe 一个规范(或所有规范)因此错误而失败,然后其他未失败的规范失败。

这是 package.json

{
  "name": "enterprise-frontend",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test --browsers=Chrome --code-coverage",
    "lint": "ng lint --type-check",
    "e2e": "ng e2e",
    "compodoc": "./node_modules/.bin/compodoc -p tsconfig.json -s"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.2",
    "@angular/cdk": "^6.0.2",
    "@angular/common": "^6.0.2",
    "@angular/compiler": "^6.0.2",
    "@angular/core": "^6.0.2",
    "@angular/forms": "^6.0.2",
    "@angular/http": "^6.0.2",
    "@angular/material": "^6.0.2",
    "@angular/platform-browser": "^6.0.2",
    "@angular/platform-browser-dynamic": "^6.0.2",
    "@angular/router": "^6.0.2",
    "@auth0/angular-jwt": "^2.0.0",
    "@ngrx/effects": "^6.0.1",
    "@ngrx/router-store": "^6.0.1",
    "@ngrx/store": "^6.0.1",
    "@ngrx/store-devtools": "^6.0.1",
    "auth0-js": "^9.5.1",
    "chart.js": "^2.7.2",
    "core-js": "^2.5.6",
    "lodash": "^4.17.10",
    "ng2-charts": "^1.6.0",
    "ng2-dnd": "^5.0.2",
    "ngx-clipboard": "^11.1.0",
    "rxjs": "^6.1.0",
    "rxjs-compat": "^6.1.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.3",
    "@angular/cli": "^6.0.3",
    "@angular/compiler-cli": "^6.0.2",
    "@angular/language-service": "^6.0.2",
    "@compodoc/compodoc": "^1.1.3",
    "@types/jasmine": "^2.8.7",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^6.0.111",
    "codelyzer": "^4.3.0",
    "file-saver": "^1.3.8",
    "jasmine-core": "^2.99.1",
    "karma": "~2.0.2",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.3",
    "karma-jasmine": "^1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
        "protractor": "~5.1.2",
        "ts-node": "~3.2.0",
        "tslint": "^5.10.0",
        "typescript": "^2.7.2"
      }
    }

这是目前失败的规范之一(其中大多数都比这复杂得多,但这让我认为这不是测试本身的问题):

import { HttpClientModule } from '@angular/common/http';
import { MaterialModule } from '../../modules/material.module';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { NumberPerPageComponent } from './number-per-page.component';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

describe('NumberPerPageComponent', () => {
  let component: NumberPerPageComponent;
  let fixture: ComponentFixture<NumberPerPageComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ NumberPerPageComponent ],
      imports: [
        HttpClientModule,
        MaterialModule,
        NoopAnimationsModule
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(NumberPerPageComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

这是组件本身:

import { Component } from '@angular/core';

@Component({
  selector: 'app-number-per-page',
  templateUrl: './number-per-page.component.html',
  styleUrls: ['./number-per-page.component.scss']
})
export class NumberPerPageComponent {

  resultNumber = 10;

  constructor() { }

}

【问题讨论】:

    标签: angular unit-testing jasmine karma-jasmine angular6


    【解决方案1】:

    我遇到的问题是将HttpClientModule 导入我的规范而不是HttpClientTestingModule

    测试试图进行 api 调用,但在收到来自我们后端的 401 响应时失败。

    【讨论】:

    • 就我而言,我还不得不在很多地方使用RouterTestingModuleApolloTestingModule
    【解决方案2】:

    这种错误通常直接扔到浏览器中:您在控制台中看到此消息,但真正的错误是浏览器的开发工具。你能快速检查一下,看看你的错误是否弹出?

    否则,它会告诉您哪个测试失败:尝试异步运行此测试,或使用回调中的参数:

    it('XXX', done => {
      // do your things then
      done();
    });
    

    【讨论】:

    • +1 用于提及打开开发者工具。从来没有想过为单元测试这样做。打开开发工具后,出现了错误。
    • 没问题,有几个人知道!
    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2019-05-05
    • 1970-01-01
    相关资源
    最近更新 更多