Brian Low
Brian Low

Reputation: 11811

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

Upvotes: 114

Views: 107007

Answers (11)

bobbyg603
bobbyg603

Reputation: 3840

To do this globally for all of your tests (in the case of e2e or integration testing) you can use a helper.

A helper file when configured correctly should get loaded before the tests are executed and allow you to change the DEFAULT_TIMEOUT_INTERVAL globally:

spec/support/jasmine.json

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

helpers/timeout.ts

jasmine.DEFAULT_TIMEOUT_INTERVAL = 300000;

Upvotes: 1

Andreas Foteas
Andreas Foteas

Reputation: 452

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:

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

to my code to successfully run all the tests.

Upvotes: 2

Michał Kuliński
Michał Kuliński

Reputation: 1976

Put it after describe statement:

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

Upvotes: 4

danday74
danday74

Reputation: 57016

In Angular, put this outside your describe block:

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

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

Upvotes: 13

Ivan Rangel
Ivan Rangel

Reputation: 21

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

Upvotes: 2

sharaj rewoo
sharaj rewoo

Reputation: 1

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

Upvotes: -21

Francisco
Francisco

Reputation: 2056

You can (now) set it directly in the spec, as per 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;
    });
});

Upvotes: 133

d-_-b
d-_-b

Reputation: 23181

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

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

Upvotes: 30

Colin May
Colin May

Reputation: 451

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

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.

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.

Upvotes: 44

Brian Low
Brian Low

Reputation: 11811

Sent pull request for this feature (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

Upvotes: 89

ggozad
ggozad

Reputation: 13105

Why not by spying on setTimeout()?

Something like:

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

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

Upvotes: 0

Related Questions