Angular单元测试没有通过 - 异常的phantomjs错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Angular单元测试没有通过 - 异常的phantomjs错误相关的知识,希望对你有一定的参考价值。
我运行ng test
来运行硬编码传递的3个单元测试,例如:
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
declarations: [
AppComponent
]
}).compileComponents();
}));
it('should create the app', () => {
// const fixture = TestBed.createComponent(AppComponent);
// const app = fixture.debugElement.componentInstance;
// expect(app).toBeTruthy();
expect(true).toBe(true);
});
但是,我得到了这个奇怪的错误:
12 04 2019 16:38:11.231:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
12 04 2019 16:38:11.256:INFO [launcher]: Starting browser PhantomJS 12 04 2019 16:38:19.761:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket nXFNY5PRUCvs5FtgAAAA with id 70621517
PhantomJS 2.1.1 (Windows 8 0.0.0) AppComponent should create the app FAILED
parse
then
_parseTemplate
_compileTemplate
forEach
_compileComponents
_compileModuleAndAllComponents
compileModuleAndAllComponentsAsync
compileModuleAndAllComponentsAsync
compileModuleAndAllComponentsAsync
compileComponents
compileComponents
invoke
onInvoke
onInvoke
invoke
runGuarded
runInTestZone
invoke
onInvoke
invoke
run
runInTestZone
execute
execute
invokeTask
runTask
drainMicroTaskQueue
parse
then
_parseTemplate
_compileTemplate
forEach
_compileComponents
_compileModuleAndAllComponents
compileModuleAndAllComponentsSync
compileModuleAndAllComponentsSync
compileModuleAndAllComponentsSync
_initIfNeeded
createComponent
createComponent
invoke
onInvoke
invoke
run
runInTestZone
这就是所有的错误堆栈说。
答案
我在我的import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
文件中导入了app.component.ts
并在那里使用它,所以我还必须将它导入我的.spec.ts
文件并将其声明为模式:
import { TestBed, async } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
declarations: [
AppComponent
],
schemas: [
CUSTOM_ELEMENTS_SCHEMA // needed to add this
]
}).compileComponents();
}));
以上是关于Angular单元测试没有通过 - 异常的phantomjs错误的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法批量处理 Karma - Angular 单元测试?
Angular/Karma 单元测试错误“1 个计时器仍在队列中”
Angular 单元测试 NullInjectorError:没有 HttpClient 的提供者!错误