gpt4 book ai didi

javascript - 无限 Jasmine 超时

转载 作者:可可西里 更新时间:2023-11-01 01:18:27 25 4
gpt4 key购买 nike

这基本上是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com