【发布时间】:2019-08-06 19:39:02
【问题描述】:
我不知道如何在 jest 中模拟内部函数的返回值 我尝试了不同的方法。最后我找到了这个answer 但由于某种原因不嘲笑,这里是例子:
countries.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' }));
countries.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',
},
];
mockFn.spyOn(countyListHelper, 'countriesList').mockReturnValue(expectedList);
// console.log('if return value mocked correctly',
// countyListHelper.countriesList() === expectedList); // true
expect(countyListHelper.getSortedCountryData('en')).toEqual(expectedList);
// shows error with received value list of all countries instead of mocked one
});
});
请建议我任何可能的解决方案,为什么内部函数的内部测试函数模拟返回值被忽略。从 Create React App 设置。
【问题讨论】:
标签: javascript jestjs create-react-app