Preamble: Despite looking at this question on Stack Overflow, I did not find any helpful solutions. The suggested solution of using async
and await
did not resolve the issue for me.
This TypeScript test was created 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);
});
The test failed as expected:
1) POST
returns 400 when redirectURL is missing:
Error: expected 400 "Bad Request", got 200 "OK"
Here is the current state of the HTTP API:
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;
}
After adding an if
statement to handle missing properties, the test started failing with a timeout error:
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)
I encountered this issue even without any actual logic inside the if
statement. Why is this happening and how can I fix it?
Simply accessing req.body.redirectURL
seems to trigger the timeout problem:
app.post("/", authorize, async (req, res) => {
if (!req.body.redirectURL) {
}
const result = await hooks.onPostAuth(req);
return res.send(result);
})
Despite the empty condition in the if
statement, the test continues to hang and time out.