W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
Jest 允許在測試中模擬整個模塊,這對于測試你的代碼是否正確地從該模塊調用函數非常有用。但是,有時你可能希望在測試文件中使用部分模擬模塊,在這種情況下,希望訪問原始實現(xiàn),而不是模擬版本。
考慮為此?createUser
?函數編寫一個測試用例:
// createUser.js
import fetch from 'node-fetch';
export const createUser = async () => {
const response = await fetch('http://website.com/users', {method: 'POST'});
const userId = await response.text();
return userId;
};
你的測試將要模擬?fetch
?函數,以便我們可以確保在不實際發(fā)出網絡請求的情況下調用它。但是,還需要模擬?使用 ?Response
?(包裝在?Promise
?中)模擬fetch
?的返回值,因為我們的函數使用它來獲取創(chuàng)建的用戶 ID。因此,你最初可能會嘗試編寫這樣的測試:
jest.mock('node-fetch');
import fetch, {Response} from 'node-fetch';
import {createUser} from './createUser';
test('createUser calls fetch with the right args and returns the user id', async () => {
fetch.mockReturnValue(Promise.resolve(new Response('4')));
const userId = await createUser();
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith('http://website.com/users', {
method: 'POST',
});
expect(userId).toBe('4');
});
但是,如果運行該測試,你會發(fā)現(xiàn)該?createUser
?函數會失敗,并拋出錯誤:?TypeError: response.text is not a function
?。這是因為?Response
從中導入的類?node-fetch
?已被模擬(由于?jest.mock
?測試文件頂部的調用),因此它不再按應有的方式運行。
為了解決此類問題,Jest 提供了?jest.requireActual
?幫助程序。要使上述測試工作,請對測試文件中的導入進行以下更改:
// BEFORE
jest.mock('node-fetch');
import fetch, {Response} from 'node-fetch';
// AFTER
jest.mock('node-fetch');
import fetch from 'node-fetch';
const {Response} = jest.requireActual('node-fetch');
這允許你的測試文件從node-fetch
?導入實際?Response
?對象?,而不是模擬版本。這意味著測試現(xiàn)在將正確通過。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: