从库中测试Angular组件失败,并出现以下错误:TypeError:无法读取null的'getComponentFromError'属性

时间:2019-06-28 13:42:27

标签: angular unit-testing jestjs

我使用应用程序和ui组件库创建了一个nrwl / nx项目。我还使用玩笑作为默认测试工具。

我的应用程序中的测试运行良好,但是当我尝试在ui组件库中测试组件时,它总是以以下错误结束:

 FAIL  libs/ui-components/src/lib/white-label-header/white-label-header.component.spec.ts
  ● WhiteLabelHeaderComponent › should create



       7 |   let fixture: ComponentFixture<WhiteLabelHeaderComponent>;
       8 |
    >  9 |   beforeEach(async(() => {
         |   ^
      10 |     TestBed.resetTestEnvironment();
      11 |     TestBed.initTestEnvironment(
      12 |       BrowserDynamicTestingModule,

      at Env.beforeEach (../../node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:41:24)
      at Suite.Object.<anonymous>.describe (src/lib/white-label-header/white-label-header.component.spec.ts:9:3)
      at Object.<anonymous> (src/lib/white-label-header/white-label-header.component.spec.ts:5:1)

  ● WhiteLabelHeaderComponent › should create

    TypeError: Cannot read property 'getComponentFromError' of null

      at TestBedViewEngine._initIfNeeded (../../../../packages/core/testing/src/test_bed.ts:378:46)
      at TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:570:10)
      at Function.TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:232:36)
      at Object.beforeEach (src/lib/white-label-header/white-label-header.component.spec.ts:22:23)

  ● WhiteLabelHeaderComponent › should create

    expect(received).toBeTruthy()

    Received: undefined

      26 |
      27 |   it('should create', () => {
    > 28 |     expect(component).toBeTruthy();
         |                       ^
      29 |   });
      30 | });
      31 |

      at Object.it (src/lib/white-label-header/white-label-header.component.spec.ts:28:23)

我试图将其添加到我的beforeEach中:

    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
    );

但没有成功。

这是我的规格文件: white-label-header.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { WhiteLabelHeaderComponent } from './white-label-header.component';

describe('WhiteLabelHeaderComponent', () => {
  let component: WhiteLabelHeaderComponent;
  let fixture: ComponentFixture<WhiteLabelHeaderComponent>;

  beforeEach(async(() => {
    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
    );

    TestBed.configureTestingModule({
      declarations: [WhiteLabelHeaderComponent],
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(WhiteLabelHeaderComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

所有其他相关文件只是空的Angular组件。 我只想编译测试,就这样。

有人知道我在做什么错吗?

0 个答案:

没有答案