UNPKG

@apteco/ngth

Version:

ng-bullet successor to help speed up angular testing

110 lines (104 loc) 3.31 kB
import { __awaiter } from 'tslib'; import { getTestBed, TestBed } from '@angular/core/testing'; const resetProviders = (moduleDef) => { if (!moduleDef) { return; } if (!moduleDef.providers) { return; } moduleDef.providers.forEach((provider) => { if (!provider || !provider.useValue) { return; } const instance = provider.useValue; if (instance && instance.resetProvider) { instance.resetProvider(); } Object.keys(instance).forEach((property) => { if (instance[property] && instance[property].calls && instance[property].calls.reset) { instance[property].calls.reset(); } }); }); }; const cleanUpStyles = () => { const head = document.getElementsByTagName('head')[0]; const styles = head.getElementsByTagName('style'); for (let i = 0; i < styles.length; i++) { head.removeChild(styles[i]); } }; const configureSuite = (configure) => { const testBed = getTestBed(); const originalResetTestModule = TestBed.resetTestingModule; const originalConfigureTestingModule = TestBed.configureTestingModule; let moduleMetadata; beforeAll(() => { TestBed.resetTestingModule(); TestBed.resetTestingModule = () => TestBed; TestBed.configureTestingModule = (moduleDef) => { moduleMetadata = moduleDef; return originalConfigureTestingModule(moduleMetadata); }; }); if (configure) { beforeAll((done) => (() => __awaiter(void 0, void 0, void 0, function* () { configure(); yield TestBed.compileComponents(); }))() .then(done) .catch(done.fail)); } afterEach(() => { testBed['_activeFixtures'].forEach((fixture) => { fixture.destroy(); }); testBed['_instantiated'] = false; testBed['_testModuleRef'] = null; resetProviders(moduleMetadata); }); afterAll(() => { cleanUpStyles(); TestBed.resetTestingModule = originalResetTestModule; TestBed.resetTestingModule(); TestBed.configureTestingModule = originalConfigureTestingModule; }); }; class ComponentFixtureWrapper { constructor(fixture) { this.fixture = fixture; } get component() { return this.fixture.componentInstance; } get debugElement() { return this.fixture.debugElement; } get element() { return this.debugElement.nativeElement; } detectChanges() { this.fixture.detectChanges(); } } const createComponent = (component) => { return new ComponentFixtureWrapper(TestBed.createComponent(component)); }; const createStableComponent = (component) => __awaiter(void 0, void 0, void 0, function* () { const fixtureWrapper = createComponent(component); fixtureWrapper.detectChanges(); yield fixtureWrapper.fixture.whenStable(); fixtureWrapper.detectChanges(); return fixtureWrapper; }); /* * Public API Surface of ngth */ /** * Generated bundle index. Do not edit. */ export { ComponentFixtureWrapper, configureSuite, createComponent, createStableComponent }; //# sourceMappingURL=apteco-ngth.mjs.map