由于Zone.js TypeError导致Karma测试失败:无法读取未定义的属性'takeUntil'

时间:2018-01-03 00:23:03

标签: angular ionic2 rxjs zone.js

运行此测试的体验( TypeError:无法读取未定义的属性'takeUntil')。这与Zone.js有关。

组件在应用程序中表现良好,只是测试失败了吗?

让您满意的更多细节

这是我试图测试的组件

// Import: Angular
import {Component, EventEmitter, Injectable, Input, Output} from 
'@angular/core';

// Component
@Component({
  selector: 'currency-input',
  templateUrl: './currency-input.html'
})
@Injectable()
export class CurrencyInput {

  @Input() public currencyValue: string;
  @Input() public valid: boolean;
  @Input() public errorMessage: string;
  @Output() public onChange: any = new EventEmitter();

  public currencyInputChange(e: any): void {
    this.onChange.emit(e);
  }
}

这是测试:

describe('CurrencyInput', () => {
let fixture: ComponentFixture<CurrencyInput> = null;
let instance: any = null;

beforeEach(async(() => 
   TestUtils.beforeEachCompiler([CurrencyInput]).then(compiled => {
   fixture = compiled.fixture;
   instance = compiled.instance;
   instance.currencyValue = 'some_value';
   fixture.detectChanges();
})));

  it('defined', () => {
    expect(fixture).not.toBeNull();
    expect(instance).not.toBeNull();
  });
});

堆栈追踪:

TypeError: Cannot read property 'takeUntil' of undefined
    at TextInput.webpackJsonp.../../../../ionic-angular/components/input/input.js.TextInput._enableHideCaretOnScroll Users/larry/Projects/turbo-ionic/node_modules/ionic-angular/components/input/input.js:440:1)
    at new TextInput Users/larry/Projects/turbo-ionic/node_modules/ionic-angular/components/input/input.js:155:1)
    at createClass Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:12173:1)
    at createDirectiveInstance Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:12010:22)
    at createViewNodes Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:13448:38)
    at callViewAction Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:13880:1)
    at execComponentViewsAction Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:13789:1)
    at createViewNodes Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:13476:1)
    at createRootView Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:13338:1)
    at callWithDebugContext Users/larry/Projects/turbo-ionic/node_modules/@angular/core/esm5/core.js:14739:26)
Error: Uncaught (in promise): TypeError: Cannot read property 'takeUntil' of undefined
    at resolvePromise Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:824:1)
    at resolvePromise Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:795:1)
    at http://localhost:9876/_karma_webpack_/webpack:/Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:873:1
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:425:1)
    at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvokeTask Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/proxy.js:103:1)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:424:1)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:192:1)
    at drainMicroTaskQueue Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:602:1)
    at <anonymous>
Expected null not to be null.
    at UserContext.<anonymous> Users/larry/Projects/turbo-ionic/src/components/currency-input/currency-input.spec.ts:23:25)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:392:1)
    at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/proxy.js:79:1)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:391:1)
Expected null not to be null.
    at UserContext.<anonymous> Users/larry/Projects/turbo-ionic/src/components/currency-input/currency-input.spec.ts:24:26)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:392:1)
    at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/proxy.js:79:1)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/larry/Projects/turbo-ionic/node_modules/zone.js/dist/zone.js:391:1)

1 个答案:

答案 0 :(得分:0)

尝试从beforeEach中删除fixture.detectChanges() - 这似乎是一个返回此错误的常见问题。