Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

E.g. 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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!