I attempted to utilize Jest for testing my TypeScript script
// api.ts
import got from "got";
export const run = async () => {
const body = await got.get('https://jsonplaceholder.typicode.com/posts/1').json();
return body;
};
Here is my test:
// api.test.ts
import { run } from "../api";
import got from "got";
import { mocked } from "ts-jest/dist/util/testing";
jest.mock("got");
test("using another got", async () => {
const response = {
get: jest.fn(),
};
mocked(got).mockResolvedValue(response);
const result = await anotherGot();
console.log(result);
// expect(result).toBe(response.body);
});
However, when I tried running the test (npm test
), I encountered an error:
TypeError: Cannot read property 'json' of undefined
What is the best approach to handle this issue in a Jest test?