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.

贡献者指南