Im having an issue with Supertest when testing Express endpoints where by it will randomly fail, usually with a AxiosError: Request failed with status code 404
error that has no useful stack trace.
The error occurs on a random file each time. If I run that files tests directly, they all pass fine, there is nothing actually wrong with any of the tests, they’ll all pass when run directly.
The problem is when running the full suite of tests (about 280 tests across about 40 files). There will always be at least one, sometimes more of this same error
● Test suite failed to run
AxiosError: Request failed with status code 404
at settle (node_modules/axios/lib/core/settle.js:19:12)
at IncomingMessage.handleStreamEnd (node_modules/axios/lib/adapters/http.js:599:11)
at Axios.request (node_modules/axios/lib/core/Axios.js:45:41)
The error does not seem to related to the tests in the file at all. Sometimes the test file that fails is a pure unit test that does not perform any sort of HTTP request, or even import anything with axios, yet it will throw that same axios error.
There is something strange going on, some sort of test overlapping, async test bleeding or race condition and I have no idea how to get to the bottom of it.
I found this issue with supertest: https://github.com/ladjs/supertest/issues/566
I spent a while refactoring all my tests to use this new startup and shutdown procedure, it did nothing. So im back to square one.
Can anyone help either debug this, or tell me how I can go about surfacing some more information to debug it? Its impossible to know where its happening as its failing tests that are not actually related to the error.
I cant even show an example of a failing test because they all pass when run seperately.
All of my tests follow this pattern
const request = supertest(app);
describe("functionName", () => {
it("should perform x", async () => {
const res = await request.get("/v1/asset");
const { result } = res.body;
expect(res.status).toEqual(200);
// etc etc
});
});
An example of app
, pretty standard stuff
import express from "express";
const app = express();
app.get("/routes", (req, res) => {
res.json({}));
});
export default app;
Running: Node 20.16, Yarn 4.4.0, Jest 29.7.0, Supertest 6.3.4
I know its a hard one to actually solve, but maybe someone has a suggestion of how to debug this, how can I actually find where this race condition or whatever it is, is happening.
Cheers