Keith
Keith

Reputation: 4204

In Mocha can I assert that a request times out?

I'm testing / confirming some DB lock stuff, and I want to stand up a test that'll confirm that the request ("request-promise" library) will hang if I lock the row.

Some ideas that didn't work were

request(options).catch((response) => {})

assert.throws(fn, Error, "timeout")

I continue to receive this message: Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Is there a way to assert that this timeout happens?

Upvotes: 0

Views: 200

Answers (2)

Keith
Keith

Reputation: 4204

My solution was to set a timeout for half the time I expected the timeout to take.

setTimeout(function(){ done(); })

Then in my endpoint promise response I had

done(new Error("Timeout should have happened.");

That way, if the timeout happens, done gets called. If it doesn't happen, then done gets called with an error.

Upvotes: 0

jakemingolla
jakemingolla

Reputation: 1649

In a mocha test (or describe, before, etc.), this.timeout(msec) will change how long the test can run for before Mocha will consider the test failed.

Check out the Mocha documentation for more information.

Upvotes: 2

Related Questions