lundi 19 novembre 2018

Angular unit test: Error: AsyncTestZoneSpec is needed for the async() test helper but could not be found

I have an issue running unit tests (.spec.ts) in Angular. After I launch the test, I get:

Error: AsyncTestZoneSpec is needed for the async() test helper but could not be found. Please make sure that your environment includes zone.js/dist/async-test.js
        at runInTestZone (./node_modules/@angular/core/fesm5/testing.js?:94:15)
        at UserContext.eval (./node_modules/@angular/core/fesm5/testing.js?:69:13)
    TypeError: Cannot read property 'getComponentFromError' of null
        at TestBed._initIfNeeded (./node_modules/@angular/core/fesm5/testing.js?:984:52)
        at TestBed.createComponent (./node_modules/@angular/core/fesm5/testing.js?:1177:14)
        at Function.TestBed.createComponent (./node_modules/@angular/core/fesm5/testing.js?:870:29)
        at UserContext.eval (./src/app/account-settings/account-settings.component.spec.ts?:19:81)
    Expected undefined to be truthy.
        at UserContext.eval (./src/app/account-settings/account-settings.component.spec.ts?:24:27)
    TypeError: Cannot read property 'fixture' of undefined
        at UserContext.eval (./src/app/account-settings/account-settings.component.spec.ts?:25:22)
Chrome 67.0.3396 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.006 secs)
Chrome 67.0.3396 (Linux 0.0.0) AccountSettingsComponent should create, and should have three inputs and one button FAILED
    Error: AsyncTestZoneSpec is needed for the async() test helper but could not be found. Please make sure that your environment includes zone.js/dist/async-test.js
        at runInTestZone (./node_modules/@angular/core/fesm5/testing.js?:94:15)
        at UserContext.eval (./node_modules/@angular/core/fesm5/testing.js?:69:13)
    TypeError: Cannot read property 'getComponentFromError' of null
        at TestBed._initIfNeeded (./node_modules/@angular/core/fesm5/testing.js?:984:52)
        at TestBed.createComponent (./node_modules/@angular/core/fesm5/testing.js?:1177:14)
        at Function.TestBed.createComponent (./node_modules/@angular/core/fesm5/testing.js?:870:29)
        at UserContext.eval (./src/app/account-settings/account-settings.component.spec.ts?:19:81)
    Expected undefined to be truthy.
        at UserContext.eval (./src/app/account-settings/account-settings.component.spec.ts?:24:27)
    TypeError: Cannot read property 'fixture' of undefined
Chrome 67.0.3396 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.08 secs / 0.006 secs)

My test looks like:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AccountSettingsComponent } from './account-settings.component';
import { AppComponent } from '../app.component';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ AppComponent, AccountSettingsComponent]

    })
    .compileComponents();
  }));

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

  it('should create, and should have three inputs and one button', () => {
    expect(component).toBeTruthy();
    expect(this.fixture.debugElement.nativeElement.querySelector("input").length).toEqual(3)
    expect(this.fixture.debugElement.nativeElement.querySelector("button").length).toEqual(1)
  });
});

The test.js is default one, as generated by the cli. I did not encounter this issue in Angular 5 (not the same app).

I am running the test in this way: ng test --main src/app/account-settings/account-settings.component.spec.ts --watch=false

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire