I created a new project with NG-CLI (beta.15) and modified the app.component.spec
to change the beforeEach
to to a beforeAll
and it caused the tests to fail with the following error:
Failed: Cannot create the component AppComponent as it was not imported into the testing module!
I don't understand what this error means and of course why I would get it in the first place.
Here's the modified spec:
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('App: Ng2CliTest2', () => {
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
});
});
it('should create the app', async(() => {
let fixture = TestBed.createComponent(AppComponent);
let app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
let fixture = TestBed.createComponent(AppComponent);
let app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
let fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('app works!');
}));
});
I then modified the spec to this and the first two tests pass and the third fails with the following message :
Failed: Attempt to use a destroyed view: detectChanges
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
let fixture;
let app;
describe('App: Ng2CliTest2', () => {
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
});
fixture = TestBed.createComponent(AppComponent);
app = fixture.debugElement.componentInstance;
});
it('should create the app', async(() => {
expect(app).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
expect(app.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('app works!');
}));
});
I don't understand why there are any failures.