茉莉单元测试 "在 afterAll 中抛出了一个错误"

3

我只是在单元测试组件是否已创建。以下是我的规范文件

import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { MatTableModule } from '@angular/material';
import { HttpClientModule, HttpXhrBackend } from '@angular/common/http';
import { MockBackend } from '@angular/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

import { ReleaseListComponent } from './release-list.component';
import { ReleaseService } from '../release.service';

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

  beforeEach(() => {
    TestBed.configureTestingModule({
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      imports: [RouterTestingModule, MatTableModule, HttpClientModule ],
      declarations: [ReleaseListComponent],
      providers: [ReleaseService]
    })
      .compileComponents();
  });

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

  it('should create', () => {
    expect(component).toBeDefined();
  });

我的组件

import { Component, OnInit, ViewChild } from '@angular/core';
import { ReleaseService } from '../release.service';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';

@Component({
  selector: 'bw-release-list',
  templateUrl: './release-list.component.html'
})
export class ReleaseListComponent implements OnInit {
  title = 'Release notes';
  displayedColumns = ['title'];
  dataSource;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor( private releaseNotes: ReleaseService ) { }

  ngOnInit() {
    this.releaseNotes.getReleaseNotes()
      .subscribe(data => {
        this.dataSource = new MatTableDataSource(data.results);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
      });
  }
  applyFilter(filterValue: string) {
    filterValue = filterValue.trim();
    filterValue = filterValue.toLowerCase();
    this.dataSource.filter = filterValue;
  }
}

我一直在调试,但是出现了An error was thrown in afterAll的错误,已经尝试了很多方法,包括在asyncsync中使用beforeEach,也将所有依赖文件导入到规范文件中,但仍然出现相同的错误。

请建议我如何解决这个问题。

1个回答

2
您似乎导入了HttpClientTestingModule,但是正在使用:
imports: [RouterTestingModule, HttpClientModule ],。这应该是HttpClientTestingModule
然后,在浏览器中启动您的测试并打开console,您可以在浏览器控制台中看到一些额外的问题,这些问题在仅在命令提示符中运行时不会显示出来。
例如,我遇到了一些源映射问题,我不得不使用ng test --source-map=false来禁用它进行测试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接