111

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();
     });
});
Brian Low
  • 11,605
  • 4
  • 58
  • 63

11 Answers11

132

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;
    });
});
Francisco
  • 2,018
  • 2
  • 16
  • 16
  • Yeah, changing ```jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;``` value did the trick for me on a brand new setup built from scratch using Jasmine + Jest. It works as expected. – Ignacio Segura Sep 14 '17 at 08:12
  • I agree @willydee, had issues runnig tests with cloudinary and this little snippet came to my rescue. – Dev Yego Nov 11 '19 at 14:32
  • 2
    Worth noting perhaps that this has to happen before the `it`. By the time you're in the test, `done` seems to have already hooked into whatever `DEFAULT_TIMEOUT_INTERVAL` was before `it` started. – ruffin Nov 09 '20 at 17:26
87

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
Brian Low
  • 11,605
  • 4
  • 58
  • 63
44

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.

Colin May
  • 451
  • 4
  • 6
  • 29
    Update for anyone stumbling on this answer in 2014: For Jasmine 2 setting jasmine.DEFAULT_TIMEOUT_INTERVAL works. – Simon Groenewolt Nov 12 '14 at 21:54
  • 2
    It works for me on a brand new setup using Jest + Jasmine. I just added ```jasmine.DEFAULT_TIMEOUT_INTERVAL = 12000;``` on a global config file I use for all tests and it works as expected. – Ignacio Segura Sep 14 '17 at 08:09
29

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
});
d-_-b
  • 21,536
  • 40
  • 150
  • 256
13

In Angular, put this outside your describe block:

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

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

danday74
  • 52,471
  • 49
  • 232
  • 283
4

Put it after describe statement:

describe("A saves to DB", function() {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
Akshaya Shanbhogue
  • 1,438
  • 1
  • 13
  • 25
Michał Kuliński
  • 1,928
  • 3
  • 27
  • 51
3

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

2

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.

Andreas Foteas
  • 422
  • 3
  • 11
1

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;
bobbyg603
  • 3,536
  • 2
  • 19
  • 30
0

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();
ggozad
  • 13,105
  • 3
  • 40
  • 49
  • 1
    Thanks. These are integration tests, node.js is calling out to an external service that is often slow. – Brian Low Mar 31 '12 at 04:02
  • this is brilliant, it makes it possible to test the timeout without making the test wait for the actual timeout – Guillaume Feb 24 '16 at 03:42
  • in this fashion we don't exercise the passage of time, and calling the fn right away, while the original never calls it before the next cycle. – André Werlang Feb 24 '17 at 01:30
-21

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