【问题标题】:Jest / Supertest Error - Right-hand side of 'instanceof' is not callable开玩笑/超级测试错误 - 'instanceof' 的右侧不可调用
【发布时间】:2020-06-24 19:51:03
【问题描述】:

当像这样使用超测时,

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

  });
});

我收到"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'

这只是supertest,API在使用Postman时有效。

此调用的其余代码,

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

是否缺少用于 supertest 和 typescript 的配置?

【问题讨论】:

  • 这里有同样的问题,你解决了吗?
  • 我做了,现在不知道如何,但会发布工作代码。
  • 虽然我在测试中使用了async 和await,但我得到了同样的错误。最后,我发现我忘记了一个await 用于我的 API 控制器中的异步操作。结果,控制器在 promise 解决之前返回了响应,而 Jest 不喜欢这样。

标签: express supertest


【解决方案1】:

如果您不想在代码中使用“await”,可以在回调函数中使用“done()”。 像这样。

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

【讨论】:

    【解决方案2】:

    这种方法奏效了,

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

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 2022-07-26
      • 2017-09-16
      • 2017-01-18
      • 1970-01-01
      • 2019-09-17
      • 2022-08-07
      • 2020-08-16
      • 2020-09-03
      相关资源
      最近更新 更多