nuxt/test-utils

Expose mocks directly with `mockNuxtImport`

開放

#541 建立於 2023年8月18日

 (1 則留言) (11 個反應) (0 位負責人)TypeScript (115 個分叉)github user discovery
enhancementgood first issuevitest-environment

倉庫指標

星標
 (440 顆星)
PR 合併指標
 (平均合併 5天 23小時) (30 天內合併 28 個 PR)

描述

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.

貢獻者指南