[英]Argument of type '"openURL"' is not assignable to parameter of type 'never' using Jest
我是 Jest 的初學者,這些是定義的類。 APIService class 未導出; 僅定義了 openURL function。
API服務.ts
export const openURL = async (openURL : string) => {
await Linking.openURL(openURL );
};
注冊頁面.tsx
import{openURL} from '../APIService';
注冊頁面.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();
});
根據我的輕描淡寫寫完這個 function 之后可能是它有漏洞,沒有正確模擬或監視它運行它導致錯誤類型“openURL”的參數無法使用 Jest 分配給類型“從不”的參數
改進此測試用例的建議將很有幫助。
僅僅在本地定義一個變量,比如const mockOpenURL = jest.fn()
不能模擬任何東西。
jest.spyOn(openURL, 'openURL')
失敗,因為openURL
是一個 function 並且沒有openURL.openURL
屬性。
需要模擬的是APIService
模塊:
import{openURL} from '../APIService';
jest.mock('../APIService', () => {
return {
__esModule: true,
openURL: jest.fn()
}
};
...
openURL.mockResolvedValue();
// code that calls openURL
謝謝 @jest.mock('../APIService.ts', () => { return { __esModule: true, openURL: jest.fn(), }; });
test('openURL function to be called with', async () => {
const openURL = 'www.google.com';
const openURL = jest.fn();
openURL.mockReturnValue(openURL );
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.