Jasmine使用beforeAll / beforeEach编译/创建奇怪的组件

问题描述 投票:0回答:1

我正在为我的应用程序编写一些单元测试,我遇到了一个非常奇怪的情况。我构建了一个角度应用程序,我使用业力和茉莉花进行单元/集成测试。

这是交易:我写了这段代码

describe("Body Container component", () => {
    let component: BodyContainerComponent;
    let fixture: ComponentFixture<BodyContainerComponent>;
    beforeEach(async () => {
        getTestBed().configureTestingModule({
            imports: [AppTestingModule]
        });
        await getTestBed().compileComponents();
        fixture = getTestBed().createComponent(BodyContainerComponent);
        component = fixture.componentInstance;
        component.ngOnInit();
        fixture.detectChanges();
    });

它工作正常,测试编译和正确执行(并通过)。他们确实需要一段时间,我猜是因为每次和每次测试都会再次配置testModule并编译组件。

所以试图通过这种方式改变它:

 beforeAll(async () => {
    getTestBed().resetTestingModule();
    getTestBed().configureTestingModule({
        imports: [AppTestingModule]
    });
    await getTestBed().compileComponents();
});

beforeEach(async () => {
    fixture = getTestBed().createComponent(BodyContainerComponent);
    component = fixture.componentInstance;
    component.ngOnInit();
    fixture.detectChanges();
});

调用相同逻辑/ API的此版本在getTestBed()。createComponent失败,失败:

 Error: Illegal state: Could not load the summary for directive BodyContainerComponent.
    error properties: Object({ ngSyntaxError: true })
        at syntaxError (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:2430:1)
        at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getDirectiveSummary (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:18535:1)
        at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler.getComponentFactory (http://localhost:9876/node_modules/@angular/compiler/fesm5/compiler.js?:25979:1)
        at CompilerImpl.push../node_modules/@angular/platform-browser-dynamic/fesm5/platform-browser-dynamic.js.CompilerImpl.getComponentFactory (http://localhost:9876/node_modules/@angular/platform-browser-dynamic/fesm5/platform-browser-dynamic.js?:162:1)
        at TestingCompilerImpl.push../node_modules/@angular/platform-browser-dynamic/fesm5/testing.js.TestingCompilerImpl.getComponentFactory (http://localhost:9876/node_modules/@angular/platform-browser-dynamic/fesm5/testing.js?:110:1)
        at TestBedViewEngine.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine.createComponent (http://localhost:9876/node_modules/@angular/core/fesm5/testing.js?:1905:1)
        at Object.<anonymous> (http://localhost:9876/src/test/integration/app/components/body-container.integration-spec.ts?:21:32)
        at step (http://localhost:9876/node_modules/tslib/tslib.es6.js?:97:1)
        at Object.next (http://localhost:9876/node_modules/tslib/tslib.es6.js?:78:45)
        at http://localhost:9876/node_modules/tslib/tslib.es6.js?:71:1

我尝试了各种迭代和更改以使其工作,但错误仍然存​​在。经过一些分析后我可以说:1- async / await工作正常,beforeach仅在beforeAll完成之后调用2-每次测试都给出上面的错误,即使是第一个错误(这很奇怪,因为逻辑上第一个不应该改变一个两个版本之间的英寸

我注意到的一件事是第一个版本测试是随机进行的,而第二个版本似乎遵循一个订单。第二个版本的beforeach上的异步不会改变任何东西,我也尝试调用各种重置/销毁方法,但似乎没有贡献。

你知道为什么会这样吗?如果它无法解决就可以了,毕竟在查看整个构建过程时进行了一些小的优化,我更加好奇为什么会发生这种情况。

编辑:在附加分析之后,通过查看testBed实例,我注意到“只有beforeEach”实现了

TestBedViewEngine{_instantiated: false, _compiler: TestingCompilerImpl{_compiler: CompilerImpl{_metadataResolver: ..., _delegate: ..., injector: ...}, _directiveResolver: MockDirectiveResolver{_reflector: ..., _directives: ...}, _pipeResolver: MockPipeResolver{_reflector: ..., _pipes: ...}, _moduleResolver: MockNgModuleResolver{_reflector: ..., _ngModules: ...}, _overrider: MetadataOverrider{_references: ...}}, _moduleRef: null, _moduleFactory: NgModuleFactory_{moduleType: function DynamicTestModule() { ... }, _bootstrapComponents: [], _ngModuleDefFactory: function(_l) { ... }}, _compilerOptions: [], _moduleOverrides: [], _componentOverrides: [], _directiveOverrides: [], _pipeOverrides: [], _providers: [], _declarations: [], _imports: [function AppTestingModule() { ... }], _schemas: [], _activeFixtures: [], _testEnvAotSummaries: function () { ... }, _aotSummaries: [], _templateOverrides: [], _isRoot: true, _rootProviderOverrides: [], platform: PlatformRef{_injector: StaticInjector{parent: ..., source: ..., _records: ...}, _modules: [], _destroyListeners: [], _destroyed: false}, ngModule: function BrowserDynamicTestingModule() { ... }}

而beforeAll有:

TestBedViewEngine{_instantiated: false, _compiler: null, _moduleRef: null, _moduleFactory: null, _compilerOptions: [], _moduleOverrides: [], _componentOverrides: [], _directiveOverrides: [], _pipeOverrides: [], _providers: [], _declarations: [], _imports: [], _schemas: [], _activeFixtures: [], _testEnvAotSummaries: function () { ... }, _aotSummaries: [], _templateOverrides: [], _isRoot: true, _rootProviderOverrides: [], platform: PlatformRef{_injector: StaticInjector{parent: ..., source: ..., _records: ...}, _modules: [], _destroyListeners: [], _destroyed: false}, ngModule: function BrowserDynamicTestingModule() { ... }}

我可以通过此输出看到的最大区别是_compiler,它分别为null和实例化。这个TestBed“快照”是在createComponent调用之前拍摄的

angular jasmine karma-runner
1个回答
0
投票

我实际上发现问题可以在这里找到https://github.com/angular/angular/issues/12409

基本上,总结一下,angular实际上重新定义了之前的每个函数,添加了一些非常自以为是的行为。具体来说,它写在这里:https://github.com/angular/angular/blob/master/packages/core/testing/src/before_each.ts#L24

出于这个原因,在beforeAll中配置的测试模块实际上在beforeEach中重置(在createComponent调用之前)。而只使用beforeEach实际上是有效的,因为模块是在重置调用后配置的。

所以,要么你按照预期使用beforeEach,要么牺牲速度(角度意见是每次测试时提供的服务应该是新鲜的,这对无状态的东西没有意义)或者你可以选择使用2种不同的解决方案:1)重新定义全局beforeEach,将其恢复为默认值,然后放回角度行为:这当然是一个黑客并且非常不稳定并抛出随机错误,因为角度假设模块对于它自己的内部是新鲜的2)根本不使用beforeEach,配置模块在beforeAll中,并且要求所有beforeEach活动到一个自定义函数,你在每个it语句上调用它

© www.soinside.com 2019 - 2024. All rights reserved.