node.js 如何更改 jasmine-node 异步规范的超时时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9867601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:23:21  来源:igfitidea点击:

How do I change the timeout on a jasmine-node async spec

node.jsjasmine

提问by Brian Low

How can I get this test to pass without resorting to runs/waitsFor blocks?

如何在不诉诸运行/等待块的情况下通过此测试?

it("cannot change timeout", function(done) {

     request("http://localhost:3000/hello", function(error, response, body){

         expect(body).toEqual("hello world");

         done();
     });
});

采纳答案by Brian Low

Sent pull request for this feature (https://github.com/mhevery/jasmine-node/pull/142)

已发送此功能的拉取请求(https://github.com/mhevery/jasmine-node/pull/142

it("cannot change timeout", function(done) {

  request("http://localhost:3000/hello", function(error, response, body){

     expect(body).toEqual("hello world");

     done();
  });

}, 5000); // set timeout to 5 seconds

回答by Francisco

You can (now) set it directly in the spec, as per Jasmine docs.

您可以(现在)根据Jasmine docs直接在规范中设置它。

describe("long asynchronous specs", function() {

    var originalTimeout;

    beforeEach(function() {
        originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    });

    it("takes a long time", function(done) {
        setTimeout(function() {
            done();
        }, 9000);
    });

    afterEach(function() {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });
});

回答by Colin May

To set the global Jasmine-Node timeout, do this:

要设置全局 Jasmine-Node 超时,请执行以下操作:

jasmine.getEnv().defaultTimeoutInterval = timeoutYouWouldPrefer;// e.g. 15000 milliseconds

Credit to developer Gabe Hicks for figuring out the .getEnv() part via debugging in spite of misinformation in the README docwhich claims it's done by setting jasmine.DEFAULT_TIMEOUT_INTERVAL.

感谢开发人员 Gabe Hicks 通过调试找出 .getEnv() 部分,尽管自述文件中的错误信息声称它是通过设置 jasmine.DEFAULT_TIMEOUT_INTERVAL 完成的。

If you want to set a custom timeout just for one it(), you could try passing the timeout (milliseconds) as a third argument (after the string statement and the function). There's an example of that being done here, but I'm not sure what would happen if the custom timeout was longer than Jasmine's default. I expect it would fail.

如果您只想为一个 it() 设置自定义超时,您可以尝试将超时(毫秒)作为第三个参数(在字符串语句和函数之后)传递。还有的是正在做的例子在这里,但我不知道如果自定义超时比茉莉花的默认不再会发生什么。我预计它会失败。

回答by d-_-b

Looks like you can now add it as the last argument for the itfunction:

看起来您现在可以将其添加为it函数的最后一个参数:

describe('my test', function(){
    it('works', function(done){
        somethingAsync().then(done);
    }, 10000); // changes to 10 seconds
});

回答by danday74

In Angular, put this outside your describe block:

在 Angular 中,把它放在你的 describe 块之外:

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

This applies to all the tests in the .spec.ts file

这适用于 .spec.ts 文件中的所有测试

回答by Micha? Kuliński

Put it after describestatement:

把它放在describe声明之后:

describe("A saves to DB", function() {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

回答by ggozad

Why not by spying on setTimeout()?

为什么不通过监视setTimeout()

Something like:

就像是:

var spy = spyOn(window, 'setTimeout').andCallFake(function (func, timeout) {
    expect(timeout).toEqual(2500);
    func();
});

setTimeOut(function () { ... }, 2500);
expect(spy).toHaveBeenCalled();

回答by Ivan Rangel

Adding: jasmine.DEFAULT_TIMEOUT_INTERVAL = yourTime;on a helper file worked for me.

添加:jasmine.DEFAULT_TIMEOUT_INTERVAL = yourTime;在对我有用的帮助文件上。

回答by sharaj rewoo

Change j$.DEFAULT_TIMEOUT_INTERVAL to 10000 in following file: npm\node_modules\jasmine-core\lib\jasmine-core

在以下文件中将 j$.DEFAULT_TIMEOUT_INTERVAL 更改为 10000:npm\node_modules\jasmine-core\lib\jasmine-core