简体   繁体   English

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

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

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();
     });
});

You can (now) set it directly in the spec, as per Jasmine docs .您可以(现在)根据Jasmine 文档直接在规范中设置它。

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;
    });
});

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

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 doc which 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).如果您只想为一个 it() 设置自定义超时,您可以尝试将超时(毫秒)作为第三个参数(在字符串语句和函数之后)传递。 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.我预计它会失败。

Looks like you can now add it as the last argument for the it function:看起来您现在可以将其添加为it函数的最后一个参数:

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

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 文件中的所有测试

Adding: jasmine.DEFAULT_TIMEOUT_INTERVAL = yourTime;添加: jasmine.DEFAULT_TIMEOUT_INTERVAL = yourTime; on a helper file worked for me.在帮助文件上为我工作。

Put it after describe statement:把它放在describe语句之后:

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

In my case I had multiple tests cases and while I was using the aforementioned solution with was using the:在我的情况下,我有多个测试用例,而我在使用上述解决方案时正在使用:

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

the DEFAULT_TIMEOUT_INTERVAL was not updated at the first test case, so I had to add this: DEFAULT_TIMEOUT_INTERVAL 在第一个测试用例中没有更新,所以我不得不添加:

  beforeAll(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
  })

to my code to successfully run all the tests.到我的代码以成功运行所有测试。

To do this globally for all of your tests (in the case of e2e or integration testing) you can use a helper.要对所有测试(在 e2e 或集成测试的情况下)全局执行此操作,您可以使用帮助程序。

A helper file when configured correctly should get loaded before the tests are executed and allow you to change the DEFAULT_TIMEOUT_INTERVAL globally:正确配置的帮助文件应在执行测试之前加载,并允许您全局更改 DEFAULT_TIMEOUT_INTERVAL:

spec/support/jasmine.json规格/支持/茉莉花.json

{
    ...
    "helpers": [
        "/path/to/helpers/**/*.ts"
    ]
}

helpers/timeout.ts助手/超时.ts

jasmine.DEFAULT_TIMEOUT_INTERVAL = 300000;

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();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM