I have no idea how to mock return value of inner function inside jest I tried different approaches. Finally I found this answer but value not mocking for some reason, here it is example:
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
});
});
Please suggest me any possible solutions why inside test function mocked return value for inner function is ignored. Setup from Create React App.