小编Yar*_*niy的帖子

函数内的 Jest 模拟函数

我不知道如何在玩笑中模拟内部函数的返回值我尝试了不同的方法。最后我找到了这个答案, 但由于某种原因不值得嘲笑,这是示例:

国家.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)

javascript jestjs create-react-app

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

create-react-app ×1

javascript ×1

jestjs ×1