When testing the default error handler of an express application, it results in a timeout. The function looks as follows:
const createApp = (underlyingFunction) => {
const app = express()
app.get('/my-endpoint', async (req, res) => {
await underlyingFunction()
res.send({ success: true })
})
const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
console.error('Unhandled exception');
console.error(error);
console.error(error.stack);
res.status(500).send({
message: 'Oh dear',
});
// next()
}
app.use(errorHandler)
return app;
}
And the test looks as follows:
test('error should be handled and return 500', async () => {
underlyingFunction.mockImplementation(() => {
throw new Error('Something went wrong')
})
const app = createApp(underlyingFunction)
const response = await request(app).get('/my-endpoint')
expect(response.status).toBe(500)
})
When running the test, I get the following error:
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
What could be causing this?
For express
V4, from the Error Handling#Catching Errors doc, we know:
For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the
next()
function, where Express will catch and process them.
Although the underlyingFunction
of the mock in the test case is synchronous, but in the route, the async/await
syntax converts this route handler into asynchronous code.
So, you need to use try...catch
statement to catch the error raised from underlyingFunction
function. And pass the error
to the next
function. express
will route the request to the error handler middleware with that error
.
E.g.
app.ts
:
import express from 'express';
import { ErrorRequestHandler } from 'express-serve-static-core';
export const createApp = (underlyingFunction) => {
const app = express();
app.get('/my-endpoint', async (req, res, next) => {
try {
await underlyingFunction();
res.send({ success: true });
} catch (error) {
next(error);
}
});
const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
console.error('Unhandled exception');
res.status(500).send({ message: 'Oh dear' });
};
app.use(errorHandler);
return app;
};
app.test.ts
:
import request from 'supertest';
import { createApp } from './app';
describe('68923821', () => {
test('error should be handled and return 500', async () => {
const underlyingFunction = jest.fn().mockImplementation(() => {
throw new Error('Something went wrong');
});
const app = createApp(underlyingFunction);
const res = await request(app).get('/my-endpoint');
expect(res.status).toEqual(500);
});
});
test result:
PASS examples/68923821/app.test.ts (9.128 s)
68923821
✓ error should be handled and return 500 (49 ms)
console.error
Unhandled exception
15 |
16 | const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
> 17 | console.error('Unhandled exception');
| ^
18 | res.status(500).send({ message: 'Oh dear' });
19 | };
20 |
at errorHandler (examples/68923821/app.ts:17:13)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.661 s