如何在 got.post() 重写 return 方法(使用 jest mock),这样我就可以调用 json 方法

How to rewrite return method at got.post() (using jest mock), so I can call json method

我尝试使用 jest 来测试我的脚本 (typescript)

// api.ts
import got from "got";


export const run = async () => {
  const body = await got.get('https://jsonplaceholder.typicode.com/posts/1').json();
  return body;
};

和我的测试

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

当我尝试 运行 测试时 (npm test) 我收到错误

TypeError: Cannot read property 'json' of undefined

如何处理jest测试中的代码?

您正在尝试模拟函数 got 本身(这也是一个函数)。但是你需要模拟 got.get 函数。

Got npm package 实现了 2 种调用 HTTP GET 请求的方式:

  1. const response = got('http://google.com', { method: 'get' });
  2. const response = got.get('http://google.com');

因此,如果您想模拟 got.get(...),您需要模拟 got.get 而不是 got 本身(用例 #2):

// api.test.ts
// import { run } from "../api";
import got from "got";
import { mocked } from "ts-jest/utils";

jest.mock("got");

test("using another got", async () => {
    const mockedGot = mocked(got);

    // use case #1 - using got module directly
    mockedGot.mockReturnValue({
        json: () => Promise.resolve({ dataAttr1: 'val11111' }),
    } as any)

    const response1 = got('http://www.google.com', { method: 'get' });
    const data1 = await response1.json();
    expect(data1).toEqual({ dataAttr1: 'val11111' })

    /*******************************************************************/

    // use case #2 - using got.get "alias"
    // this is your case :)
    mockedGot.get = jest.fn().mockReturnValue({
        json: () => Promise.resolve({ dataAttr1: 'val22222' }),
    } as any);

    const response2 = got.get('http://www.google.com');
    const data2 = await response2.json();
    expect(data2).toEqual({ dataAttr1: 'val22222' })
});