【问题标题】:Jest mock function inside function函数内部的笑话模拟函数
【发布时间】: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


    【解决方案1】:

    您链接到的问题当前接受的答案无效。我添加了一个 new answer 并附有解释和工作示例。

    同样的概念在这里适用:模拟替换函数的模块导出,因此要能够在getSortedCountryData 中模拟countriesList,您必须调用模块导出强>为countriesList。

    一种选择是将countriesList 移动到它自己的模块中。

    另一种选择是利用"ES6 modules support cyclic dependencies automatically"这一事实,因此将import模块转换为自身是完全有效的,这样您就可以为countriesList调用模块导出:

    countries.js

    import * as countyListHelper from './countries';
    
    export const countriesList = () => [
      {
        label: '+244',
        value: 'Angola',
      }, // list of all possible countries very long...
    ];
    
    export const getSortedCountryData = intlLang =>
      countyListHelper.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 expectedList = [
          {
            label: '+244',
            value: 'Angola',
          },
          {
            label: '+43',
            value: 'Austria',
          },
        ];
    
        const spy = jest.spyOn(countyListHelper, 'countriesList');
        spy.mockReturnValue(expectedList);
    
        expect(countyListHelper.getSortedCountryData('en')).toEqual(expectedList);  // Success!
    
        spy.mockRestore();
      });
    });
    

    【讨论】:

    • 使用循环导入看起来更像是一个黑客,但谢谢它对我很有帮助,我想在不同的文件中分开,但让我停下来的事情是它有点不好解决将一个逻辑连接的功能分成不同的文件。
    猜你喜欢
    • 2020-06-24
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多