使用Jest创建的部分模块模拟函数不会被测试函数调用 [英] Partial module mock function created using Jest is not called by function under test

查看:635
本文介绍了使用Jest创建的部分模块模拟函数不会被测试函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(根据已经在此处提供的示例) 对于以下模块,

(Based on the examples kindly provided already here.) For the module below,

// fruit.js
export const apple = 'apple';

export const strawberry = () => 'strawberry';

export default () => `banana and ${strawberry()} `;

我想编写一个测试来验证 默认 导出的函数是否正确调用了函数 strawberry >. 为此,我尝试了以下测试,

I would like to write a test that will verify that the default exported function correctly invokes the function strawberry. To achieve this I attempted the following test,

// partial_mock.js
import defaultExport, { strawberry } from '../fruit';

jest.mock('../fruit', () => {
  const originalModule = require.requireActual('../fruit');
  const mockedModule = jest.genMockFromModule('../fruit');

  // Mock the exported 'strawberry' function.
  return Object.assign({}, mockedModule, originalModule, {
    strawberry: jest.fn(() => 'mocked strawberry'),
  });
});

it('does a partial mock', () => {
  expect(strawberry()).toBe('mocked strawberry');

  const defaultExportResult = defaultExport();
  expect(defaultExportResult).toBe('banana and mocked strawberry');
});

但是没有调用模拟函数,而是调用了实际函数.

However the mocked function is not called, instead, the actual function gets called.

 × does a partial mock (21ms)

  ● does a partial mock

    expect(received).toBe(expected) // Object.is equality

    Expected: "banana and mocked strawberry"
    Received: "banana and strawberry "

这是预期的吗?

我的考试有效吗?我考试中错过了什么吗?

Is my test valid? Have I missed anything in my test?

推荐答案

正在发生的事情是,当Jest导入fruit.js从中生成模拟时,默认导出中的匿名函数已经获取了对实际strawberry处于关闭状态.

What's happening is that by the time Jest imports fruit.js to generate a mock from it, the anonymous function in your default export has already grabbed a reference to the actual strawberry in its closure.

所以Jest所做的就是为以后导入它的任何其他模块模拟strawberry.您的测试套件.

So all Jest is doing is mocking strawberry for any other modules that import it later on, e.g. your test suite.

本文介绍了一些解决此问题的方法:

This article explains some ways to work around this: https://medium.com/@qjli/how-to-mock-specific-module-function-in-jest-715e39a391f4

我的建议是在采用任何变通办法之前考虑重构您的逻辑:

My suggestion is to consider refactoring your logic before employing any workarounds:

  • fruit.js中的某些功能是否用作实现细节或用于分解逻辑?也许您可以像对待私有类方法一样对待它们,并通过defaultExport本身对其进行测试.

  • Do some functions in fruit.js serve as implementation detail or used to factor out logic? Maybe you can treat them like private class methods and test them through defaultExport itself.

如果它们的逻辑是如此独立,以至于需要在测试之间进行模拟,那么这些功能应该驻留在单独的模块中吗?

If their logic is so independent that it needs to be mocked out between tests, maybe the functions should reside in separate modules?

这篇关于使用Jest创建的部分模块模拟函数不会被测试函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆