sinsunsan-ng-bullet-workspace
Version:
Angular library which drastically improves execution time of your component's unit tests. This fork fix a problem in the original ng-bullet #48. Error messages in the test execution
89 lines (84 loc) • 3.41 kB
JavaScript
import { __awaiter } from 'tslib';
import { getTestBed, TestBed } from '@angular/core/testing';
/**
* Reconfigures current test suit to prevent angular components re-compilation after every test run.
* Forces angular test bed to re-create zone and all injectable services by directly
* setting _instantiated variable to false after every test run.
* Cleanups all the changes and reverts test bed configuration after suite has finished.
*
* @param configureAction an optional delegate which can be used to configure test bed for the current test suite
* directly in the configureTestSuite call (you don't need extra BeforeAll in this case)
*/
const configureTestSuite = (configureAction) => {
const testBedApi = getTestBed();
const originReset = TestBed.resetTestingModule;
beforeAll(() => {
TestBed.resetTestingModule();
TestBed.resetTestingModule = () => TestBed;
});
// https://github.com/topnotch48/ng-bullet-workspace/issues/38
if (configureAction) {
beforeAll((done) => {
(function () {
return __awaiter(this, void 0, void 0, function* () {
configureAction();
yield TestBed.compileComponents();
});
}().then(done).catch(done.fail));
});
}
afterEach(() => {
testBedApi._activeFixtures.forEach((fixture) => fixture.destroy());
// reset ViewEngine TestBed
testBedApi._instantiated = false;
// reset Ivy TestBed
testBedApi._testModuleRef = null;
});
afterAll(() => {
TestBed.resetTestingModule = originReset;
TestBed.resetTestingModule();
});
};
/**
* A wrapper class around ComponentFixture, which provides useful accessros:
* component - to access component instance of current the fixture
* element - to access underlying native element of the current component
* detectChanges - to run change detections using current fixture
* resolve - to resolve a type using current fixture's injector
*/
class TestCtx {
constructor(fixture) {
this.fixture = fixture;
}
get component() { return this.fixture.componentInstance; }
get element() { return this.fixture.debugElement.nativeElement; }
detectChanges() { this.fixture.detectChanges(); }
resolve(component) { return this.fixture.debugElement.injector.get(component); }
}
/**
* Creates TestCtx instance for the Angular Component which is not initialized yet (no ngOnInit called)
* Use case: you can override Component's providers before hooks are called.
*
* @param component - type of component to create instance of
* **/
const createTestContext = (component) => {
const fixture = TestBed.createComponent(component);
const testCtx = new TestCtx(fixture);
return testCtx;
};
/**Same as @function createTestContext, but waits till fixture becomes stable */
const createStableTestContext = (component) => __awaiter(void 0, void 0, void 0, function* () {
const testCtx = createTestContext(component);
testCtx.detectChanges();
yield testCtx.fixture.whenStable();
testCtx.detectChanges();
return testCtx;
});
/*
* Public API Surface of easy-testing library
*/
/**
* Generated bundle index. Do not edit.
*/
export { TestCtx, configureTestSuite, createStableTestContext, createTestContext };
//# sourceMappingURL=sinsunsan-ng-bullet-workspace.mjs.map