Search code examples
javascriptnode.jstypescriptexpressaxios

Can someone help me identify why this express server returns a 404?


This test fails with a 404 error, can someone help me figure out what is wrong because I really don't see the issue, I need a second pair of eyes I think. I am trying to mock a Microsoft Graph API request to remove someone from an Entra group. It works fine the actual code but I am trying to write an integration tests with a mock backend as you can see.

import axios from "axios";
import express from "express";
import { Server } from "http";

describe.only("fourohfour", () => {
  const app = express();
  app.use(express.json());
  // Entra remove member
  app.delete("graph/groups/:group/members/:userId/$ref", (req, res) => {
    console.log(`ENDPOINT remove ${req.params.group} ${req.params.userId}`);
    res.sendStatus(200);
  });

  let server: Server;

  beforeAll(done => {
    server = app.listen(3001, done);
  });

  afterAll(async () => {
    server.close();
  });

  it("should not four oh four", async () => {
    const res = await axios.delete(
      "http://localhost:3001/graph/groups/123/members/456/$ref",
    );

    expect(res.status).toBe(200);
  });
});

Solution

  • As the express documentation states:

    If you need to use the dollar character ($) in a path string, enclose it escaped within ([ and ]). For example, the path string for requests at “/data/$book”, would be “/data/([$])book”.

    So in your case changing the route schema to:

     /graph/groups/:group/members/:userId/([$])ref
    

    should do the trick.