I'm new to backend development and i face a problem that i don't understand.
I set up the 1st route of my API called "health" who just return a simple message to know if my server is up.
This route looks to works as expected.
However,
when I try to test this route with the method "toMatchSnapshot" from jest API, the test is not passing because of the in the url is changing constantly.
My test file "index.test.ts":
const request = supertest.agent(app);
describe("app", () => {
it("should return a successful response for GET /health", async () => {
const res = await request.get("/health");
res.header = omit(res.header, ["date"]);
expect(res).toMatchSnapshot();
});
});
index of the server "index.ts":
const app = express();
expressService(app);
if (require.main === module) {
app.listen(PORT, () => {
console.log("server started at http://localhost:" + PORT);
});
}
export default app;
my function "expressService":
const expressService = (app: Application) => {
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(api);
};
export default expressService;
My PORT variable: PORT = 3000;
- "url": "http://127.0.0.1:49694/health",
+ "url": "http://127.0.0.1:52568/health",
this is where the test is failing.
Thank you for your answers.