Jest - Mocking a factory function

RootNode

Function I want to test is createCarAndDrive() which uses Car module. I'd like to mock one function in the Car module so its actual function is not called but the mock function.

So, the Car function returns two functions gas() and brake(). Car is implemented by using factory function pattern. So those two functions are wrapped in Car and won't be exposed until Car is called.

Is it somehow possible to mock the function brake() to return false?

Here's the implementation.

// Car.js
function Car({ name, model }) {
  function gas(speed) {
    return `${name} ${model} goes forward at a speed of ${speed}km/h`;
  }
  function brake() {
    return true;
  }

  return {
    gas,
    brake,
  };
}

// driver.js
function createCarAndDrive() {
  const car = Car({ name: 'Fiat', model: 'Punto' });
  car.gas(123);
  return car.brake();
}

// driver.test.js
describe('drive', () => {
  beforeEach(() => {
    // How to mock function inside a function?
    jest.mock('./Car', () => ({
      brake: jest.fn().mockImplementation(() => false),
    }));
  });

  test('it should not brake', () => {
    const itHitBreak = createCarAndDrive();
    expect(itHitBreak).toBe(false);
  });
});

Brian Adams

jest.mock factory functions don't work within test functions.

Move jest.mock to the top level scope of your test and it should work:

import { createCarAndDrive } from './driver';

jest.mock('./Car', () => ({
  Car: () => ({
    gas: () => 'mock gas',
    brake: () => false
  })
}));

describe('drive', () => {
  test('it should not brake', () => {
    const itHitBreak = createCarAndDrive();
    expect(itHitBreak).toBe(false);  // Success!
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mocking a function call in Jest

Mocking multiple named exports in jest.mock(moduleName, factory) factory function

Jest mocking function inside function

Node Jest mocking a function in a function

Specify function parameter in jest mocking

Mocking a function call inside a function in Jest

TypeError: XXXXXXX is not a function (While jest mocking)

Mocking a function that is called during file load in Jest

jest Mocking a jQuery function from a promise

Mocking module values that exports an anonymous function with Jest

Jest - Mocking a function that in turn has a method defined on it

Mocking up Firebase onValue function using Jest

Jest having trouble mocking specific function

Mocking promises and Timers in a recursive function with Jest

Jest mocking of the readOnly function from dependant file

Jest - Mocking a function call within the handleSubmit of a form

mocking a function inside a function and getting calls count in jest

Testing function calls in Jest after button click (mocking fetch calls)

Jest mocking request.res.setHeader always return error - not a function

Mocking simple internal function using Jest for use in rendered React component

Mocking shelljs with Jest - [TypeError: shell.exec is not a function]

Mocking a function within an express request for a Jest Unit test

How to assert a default exported function is called when mocking a module in Jest?

Mocking a Node Module which uses chained function calls with Jest in Node

JavaScript tests - mocking function from same module with jest

Mocking node_modules which return a function with Jest?

Mocking an object in Jest that has a function that makes an Axios call

Error Mocking Firebase Admin in Jest: "TypeError: admin.firestore is not a function"

Jest manual mocking works only for the first function call

TOP Ranking

HotTag

Archive