我不知道如何在玩笑中模拟内部函数的返回值我尝试了不同的方法。最后我找到了这个答案, 但由于某种原因不值得嘲笑,这是示例:
国家.js
export const countryList = () => [
{
label: '+244',
value: 'Angola',
}, // list of all possible countries very long...
];
export const getSortedCountryData = intlLang =>
countriesList()
.sort((compare, comparable) =>
compare.value.localeCompare(comparable.value, intlLang, { sensitivity: 'base' }));
Run Code Online (Sandbox Code Playgroud)
国家.test.js
import * as countyListHelper from './countries';
describe('countries list', () => {
test('returns list of countries', () => {
const mockFn = jest.mock();
const expectedList = [
{
label: '+244',
value: 'Angola',
},
{
label: '+43',
value: 'Austria', …Run Code Online (Sandbox Code Playgroud)