mercredi 23 octobre 2019

how to mock a named import in jest?

I used to have a set up like so:

export class MyClass {
}
export default new MyClass()

then I would do: import myclass from 'libraries/myclass'

now I change it to:

export const myclass = new MyClass()

and importing like so import { myclass } from 'libraries/myclass' which seems to work in my code

however all my jest tests are failing

I'm currently doing:

jest.mock('libraries/myclass', () => ({
    myclassfunction: jest.fn(),
}))

I've tried changing to:

jest.mock('./myclass.js', () => (
  {
    ...(jest.requireActual('./myclass.js')),
    myclassfunc: () => {}
  }
))

but it still fails and when I console.log(myclass) it is coming through as undefined

Aucun commentaire:

Enregistrer un commentaire