Karma + Jasmine - Default unit test "it should create" failing with error "Uncaught TypeError: Cannot read property 'nativeElement' of undefined"

Karma + Jasmine - Default unit test "it should create" failing with error "Uncaught TypeError: Cannot read property 'nativeElement' of undefined"

我有一个极其简单的单元测试 -- 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() {

  }

}

版本:

angular/core: 8.2.2

业力:6.3.4

茉莉花核心:2.99.1

节点:12.18.3

我最终自己“解决”了这个问题。从 chrome-headless 作为 karma.config.js 中的浏览器切换到 PhantomJS 似乎已经解决了这个问题。也许 chrome-headless 缓存了一些东西。无论哪种方式,这似乎现在都按预期工作。

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