Karma + Jasmine -默认的单元测试&它应该创建"Uncaught TypeError: Cann



我有一个非常简单的单元测试——由Angular自动生成的测试。

import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CtaSecondaryComponent } from './cta-secondary.component';
describe('CtaSecondaryComponent', () => {
let component: CtaSecondaryComponent;
let fixture: ComponentFixture<CtaSecondaryComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CtaSecondaryComponent ],
schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CtaSecondaryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

我得到一个错误大约50%的时间(即没有更改测试-有时它失败,有时它通过):

Uncaught TypeError: Cannot read property 'nativeElement' of undefined thrown

据我所知,在这个组件中没有对nativeElement的引用。事实上,在整个项目中都没有对nativeElement的引用。我在网上找不到有这个问题的人。有人能提供一些建议吗?

cta-secondary.component.html

<!-- button -->
<button [ngClass]="{'disabled': disabled}" class="btn btn-secondary">
<span>{{label}}</span>
</button>

cta-secondary.component.ts

import {Component, Input, OnInit, HostListener, ElementRef} from '@angular/core';
@Component({
selector: 'fuse-cta-secondary',
templateUrl: './cta-secondary.component.html',
styleUrls: ['./cta-secondary.component.scss']
})
export class CtaSecondaryComponent implements OnInit {
// label for cta secondary
@Input() label: string;
@Input() disabled: boolean;
constructor() { }
ngOnInit() {
}
}

版本:

角/核心:8.2.2

业力:6.3.4

jasmine-core: 2.99.1

节点:12.18.3

我最终"解决了";这个我自己。从chrome-headless切换到PhantomJS作为karma.config.js中的浏览器似乎已经解决了这个问题。也许"无头铬"缓存了什么东西。无论哪种方式,现在看起来都像预期的那样工作。

也许其他人可以插话,如果有某种缓存重置需要在headless-chrome中完成?