gpt4 book ai didi

javascript - 测试扩展类时的 ES6 基类( super 方法)的 Jest 模拟方法

转载 作者:行者123 更新时间:2023-12-05 01:09:16 24 4
gpt4 key购买 nike

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

// 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

关于javascript - 测试扩展类时的 ES6 基类( super 方法)的 Jest 模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65391369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com