mardi 29 décembre 2020

Mocking function inside another function using jest

I'd like to mock a function which called by another function using jest but it doesn't work. The mock function is never been called by the parent function. Could you help me please. Thanks in advance.

app.js

const evaluate = (a, b) => {
  return 'Sum is ' + add(a, b);
}

const add = (a, b) => {
  return a + b;
}

exports.evaluate = evaluate;
exports.add = add;

test.js

const evaluateService = require("./app");

describe('Call jumpRequest', () => {

  it('should return Evaluate', async () => {

    evaluateService.add = jest.fn().mockReturnValue(12);
    const res = await evaluateService.evaluate(5, 3);
    expect(res).toEqual("Sum is 12");
 })
})

Aucun commentaire:

Enregistrer un commentaire