zone.js和Jasmine的时钟之间的冲突 [英] Conflict between zone.js and Jasmine's clock

查看:114
本文介绍了zone.js和Jasmine的时钟之间的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Jasmine测试套件,其中包括香草" Jasmine测试以及一些Angular 2组件的Jasmine测试.由于Angular 2的包含,将加载zone.js.这与茉莉花的时钟产生冲突.例如,以下测试失败,并显示错误Error: Jasmine Clock was unable to install over custom global timer functions. Is the clock already installed?

I am working with a Jasmine testing suite that includes both "vanilla" Jasmine tests along with Jasmine tests for some Angular 2 components. Because of Angular 2's inclusion, zone.js gets loaded. This creates a conflict with Jasmine's clock. For example, the following test fails with the error, Error: Jasmine Clock was unable to install over custom global timer functions. Is the clock already installed?

describe('an async test with zone.js present', function() {
  beforeEach(function() {
    jasmine.clock().install();
  });

  afterEach(function() {
    jasmine.clock().uninstall();
  });

  it('cannot install jasmine\'s mock clock', function() {
    var callback = jasmine.createSpy('setTimeoutCallback')
    setTimeout(callback, 55);
    jasmine.clock().tick(56);
    expect(callback).toHaveBeenCalled();
  });
})

此处对于上面的代码来说很笨拙.

Here is plunker for above code.

与普通"测试分开交付Angular 2测试的时间短,我想知道可以使用哪些选项.例如,是否可以对该区域执行Jasmine Clock的工作?例如,是否可以在断言之前用区域模拟滴答声或刷新所有计划的任务?

Short of delivering the Angular 2 tests separately from the "vanilla" tests, I am wondering what options might be available. For example, is it possible to perform the Jasmine clock's job with the zone? For example, is it possible to simulate the tick with the zone or flush all of the scheduled tasks before the assertion?

推荐答案

对我来说,如果您卸载beforeEach中的时钟,它会起作用.茉莉花不推荐使用它,这有点奇怪,因为对于卸载,使用afterEach更有意义.但是在第一次install调用之前先呼叫uninstall 会为我解决.

For me it works if you uninstall the clock in beforeEach. Its not recommended by jasmine and a bit strange because for uninstall it makes more sense to use afterEach. But calling uninstall before the first install call happens fixed it for me.

这篇关于zone.js和Jasmine的时钟之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆