javascript - 无限 Jasmine 超时

标签 javascript testing jasmine protractor

这基本上是 Remove timeout for single jasmine spec 的后续行动github问题。

问题:

是否可以让单个测试永不超时?

问题:

可以通过 DEFAULT_TIMEOUT_INTERVAL 全局设置超时值或者对于每个用 beforeEach/afterEach 或在单个 it() block 上描述的:

it('Has a custom timeout', function() {
  expect(true).toBeTruthy();
}, value in msec)

我感兴趣的是让单个规范永不过时。我尝试遵循上述 github 问题中提出的建议并使用 Infinity:

it('Has a custom timeout', function() {
  expect(true).toBeTruthy();
}, Infinity)

但是,在测试进入 it() block 后,我立即收到以下错误:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

我想我不能使用 Infinity 作为超时值,或者我做错了什么。


作为解决方法,我可以改用硬编码的大数字,但我想避免这种情况。

最佳答案

Jasmine 在内部使用 setTimeout 来等待规范在定义的时间段内完成。

根据此问答 - Why does setTimeout() "break" for large millisecond delay values? :

setTimeout using a 32 bit int to store the delay

...

Timeout values too big to fit into a signed 32-bit integer may cause overflow in FF, Safari, and Chrome, resulting in the timeout being scheduled immediately. It makes more sense simply not to schedule these timeouts, since 24.8 days is beyond a reasonable expectation for the browser to stay open.

只要 Infinity 大于任何其他数字,就会发生溢出。

在这种情况下,最大安全整数是 231-1 = 2147483647。这个值是有限的,所以测试实际上不会无限长地运行,但如前所述,我认为 24​​.8 天很长足够。

你可以定义一个常量来存储这个值:

jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;

var MAX_SAFE_TIMEOUT = Math.pow(2, 31) - 1;

describe('suite', function () {

  it('should work infinitely long', function (done) {

    setTimeout(function () {
      expect(true).toBe(true);
      done();
    }, 3000)

  }, MAX_SAFE_TIMEOUT);

});

See working sample here

关于javascript - 无限 Jasmine 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32336575/

相关文章:

jquery - AngularJs/在 jasmine 测试中使用 jquery 选择器不起作用

javascript - 当我在Javascript中通过键从对象中获取值时,时间复杂度是O(1)吗?

javascript - 使用 Google 可视化如何在特定行上触发选择事件?

javascript - 主干调用 View 中的函数

ruby-on-rails - 如何在没有规范框架的情况下使用 Minitest 首先测试工厂?

javascript - AngularJS 中对 http api 的调用次数为 `n` 次。我应该期待它们 `n` 次吗?

angular - 测试: blur emulate

javascript - jQuery icheck 插件在追加后不起作用

testing - Protractor - 元素在点 (968, 23) 不可点击

testing - hudson / Jenkins : Buckminster emma: How to exclude packages