skip to Main Content

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);
  });
});

2

Answers


  1. Chosen as BEST ANSWER

    I found the reason, the $ character in the express router needs to be escaped like \$, therefore the solution:

    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/:groupId/members/:userId/\$ref", (req, res) => {
        console.log(`ENDPOINT remove ${req.params.groupId} ${req.params.userId}`);
        res.sendStatus(200);
      });
    
      let server: Server;
    
      beforeAll(done => {
        server = app.listen(3001, done);
      });
    
      afterAll(done => {
        server.close(done);
      });
    
      it("should not four oh four", async () => {
        const res = await axios.delete(
          "http://localhost:3001/graph/groups/group123/members/user123/$ref",
        );
    
        expect(res.status).toBe(200);
      });
    });
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search