7

When using supertest like so,

import app from "../../src/app";
import request from "supertest";

describe("GET / - a simple api endpoint", () => {
  it("Hello API Request", () => {
    const result = request(app)
    .get("/api/location/5eda6d195dd81b21a056bedb")
    .then((res) => {
      console.log(res);
    })
    // expect(result.text).toEqual("hello");
    // expect(result.status).toEqual(200);

  });
});

Im getting "Right-hand side of 'instanceof' is not callable".

 at Response.toError (node_modules/superagent/lib/node/response.js:94:15)
      at ResponseBase._setStatusProperties (node_modules/superagent/lib/response-base.js:123:16)
      at new Response (node_modules/superagent/lib/node/response.js:41:8)
      at Test.Request._emitResponse (node_modules/superagent/lib/node/index.js:752:20)
      at node_modules/superagent/lib/node/index.js:916:38
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:19:7)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
          status: 500,
          text: `"Right-hand side of 'instanceof' is not callable"`,
          method: 'GET',
          path: '/api/location/5eda6d195dd81b21a056bedb'

This is just with supertest, the API works when using Postman.

Rest of the code for this call,

router.get(
  "/location/:id",
  (req, res) => {
    locationController.getLocation(req, res);
  }
);


const getLocation = async (req: Request, res: Response): Promise<void> => {
  const { id } = req.params;
  const location = await data.readRecord(id, Location);
  res.status(location.code).json(location.data);
};


const readRecord = async (id: string, model: IModel): Promise<Response> => {
  try {
    const response = await model.findById(id);
    if (response == null) return { code: 404, data: `ID ${id} Not Found` };
    return { code: 200, data: response };
  } catch (error) {
    return errorHandler(error);
  }
};

Is there a configuration im missing for supertest and typescript?

Terry
  • 1,621
  • 4
  • 25
  • 45
  • Having the same issue here, did u solve it? – MatayoshiMariano Oct 14 '20 at 15:44
  • I did, not sure how now, but will post the working code. – Terry Oct 22 '20 at 13:15
  • I got the same error although I was using `async` and `await` in my test. In the end it turned out that I had forgotten an `await` for an asynchronous operation in my API controller. As a result, the controller returned a response before the promise was resolved, and Jest did not like that. – Elias Strehle Jun 14 '21 at 13:35

4 Answers4

0

This approach worked,

import request = require("supertest");
import app from "../../src/app";

describe("GET/ api/location/id", () => {
  it("should connect retrieve record and retrieve a code 200 and json response", async () => {
    const res = await request(app)
      .get(`/api/location/${id}`)

    expect(res.status).toBe(200);
    expect(res.body._id).toBe(`${id}`);
  });
});
Terry
  • 1,621
  • 4
  • 25
  • 45
0

If you don't want to use "await" in your code , you can use "done()" in callback function. like this.

    import app from "../../src/app";
    import request from "supertest";
    
    describe("GET / - a simple api endpoint", () => {
      it("Hello API Request", (done) => {
        const result = request(app)
        .get("/api/location/5eda6d195dd81b21a056bedb")
        .then((res) => {
          console.log(res);
          expect(res.text).toEqual("hello");
          expect(res.status).toEqual(200);
          done();
        //done() function means this test is done.
        })
      });
    });
鄭棋文
  • 11
  • 1
  • 2
0

Awaiting the expect call (with Jest) worked for me.

await expect(...).rejects.toThrow()
Riadh Adrani
  • 486
  • 2
  • 5
  • 16
0
TypeError: Right-hand side of 'instanceof' is not callable'

This error occurred when I checked an object value with an enum, like:

expect.any(ESomeEnum)

If you have the same issue you can check the following answer

zemil
  • 3,235
  • 2
  • 24
  • 33