我是Jest的初学者,这些是定义的类。APIService类不会导出;仅定义了openURL函数。
APIService.ts
export const openURL = async (openURL : string) => {
await Linking.openURL(openURL );
};
RegistrationPage.tsx
import{openURL} from '../APIService';
RegistrationPage.test.ts
test('should call function openURL with empty value', async () => {
const url = '';
const mockOpenURL = jest.fn();
mockOpenURL .mockImplementation(() => Promise.resolve(''));
const openURLSpy = jest.spyOn(openURL, 'openURL');
const mockURL = await openURL(url);
expect(mockOpenURL).toBeCalled();
expect(mockURL).toEqual(url);
expect(mockOpenURL).toHaveBeenCalledWith(url);
openURLSpy.mockRestore();
});
根据我的低估编写此函数后,可能是漏洞没有正确模拟或间谍运行,从而导致无法使用Jest将类型为“ openURL”的参数分配给“从不”的参数
改进此测试用例的建议将有所帮助。
仅在本地定义变量const mockOpenURL = jest.fn()
就不能模拟任何内容。
jest.spyOn(openURL, 'openURL')
失败,因为openURL
是一个函数,没有openURL.openURL
属性。
APIService
需要模拟的模块:
import{openURL} from '../APIService';
jest.mock('../APIService', () => {
return {
__esModule: true,
openURL: jest.fn()
}
};
...
openURL.mockResolvedValue();
// code that calls openURL
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句