测试扩展 class 时基本 ES6 class(超级方法)的 Jest 模拟方法

Jest mock method of base ES6 class (super method) when testing extended class

我在测试使用某些参数调用原始方法(来自基础 class)时遇到问题,在测试扩展 class 时遇到问题。我要测试的 class 是:

// ApiDataSource.js
import { RESTDataSource } from "apollo-datasource-rest";

export default class ApiDataSource extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'test';
  }

  //We add the authorization params to the get method
  async get(path, params = {}, init = {}) {
    return super.get(
      path,
      {
        ...params,
        app_id: "test app id",
        app_key: "test app key",
      },
      init
    );
  }
}

基本上,我想模拟 super.get() 以断言当调用 ApiDataSource.get() 时,会使用授权参数调用 super 方法。

类似于:

// ApiDataSource.test.js
import ApiDataSource from './ApiDataSource'

// ...
test("adds the authorization parameters to the get call", async () => ({
  const class = new ApiDataSource();
  await class.get("test");

  expect(mockedSuperGet).toHaveBeenCalledWith("test", {app_id: "test app id", app_key: "test app key"})
});

知道怎么做吗?已经尝试了 jest.mock() jest.spyOn 等等,但我似乎无法理解...

代码沙盒: https://codesandbox.io/s/example-test-api-data-source-6ldpk?file=/src/ApiDataSource.test.ts

您可以使用 jest.spyOn(object, methodName)RESTDataSource.prototype.get 方法创建模拟函数。

例如

ApiDataSource.js:

import { RESTDataSource } from 'apollo-datasource-rest';

export default class ApiDataSource extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'test';
  }

  async get(path, params = {}, init = {}) {
    return super.get(
      path,
      {
        ...params,
        app_id: 'test app id',
        app_key: 'test app key',
      },
      init
    );
  }
}

ApiDataSource.test.js:

import { RESTDataSource } from 'apollo-datasource-rest';
import ApiDataSource from './ApiDataSource';

describe('65391369', () => {
  test('adds the authorization parameters to the get call', async () => {
    const mockedSuperGet = jest.spyOn(RESTDataSource.prototype, 'get').mockResolvedValueOnce('fake data');
    const apiDataSource = new ApiDataSource();
    await apiDataSource.get('test');
    expect(mockedSuperGet).toHaveBeenCalledWith('test', { app_id: 'test app id', app_key: 'test app key' }, {});
    mockedSuperGet.mockRestore();
  });
});

单元测试结果:

 PASS  examples/65391369/ApiDataSource.test.js (5.273 s)
  65391369
    ✓ adds the authorization parameters to the get call (4 ms)

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |     100 |      100 |     100 |     100 |                   
 ApiDataSource.js |     100 |      100 |     100 |     100 |                   
------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        6.28 s