Preamble: I'm aware of this question, but found no useful answers there. The accepted answer there suggests that using async
and await
should address the issue, but it doesn't.
I've written this test in TypeScript using Mocha and Supertest:
it('returns 400 when redirectURL is missing', async () => {
const isAuthorized = (_req: Request) => true;
const hooks = { onPostAuth: async (_req: Request) => {} };
const app = createApp(isAuthorized, hooks);
await request(app)
.post('/')
.set('Content-Type', 'application/json')
.send({ }) // Body is missing redirectURL
.expect(400);
});
When I run it, as expected the test fails:
1) POST
returns 400 when redirectURL is missing:
Error: expected 400 "Bad Request", got 200 "OK"
At this point, the HTTP API looks like this:
export function createApp(
isAuthorized: (_: Request) => boolean,
hooks: { onPostAuth: (_: Request) => any; })
{
const app = express();
const authorize = createAuthorizationHandler(isAuthorized);
app.post("/", authorize, async (req, res) => {
const result = await hooks.onPostAuth(req);
return res.send(result);
})
return app;
}
All good. I now add an if
statement in the code to return 400 Bad Requst
if the required property is missing:
app.post("/", authorize, async (req, res) => {
if (!req.body.redirectURL) {
return res.status(400).send();
}
const result = await hooks.onPostAuth(req);
return res.send(result);
})
While I expect the test to pass, it now fails with this error message:
3) POST
returns 400 when redirectURL is missing:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\mark\Documents\Criipto\fusebit-extensibility\tests\index.test.ts)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
Why does it do that, and what can I do to rectify the problem?
Actually, just reading req.body.redirectURL
is enough to trigger that behaviour:
app.post("/", authorize, async (req, res) => {
if (!req.body.redirectURL) {
}
const result = await hooks.onPostAuth(req);
return res.send(result);
})
Although it doesn't do anything inside of that if
statement, the test still times out and hangs, as described above.