How do I change the timeout on a jasmine-node async spec
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
Brian Low
Updated on January 07, 2021Comments
-
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 over 12 yearsThanks. These are integration tests, node.js is calling out to an external service that is often slow.
-
Simon Groenewolt over 9 yearsUpdate for anyone stumbling on this answer in 2014: For Jasmine 2 setting jasmine.DEFAULT_TIMEOUT_INTERVAL works.
-
Guillaume over 8 yearsthis is brilliant, it makes it possible to test the timeout without making the test wait for the actual timeout
-
André Werlang over 7 yearsin 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 almost 7 yearsIt 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 almost 7 yearsYeah, 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 over 4 yearsI agree @willydee, had issues runnig tests with cloudinary and this little snippet came to my rescue.
-
domino_katrino over 4 yearsNot fruitful for the scenarios where libraries are installed every time test is run. Ex:- running CI on Cloud.
-
ruffin over 3 yearsWorth 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 whateverDEFAULT_TIMEOUT_INTERVAL
was beforeit
started. -
Steve Drake about 3 yearsdon't think the PR got merged.
-
Matej over 2 yearsThey asked for nodejs, not typescript.
-
bobbyg603 over 2 years@MatejVoboril The same applies to js, just change the file extension to .js...