samedi 11 mars 2017

Mocking a node_module imported by the function under test

I am using Jest to test a module consisting of a single function that uses an imported third party lib (from node_modules). The third party lib performs a time-consuming asynchronous action, so I need to mock it.

A much simplified version of the function looks like this:

import SomeLib from 'some-lib'

const example = (alpha, beta) => {
  const o = new SomeLib(alpha, beta);
  ...
  return o.doSomething();
}

export example

I've read through Jest's mocking docs looking for information related to mocking third party node_modules, but their examples all seem to presuppose that the mocked module is passed in to the function under test, allowing it to be easily mocked. Given that I don't (and cannot) pass in the third part dependency, how should I be mocking it using jest.

Aucun commentaire:

Enregistrer un commentaire