简体   繁体   English

从另一个模块开玩笑 mocking function:结果值未定义

[英]Jest mocking function from another module: results values are undefined

I'm practicing testing with Jest, in particular mocking functions.我正在练习使用 Jest 进行测试,特别是 mocking 函数。

I am testing a function that returns the doubled parameter.我正在测试返回双参数的 function。 I am testing this function by mocking it and mapping several parameters.我正在通过 mocking 测试这个 function 并映射几个参数。

here is the function that i am testing, it is located in the file ("./mock-func.js"):这是我正在测试的 function,它位于文件(“./mock-func.js”)中:

function doubling(n) {
  return n * 2;
}

exports.module = doubling;

here is the test (in another file):这是测试(在另一个文件中):

const mockFunc = require("./mock-func.js");

mockFunc.doubling = jest.fn();

test("map calls functions correctly", () => {
  const a = [10, 20, 30];

  a.map(mockFunc.doubling);

  expect(mockFunc.doubling.mock.calls.length).toBe(3);
  expect(mockFunc.doubling.mock.calls[0]).toEqual([10, 0, a]);
  expect(mockFunc.doubling.mock.calls[1]).toEqual([20, 1, a]);
  expect(mockFunc.doubling.mock.calls[2]).toEqual([30, 2, a]);
  expect(mockFunc.doubling.mock.results[0].value).toBe(20);
  expect(mockFunc.doubling.mock.results[1].value).toBe(40);
  expect(mockFunc.doubling.mock.results[2].value).toBe(60);
});

the problem that i have is that the test passes for the mock calls but not for the results values.我遇到的问题是模拟调用的测试通过了,但结果值没有通过。 It indicates that it returns undefined when the expected values are 20, 40, 60. I can't figure out why the mock calls pass but not the results values.它表明它在预期值为 20、40、60 时返回 undefined。我不明白为什么模拟调用通过但结果值不通过。

I am suspecting that it is because the function is located in another module because the test passes if i define the function in the test like so:我怀疑这是因为 function 位于另一个模块中,因为如果我在测试中定义 function ,则测试通过,如下所示:

const myMockFn = jest.fn(n => n * 2);

test("map calls functions correctly", () => {
  const a = [10, 20, 30];

  a.map(myMockFn);

  expect(myMockFn.mock.calls.length).toBe(3);
  expect(myMockFn.mock.calls[0]).toEqual([10, 0, a]);
  expect(myMockFn.mock.calls[1]).toEqual([20, 1, a]);
  expect(myMockFn.mock.calls[2]).toEqual([30, 2, a]);
  expect(myMockFn.mock.results[0].value).toBe(20);
  expect(myMockFn.mock.results[1].value).toBe(40);
  expect(myMockFn.mock.results[2].value).toBe(60);
});

any help/feedback would be appreciated!任何帮助/反馈将不胜感激! thanks!谢谢!

jest.fn() will create a mock function but it won't call the actual function. jest.fn()将创建一个模拟 function 但它不会调用实际的 function。 You can provide a mock Implementation like you have done in the scenario where the tests are passing.您可以提供一个模拟实现,就像您在测试通过的场景中所做的那样。

However, you should not be mocking the module that you are trying to test.但是,您不应该是您正在尝试测试的模块。

If you need to check the behavior of your function, it would be best to call the function itself and assert for the return value.如果您需要检查 function 的行为,最好调用 function 本身并断言返回值。

const res = doubling(20)
expect(res).toEqual(40)

Something like the above is a clean and crisp test for your module.像上面这样的东西对你的模块来说是一个干净而清晰的测试。 I'm not sure if there is a reason you are trying to check the arguments to the function but you need to call the actual function or provide a dummy implementation to get some values in the results.我不确定您是否有理由尝试检查 arguments 到 function 但您需要调用实际的 function 或提供一些值以获取结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM