[英]JEST error TypeError: specificMockImpl.apply is not a function
Trying to mock one of the function with callback from api and getting error as TypeError: specificMockImpl.apply is not a function
尝试使用api回调模拟其中一个函数并将错误视为
TypeError: specificMockImpl.apply is not a function
import { IEnvironmentMap, load } from 'dotenv-extended';
import { getTokensWithAuthCode, sdk } from '../src/connection-manager';
describe('getTokensWithAuthCode function Tests', () => {
jest.useFakeTimers();
let boxConfig: IEnvironmentMap;
beforeAll(() => {
boxConfig = load({
errorOnMissing: true,
});
});
it('should reject a promise if there is wrong auth code provided', async () => {
sdk.getTokensAuthorizationCodeGrant = jest.fn().mockImplementation(boxConfig.BOX_AUTH_CODE, null, cb => {
cb('Error', null);
});
try {
const tokens = await getTokensWithAuthCode();
} catch (error) {
expect(error).toBe('Error');
}
});
});
And my function which is trying to test is as follow: 而我试图测试的功能如下:
import * as BoxSDK from 'box-node-sdk';
import { IEnvironmentMap, load } from 'dotenv-extended';
import {ITokenInfo} from '../typings/box-node-sdk';
const boxConfig: IEnvironmentMap = load({
errorOnMissing: true,
});
export const sdk: BoxSDK = new BoxSDK({
clientID: boxConfig.BOX_CLIENT_ID,
clientSecret: boxConfig.BOX_CLIENT_SECRET,
});
/**
* - Use the provided AUTH_CODE to get the tokens (access + refresh)
* - Handle saving to local file if no external storage is provided.
*/
export async function getTokensWithAuthCode() {
return new Promise((resolve: (tokenInfo: ITokenInfo) => void, reject: (err: Error) => void) => {
if (boxConfig.BOX_AUTH_CODE === '') {
reject(new Error('No Auth Code provided. Please provide auth code as env variable.'));
}
sdk.getTokensAuthorizationCodeGrant(boxConfig.BOX_AUTH_CODE, null, (err: Error, tokenInfo: ITokenInfo) => {
if (err !== null) {
reject(err);
}
resolve(tokenInfo);
});
});
}
Is there any other way to mock function in jest? 还有其他方法可以在开玩笑中模拟功能吗? I have read an article https://www.zhubert.com/blog/2017/04/12/testing-with-jest/
我读过一篇文章https://www.zhubert.com/blog/2017/04/12/testing-with-jest/
On this line, rather than pass a function to mockImplementation
, you're passing three arguments: 在这一行,你传递三个参数,而不是将函数传递给
mockImplementation
:
jest.fn().mockImplementation(boxConfig.BOX_AUTH_CODE, null, cb => {
cb('Error', null);
});
It looks like you might have just missed some braces. 看起来你可能刚刚错过了一些牙箍。 Try switching it to:
尝试将其切换为:
jest.fn().mockImplementation((boxConfig.BOX_AUTH_CODE, null, cb) => {
cb('Error', null);
});
It's better not trying to mutate a const
used elsewhere. 最好不要试图改变其他地方使用的
const
。
You could change getTokensWithAuthCode
to make it receive sdk
as parameter, thus in your test you would pass the mock function as argument, therefore having a more predictable behavior than mutating directly sdk
. 您可以更改
getTokensWithAuthCode
以使其接收sdk
作为参数,因此在您的测试中,您将传递模拟函数作为参数,因此具有比直接变异sdk
更可预测的行为。
In your code, you could make a second getTokensWithAuthCode
implementation, with the signature getTokensWithAuthCodeUnbound(sdk)
for example, and export it. 在您的代码中,您可以使用签名
getTokensWithAuthCodeUnbound(sdk)
进行第二次getTokensWithAuthCode
实现,然后将其导出。 This implementation will be used in your tests. 此实现将用于您的测试。
Exporting using the same getTokensWithAuthCode
name, you would call: 使用相同的
getTokensWithAuthCode
名称导出,您可以调用:
export const getTokensWithAuthCode = getTokensWithAuthCodeUnbound.bind(null, sdk)
That way, your app will use getTokensWithAuthCodeUnbound
bound with the default sdk
, and you can test more easily its implementation. 这样一来,您的应用程序将使用
getTokensWithAuthCodeUnbound
绑定默认的sdk
,并且可以测试更容易实施。
Mozilla Developer Network (MDN) bind
documentation . Mozilla开发人员网络(MDN)
bind
文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.