score:1

use jest.mock mock the module. here is the working example:

util.js:

import modulename from './third_party_module';

const util = {
  simple_function() {
    const name = modulename.func1();
  }
};

export default util;

third_party_module.js:

export default {
  func1() {}
};

util.spec.js:

import util from './util';
import modulename from './third_party_module';

jest.mock('./third_party_module', () => ({
  func1: jest.fn()
}));

describe('was mocked functions called', () => {
  test('was mocked functions called??', () => {
    util.simple_function();
    expect(modulename.func1).tohavebeencalled();
  });
});

unit test result:

 pass  src/stackoverflow/54729837/util.spec.js (8.414s)
  was mocked functions called
    ✓ was mocked functions called?? (4ms)

test suites: 1 passed, 1 total
tests:       1 passed, 1 total
snapshots:   0 total
time:        9.742s

source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/54729837


Related Query

More Query from same tag