samedi 17 avril 2021

How to test Reac Lazy inside an object

At the project I'm working on, there's a few named exports like the object below:

export const object = {
    component: React.lazy(() => import('some-component'))
}

(They have more properties but I'll just show this one for brevity)

The issue

When I try to test it using Jest, I can't test inside React.lazy:

Screenshot of coverage report

I tried mocking React.lazy with no success:

jest.mock('react', () => ({
  lazy: () => {
    return 'lazy'
  },
}))

describe('Auth route object', () => {
  it('', () => {
    expect(auth).toBeDefined()
    expect(auth[0].component).toBe('lazy')
  })
})

This returns passed but it keeps complaining about coverage.

How can I mock/test that?

ps: I need at least 90% coverage on this file, that's why I can't just ignore it.

Aucun commentaire:

Enregistrer un commentaire