简体   繁体   中英

jest mocking toHaveReturnedWith undefined

I'm learning unit testing with jest and particularly mocking modules. I wrote some simple module in math.js file with some math methods:

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:

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

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
      {
        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. My question is why? What I do wrong?

After using jest.mock('someModule') , jest will create an auto-mocked version for this module. This means the exported things from this module are all mocked.

You can think that the mocked math.add method is jest.fn() . There is no mock implementation for it. If no implementation is given, the mock function will return undefined when invoked. That's why the math.add(1, 2) returns undefined .

You are testing the math module, then you should NOT mock it. But if you insist to do it. You can use math.add.mockReturnValue(3); before invoke the 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. You just match your mock return value to the assertion expect(math.add).toHaveReturnedWith(3)

Eg 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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