【问题标题】:How to determine if a nested function has been called with jest.fn()?如何确定是否已使用 jest.fn() 调用了嵌套函数?
【发布时间】:2019-07-15 16:06:51
【问题描述】:

我有一套方便的 apiHelpers。其中一个函数是 axios 的 get 方法的包装器,称为 getWrapper

const getWrapper = url => (endpoint, headers = {}) => {
  return axios.get(`${url}${endpoint}`, { headers: { ...getHeaders(), ...headers } });
};

对于未经身份验证的调用,我有一个调用getWrappergetUnauthenticatedApiHelper 函数。 getWrapper 做了一些事情,其中​​之一是将提供的端点连接到 BASE_URL 这是一个环境变量。

export const getUnauthenticatedApiHelper = (endpoint, headers) => getWrapper(BASE_URL)(endpoint, headers);

我的测试调用 getUnauthenticatedApiHelper 并期望它随后调用 getWrapper。

describe('apihelpers', () => {
  test('getUnauth', () => {
    const getWrapper = jest.fn();
    var wtf = getUnauthenticatedHelper('end/point/');
    console.log(wtf);
    expect(getWrapper).toHaveBeenCalled();
  });
});

当我注销结果时,我可以看到端点已正确连接 BASE_URL,但是,我的测试失败并出现错误 Expected mock function to have been called, but it was not called.

这是否是一个有效的测试对象与 atm 无关,因为我只是想弄清楚如何有效地使用 jest。我一直在在线 repl 中对此进行试验 - 无论我做什么,我都会遇到getWrapper 未被调用的问题,这让我相信我对 jest.fn() 有一些基本的误解 -那么我错过了什么,我应该如何测试 getWrapper 被调用?

Here is an online repl 我一直在试验的东西

【问题讨论】:

  • 从如何组织模块的问题中不清楚。 getWrapper 存根不会被魔法调用。需要模拟包含原始 getWrapper 的模块。
  • @estus getWrapper 是与 getUnauthenticatedHelper 在同一模块中定义的函数。我的想法是,通过在我的测试中执行 getUnauthenticatedHelper 函数,jest 将寻找一个名为 getWrapper 的函数定义,然后调用它。在这种情况下, jest.fn() 提供了函数定义(当然跳过了实现),并且 toHaveBeenCalled 应该是真的。我的理解不正确吗?我试图通过在测试中定义这些函数来消除模块组织的复杂性 - 请参阅:repl.it/repls/CyberRustyVirus
  • 你不能模拟在同一个模块中使用的函数。请参阅链接,stackoverflow.com/questions/52235196/…jest 将寻找一个名为 getWrapper 的函数定义,然后调用它 - 它不会,这不是 Jest 特有的,JS 只是不能那样工作。在这种情况下,您需要模拟 axios.get,而不是使用它的函数。

标签: reactjs testing jestjs


【解决方案1】:

我制作了你的演示作品,你可以在这里查看:https://repl.it/repls/IllStandardDesigner

getWrapper.js:

function getWrapper(url) {
  return (endpoint, headers={}) => (
    `${url}${endpoint}`
  );
}

module.exports = getWrapper

getUnauthenticatedHelper.js:

const getWrapper = require('./getWrapper');

const BASE_URL = 'foo/';

const getUnauthenticatedHelper = (endpoint, headers) => getWrapper(BASE_URL)(endpoint, headers);
module.exports = getUnauthenticatedHelper

单元测试:

const add = require('./add');
const getWrapper = require('./getWrapper');
const getUnauthenticatedHelper = require('./getUnauthenticatedHelper');

describe('add', () => {
  it('should add two numbers', () => {
    expect(add(1, 2)).toBe(3);
  });
});

jest.mock('./getWrapper', () => {
  return jest.fn((url) => {
    return (endpoint, headers = {}) => (
      `${url}${endpoint}`
    );
  })
})

describe('apihelpers', () => {
  test('getUnauth', () => {
    var wtf = getUnauthenticatedHelper('end/point/');
    console.log(wtf);
    expect(getWrapper).toHaveBeenCalled();
  });
});

单元测试结果:

Jest v24.9.0 node v10.16.3 linux/amd64
 jest --colors --config /home/runner/config.json
 PASS  ./add-test.js
  add
    ✓ should add two numbers (5ms)
  apihelpers
    ✓ getUnauth (57ms)

  console.log add-test.js:22
    foo/end/point/

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        2.788s
Ran all test suites.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-18
    • 2021-03-09
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2015-12-02
    • 2013-10-30
    相关资源
    最近更新 更多