简体   繁体   English

开玩笑 mocking toHaveReturnedWith undefined

[英]jest mocking toHaveReturnedWith undefined

I'm learning unit testing with jest and particularly mocking modules.我正在用 jest 学习单元测试,尤其是 mocking 模块。 I wrote some simple module in math.js file with some math methods:我用一些数学方法在 math.js 文件中写了一些简单的模块:

const add      = (a, b) => a + b;
const subtract = (a, b) => b - a;
const multiply = (a, b) => a * b;
const divide   = (a, b) => b / a;

module.exports = {
    add,
    subtract,
    multiply,
    divide
}

Then I include it in my main js and I do module mocking like this:然后我将它包含在我的主要 js 中,然后像这样执行模块 mocking:

jest.mock('./math.js');
const math = require('./math');

test("calls math.add", () => {
    math.add(1, 2);

    console.log(math.add.mock);

    expect(math.add).toHaveBeenCalled();
    expect(math.add).toHaveBeenCalledWith(1, 2);
    expect(math.add).toHaveReturned();
    expect(math.add).toHaveReturnedWith(3);
});

Now when I run my test all tests are passing besides the last one:现在,当我运行我的测试时,除了最后一个之外,所有测试都通过了:

expect(math.add).toHaveReturnedWith(3);

In console I see:在控制台中我看到:

● calls math.add ● 调用 math.add

expect(jest.fn()).toHaveReturnedWith(expected)

Expected: 3
Received: undefined

Number of returns: 1

  10 |     expect(math.add).toHaveBeenCalledWith(1, 2);
  11 |     expect(math.add).toHaveReturned();
> 12 |     expect(math.add).toHaveReturnedWith(3);
     |                      ^
  13 | });

and console.log(math.add.mock) gives me this:和 console.log(math.add.mock) 给了我这个:

 console.log
      {
        calls: [ [ 1, 2 ] ],
        instances: [
          {
            add: [Function],
            subtract: [Function],
            multiply: [Function],
            divide: [Function]
          }
        ],
        invocationCallOrder: [ 1 ],
        results: [ { type: 'return', value: undefined } ],
        lastCall: [ 1, 2 ]
      }

So it seems that math.add mocked function does not return any value.所以看起来 math.add mocked function 没有返回任何值。 My question is why?我的问题是为什么? What I do wrong?我做错了什么?

After using jest.mock('someModule') , jest will create an auto-mocked version for this module.在使用jest.mock('someModule')之后,jest 将为此模块创建一个自动模拟版本。 This means the exported things from this module are all mocked.这意味着从这个模块导出的东西都是模拟的。

You can think that the mocked math.add method is jest.fn() .您可以认为模拟的math.add方法是jest.fn() There is no mock implementation for it.它没有模拟实现。 If no implementation is given, the mock function will return undefined when invoked.如果没有给出实现,模拟 function 将在调用时返回undefined That's why the math.add(1, 2) returns undefined .这就是math.add(1, 2)返回undefined的原因。

You are testing the math module, then you should NOT mock it.您正在测试math模块,那么您不应该模拟它。 But if you insist to do it.但如果你坚持要做。 You can use math.add.mockReturnValue(3);你可以使用math.add.mockReturnValue(3); before invoke the math.add(1,2) .在调用math.add(1,2)之前。 But it doesn't make sense, you can give any value you want.但是没有意义,你可以给任何你想要的值。 You didn't test the real math.add method.您没有测试真正的math.add方法。 You just match your mock return value to the assertion expect(math.add).toHaveReturnedWith(3)您只需将模拟返回值与断言expect(math.add).toHaveReturnedWith(3)

Eg math.js :例如math.js

const add = (a, b) => a + b;
const subtract = (a, b) => b - a;
const multiply = (a, b) => a * b;
const divide = (a, b) => b / a;

module.exports = {
  add,
  subtract,
  multiply,
  divide,
};

math.test.js : math.test.js

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

jest.mock('./math.js');

test('calls math.add', () => {
  math.add.mockReturnValue(3);
  math.add(1, 2);

  expect(jest.isMockFunction(math.add)).toBeTruthy();

  expect(math.add).toHaveBeenCalled();
  expect(math.add).toHaveBeenCalledWith(1, 2);
  expect(math.add).toHaveReturned();
  expect(math.add).toHaveReturnedWith(3);
});

Test result:测试结果:

 PASS  stackoverflow/71605818/math.test.js
  ✓ calls math.add (3 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |      20 |     100 |                   
 math.js  |     100 |      100 |      20 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.225 s

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

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