我正在使用Jest编写测试并模拟调用HTTP请求的函数。
import { mocked } from "ts-jest/utils";
import * as pull from "../src/pull";
import fs = require("fs");
// read the reponse data from a file.
const response = JSON.parse(
fs.readFileSync("./__fixtures__/pr.json", "utf8")
);
// have jest mock the function and set it's response.
jest.mock("../src/pull");
const mockedCoeus = mocked(pull.getPullRequest, true);
mockedCoeus.mockImplementation(async () => {
return response as any;
});
// write the test.
describe("#get details for a PR", () => {
it("should load user data", async () => {
const data = await pull.getPullRequest(165, "data-ios");
expect(data).toBeDefined();
expect(data.updated_at).toEqual("2020-04-10T16:46:30Z");
});
});
测试通过了,但是,运行npm jest
时出现以下错误
TypeError: mockedCoeus.mockImplementation is not a function
我查看了其他报告的错误,这些错误与jest.mock
的位置有关,但是,这里似乎不是这样。为什么会引发此错误,但是测试通过了?我该如何解决?