测试扩展类时开玩笑模拟 ES6 基类的方法(超级方法)

Alb*_*ses 6 javascript unit-testing jestjs apollo-server

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

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

Run Code Online (Sandbox Code Playgroud)

基本上,我想模拟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"})
});

Run Code Online (Sandbox Code Playgroud)

知道该怎么做吗?已经尝试过jest.mock() jest.spyOn等等,但我似乎无法得到它......

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

sli*_*wp2 13

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

\n

例如

\n

ApiDataSource.js:

\n
import { RESTDataSource } from \'apollo-datasource-rest\';\n\nexport default class ApiDataSource extends RESTDataSource {\n  constructor() {\n    super();\n    this.baseURL = \'test\';\n  }\n\n  async get(path, params = {}, init = {}) {\n    return super.get(\n      path,\n      {\n        ...params,\n        app_id: \'test app id\',\n        app_key: \'test app key\',\n      },\n      init\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

ApiDataSource.test.js:

\n
import { RESTDataSource } from \'apollo-datasource-rest\';\nimport ApiDataSource from \'./ApiDataSource\';\n\ndescribe(\'65391369\', () => {\n  test(\'adds the authorization parameters to the get call\', async () => {\n    const mockedSuperGet = jest.spyOn(RESTDataSource.prototype, \'get\').mockResolvedValueOnce(\'fake data\');\n    const apiDataSource = new ApiDataSource();\n    await apiDataSource.get(\'test\');\n    expect(mockedSuperGet).toHaveBeenCalledWith(\'test\', { app_id: \'test app id\', app_key: \'test app key\' }, {});\n    mockedSuperGet.mockRestore();\n  });\n});\n
Run Code Online (Sandbox Code Playgroud)\n

单元测试结果:

\n
 PASS  examples/65391369/ApiDataSource.test.js (5.273 s)\n  65391369\n    \xe2\x9c\x93 adds the authorization parameters to the get call (4 ms)\n\n------------------|---------|----------|---------|---------|-------------------\nFile              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n------------------|---------|----------|---------|---------|-------------------\nAll files         |     100 |      100 |     100 |     100 |                   \n ApiDataSource.js |     100 |      100 |     100 |     100 |                   \n------------------|---------|----------|---------|---------|-------------------\nTest Suites: 1 passed, 1 total\nTests:       1 passed, 1 total\nSnapshots:   0 total\nTime:        6.28 s\n
Run Code Online (Sandbox Code Playgroud)\n