nuxt/test-utils

Expose mocks directly with `mockNuxtImport`

Ouverte

#541 ouverte le 18 août 2023

 (1 commentaire) (11 réactions) (0 personne assignée)TypeScript (115 forks)github user discovery
enhancementgood first issuevitest-environment

Métriques du dépôt

Stars
 (440 étoiles)
Métriques de merge PR
 (Merge moyen 5j 23h) (28 PRs mergées en 30 j)

Description

Currently, it's possible to mock a Nuxt import:

mockNuxtImport('useStorage', () => {
    return vi.fn()
})

When we want to access this mock in our test, to run an assertion or to change the implementation, we can use vi.hoisted:

const { useStorageMock } = vi.hoisted(() => {
  return {
    useStorageMock: vi.fn()
  }
})

mockNuxtImport('useStorage', () => {
  return useStorageMock
})

// now we can change the implementation of useStorageMock between tests

This is a lot of code just to define and expose a single mock. Would it be possible to make mocking Nuxt imports easier by returning the mocks directly in mockNuxtImport?

I propose something like this:

const useStorageMock = mockNuxtImport('useStorage', () => {
    return vi.fn()
})

// now we can change the implementation of useStorageMock between tests

I'm down to have a look at this if this is possible.

Guide contributeur