Angular 2 Karma: A platform with a different configuration has been created. Please destroy it first

Viewed 484

I can't call TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()) even one time. And adding TestBed.resetTestEnvironment didn't help.

When I run my code, I get the error:

"A platform with a different configuration has been created. Please destroy it first".

This is my test code:

import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FooterComponent1} from "../../app/page/footer.component";
import {DebugElement} from "@angular/core";
import {By} from "@angular/platform-browser";
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from "@angular/platform-browser-dynamic/testing";

describe('FooterComponent1 test', () => {
    let comp: FooterComponent1;
    let fixture: ComponentFixture<FooterComponent1>;
    let de: DebugElement;
    let el: HTMLElement;

    beforeAll(() => {
        TestBed.resetTestEnvironment();
        TestBed.initTestEnvironment(
            BrowserDynamicTestingModule, platformBrowserDynamicTesting());
    });

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [FooterComponent1],
        }).compileComponents();

        fixture = TestBed.createComponent(FooterComponent1);

        comp = fixture.componentInstance;

        de = fixture.debugElement.query(By.css('h1'));
        el = de.nativeElement;
    });

    it('no title in the DOM until manually call `detectChanges`', () => expect(el.textContent).toEqual(''));

    it('should display original title', () => {
        fixture.detectChanges();
        expect(el.textContent).toContain(comp.title);
    });

    it('should display a different test title', () => {
        comp.title = 'Test Title';
        fixture.detectChanges();
        expect(el.textContent).toContain('Test Title');
    });
});
0 Answers
Related