Categories
JavaScript Answers

How to change the timeout on a Jasmine Node async spec?

Spread the love

Sometimes, we want to change the timeout on a Jasmine Node async spec.

In this article, we’ll look at how to change the timeout on a Jasmine Node async spec.

How to change the timeout on a Jasmine Node async spec?

To change the timeout on a Jasmine Node async spec, we can set the jasmine.DEFAULT_TIMEOUT_INTERVAL value.

For instance, we write

describe("long asynchronous specs", () => {
  let originalTimeout;

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

  it("takes a long time", (done) => {
    setTimeout(() => {
      done();
    }, 9000);
  });

  afterEach(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
  });
});

to set the jasmine.DEFAULT_TIMEOUT_INTERVAL value to 10000 milliseconds in the beforeEach callback to change the default timeout for each test.

Then we call it with a callback to add a test that calls done after 9000 milliseconds.

And then set jasmine.DEFAULT_TIMEOUT_INTERVAL to originalTimeout in the afterEach callback to reset the timeout to originalTimeout.

Conclusion

To change the timeout on a Jasmine Node async spec, we can set the jasmine.DEFAULT_TIMEOUT_INTERVAL value.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *