Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock all except one function in a module

I am testing fileA.js, which requires fileB.js

In fileA.test.js, I wish to mock all methods from fileB.js except one.

In fileA.test.js I have:

const common = require("../src/fileB");
jest.mock("../src/fileB");

There is one method I do not wish to be mocked. Is this sort of thing possible in nodeJS?

Thank you.

like image 795
Alessandroempire Avatar asked Oct 25 '19 15:10

Alessandroempire


1 Answers

You can use jest.mock and jest.requireActual(moduleName) to partial mock the methods/functions of a module.

For example:

a.js:

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

exports.main = function main() {
  console.log(b.method1());
  console.log(b.method2());
  console.log(b.method3());
};

b.js:

module.exports = {
  method1: function() {
    return 'method 1';
  },

  method2: function() {
    return 'method 2';
  },

  method3: function() {
    return 'method 3';
  }
};

Now, we will mock all methods of b.js except method3.

a.spec.js:

jest.mock('./b', () => {
  const originalB = jest.requireActual('./b');
  const partialMockedB = Object.keys(originalB).reduce((pre, methodName) => {
    pre[methodName] = jest.fn();
    return pre;
  }, {});
  return {
    ...partialMockedB,
    method3: originalB.method3 // mock all methods of b except method3
  };
});

const { main } = require('./a');
const b = require('./b');

describe('main', () => {
  test('should correct', () => {
    const logSpy = jest.spyOn(console, 'log');
    b.method1.mockReturnValueOnce('mocked method 1');
    b.method2.mockReturnValueOnce('mocked method 2');
    main();
    expect(logSpy.mock.calls[0]).toEqual(['mocked method 1']);
    expect(logSpy.mock.calls[1]).toEqual(['mocked method 2']);
    expect(logSpy.mock.calls[2]).toEqual(['method 3']);
  });
});

Unit test result:

 PASS  src/stackoverflow/58561765/a.spec.js
  main
    ✓ should correct (18ms)

  console.log node_modules/jest-mock/build/index.js:860
    mocked method 1

  console.log node_modules/jest-mock/build/index.js:860
    mocked method 2

  console.log node_modules/jest-mock/build/index.js:860
    method 3

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.65s

Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58561765

like image 127
slideshowp2 Avatar answered Oct 13 '22 13:10

slideshowp2