Angular Karma使用CUSTOM_ELEMENTS_SCHEMA测试超时测试AppComponent

时间:2019-07-19 07:57:31

标签: angular jasmine karma-runner google-chrome-headless

当我在运行所有测试时突然发生超时时,我正在向Angular项目中添加应用程序组件测试:

[launcher]: Launching browsers headless with concurrency unlimited
 21% building 95/96 modules 1 active .../src/css/public.scss19 [launcher]: Starting browser ChromeHeadless
[HeadlessChrome 75.0.3770 (Mac OS X 10.14.5)]: Connected on socket -d4Du6uXE65XhnZkAAAA with id 51932343
[HeadlessChrome 75.0.3770 (Mac OS X 10.14.5)]: Disconnected (0 times), because no message in 30000 ms.

我最近添加了测试app.component.spec.ts:

...
describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
      imports: [RouterTestingModule],
      declarations: [AppComponent],
      providers: [...],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });

});

没有任何测试。过了一会儿,我发现CUSTOM_ELEMENTS_SCHEMA导致了超时。现在,我的app.component.html中有一个自定义元素,这就是为什么要添加架构的原因。删除架构(并从html中删除元素)可以解决此问题。

为什么添加架构会导致karma-headlessChrome超时? 另外,将自定义元素添加到应用程序组件是否是错误的做法?

-更新-

结果是我导入了错误的项目:

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/compiler/src/core';

应该是

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

1 个答案:

答案 0 :(得分:0)

结果是我导入了错误的项目:

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/compiler/src/core';

应该是

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

相关问题