Skip to content

Instantly share code, notes, and snippets.

@willgm
Created September 4, 2017 21:35
Show Gist options
  • Save willgm/54cd36ebc7165ae28d66a14befb8875f to your computer and use it in GitHub Desktop.
Save willgm/54cd36ebc7165ae28d66a14befb8875f to your computer and use it in GitHub Desktop.
Speed up your component's test suite by caching the test module
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { setUpCachedTestBed } from '../test-utils';
describe('AppComponent', () => {
setUpCachedTestBed({
declarations: [ AppComponent ],
});
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
});
import { TestBed, TestModuleMetadata } from '@angular/core/testing';
const resetTestingModule = TestBed.resetTestingModule;
export function setUpCachedTestBed(moduleDef: TestModuleMetadata) {
beforeAll(done => {
TestBed.resetTestingModule();
TestBed.resetTestingModule = () => TestBed;
TestBed.configureTestingModule(moduleDef);
TestBed.compileComponents()
.then(done).catch(done.fail);
});
afterAll(() => TestBed.resetTestingModule = resetTestingModule);
};
@willgm
Copy link
Author

willgm commented Sep 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment