Last active
August 6, 2020 19:24
-
-
Save topnotch48/69b8d346c5e08874cb6f9cea981bf93c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Reconfigures current test suit to prevent angular components compilation after every test run. | |
* Forces angular test bed to re-create zone and all injectable services by directly | |
* changing _instantiated to false after every test run. | |
* Cleanups all the changes and reverts test bed configuration after suite is finished. | |
*/ | |
export const configureTestSuite = () => { | |
const testBedApi: any = getTestBed(); | |
const originReset = TestBed.resetTestingModule; | |
beforeAll(() => { | |
TestBed.resetTestingModule(); | |
TestBed.resetTestingModule = () => TestBed; | |
}); | |
afterEach(() => { | |
testBedApi._activeFixtures.forEach((fixture: ComponentFixture<any>) => fixture.destroy()); | |
testBedApi._instantiated = false; | |
}); | |
afterAll(() => { | |
TestBed.resetTestingModule = originReset; | |
TestBed.resetTestingModule(); | |
}); | |
}; |
Agreed, many thanks for this 👍
I would just add a line at the top to add imports if you're going to import this file into each spec.ts
file:
import { ComponentFixture, getTestBed, TestBed } from '@angular/core/testing';
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is very useful if you are trying to speed up the execution of tests for components that take a long time to compile.
Thank you for sharing!