Skip to content

Instantly share code, notes, and snippets.

@topnotch48
Last active August 6, 2020 19:24
Show Gist options
  • Save topnotch48/69b8d346c5e08874cb6f9cea981bf93c to your computer and use it in GitHub Desktop.
Save topnotch48/69b8d346c5e08874cb6f9cea981bf93c to your computer and use it in GitHub Desktop.
/**
* 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();
});
};
@davidjpfeiffer
Copy link

davidjpfeiffer commented Aug 12, 2019

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!

@tonybrasunas
Copy link

tonybrasunas commented Aug 6, 2020

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