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

92,613

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

In Angular, put this outside your describe block:

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

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

Share:
92,613
Brian Low
Author by

Brian Low

Updated on January 07, 2021

Comments

  • Brian Low
    Brian Low over 3 years

    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
    Brian Low over 12 years
    Thanks. These are integration tests, node.js is calling out to an external service that is often slow.
  • Simon Groenewolt
    Simon Groenewolt over 9 years
    Update for anyone stumbling on this answer in 2014: For Jasmine 2 setting jasmine.DEFAULT_TIMEOUT_INTERVAL works.
  • Guillaume
    Guillaume over 8 years
    this is brilliant, it makes it possible to test the timeout without making the test wait for the actual timeout
  • André Werlang
    André Werlang over 7 years
    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.
  • Ignacio Segura
    Ignacio Segura almost 7 years
    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
    Ignacio Segura almost 7 years
    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.
  • Dev Yego
    Dev Yego over 4 years
    I agree @willydee, had issues runnig tests with cloudinary and this little snippet came to my rescue.
  • domino_katrino
    domino_katrino over 4 years
    Not fruitful for the scenarios where libraries are installed every time test is run. Ex:- running CI on Cloud.
  • ruffin
    ruffin over 3 years
    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.
  • Steve Drake
    Steve Drake about 3 years
    don't think the PR got merged.
  • Matej
    Matej over 2 years
    They asked for nodejs, not typescript.
  • bobbyg603
    bobbyg603 over 2 years
    @MatejVoboril The same applies to js, just change the file extension to .js...