我尝试使用Jest测试React Native库,但它显示错误,有人可以帮助我解决我的问题吗?
这里是我的函数代码:
export const createChannel = (): void => {
PushNotification.createChannel({
channelId: 'test-channel',
channelName: 'Test Channel',
vibrate: true,
});
};
我在这个函数中使用了react-native-push-notification库
下面是我的测试代码:import PushNotification from 'react-native-push-notification';
import {createChannel} from '../src/functions/PomodoroFunction';
jest.mock('react-native-push-notification', () => 'PushNotification.createChannel');
describe('Create Channel unit test', () => {
it('Should be called',()=>{
const mockFN = createChannel()
expect(mockFN).toHaveBeenCalled();
})
});
显示的错误:TypeError: _reactativepushnotification .default. createchannel不是一个函数
谁能帮我解决这个问题,非常感谢!
Try
import * as PushNotification from 'react-native-push-notification';
...
const create = jest.fn();
const rnpn = jest.spyOn(PushNotification, 'default').mockImplementation(() => {
return { createChannel: create } as any;
});
...
然后在测试中使用create
。我不确定您在上面的测试中测试的是什么,但这是模拟您不想在测试中运行的函数的好方法。