lundi 25 mars 2019

How to mock instantiated object with Jest

I have a file (src/dclient) that does this:

import DataClient from 'src/clients/data'

const DClient = new DataClient({ id: 'xxx' })
export default DClient

And I have a file (which I am trying to test) that does this:

import DClient from src/dclient

// Some code

DClient.alert('hello')

I am trying to write expectations on Dclient.alert but failing to do so. I have tried to set up the jest test as:

alertMock = jest.fn();
require('src/dclient').alert = alertMock

But this is not working when I check alertMock.mock.calls even though I know it has been called. I think because dclient returns an instance and actually doesn't have alert defined on it.

How can I set up this jest so I can write expectations on alert?

Aucun commentaire:

Enregistrer un commentaire