Skip to content

Expose mocks directly with mockNuxtImport #541

Open

Description

@rinux55

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.

Activity

  1. danielroe commented on Aug 18, 2023

    @danielroe
    Member

    It seems this should be possible to implement in the same plugin that processes/handles mockNuxtImport.

  2. added
    staleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed
    on Apr 24, 2024
  3. removed
    staleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed
    on May 1, 2024
  4. devsummar commented on Oct 18, 2026

    @devsummar

    Hi, I'd like to pick this up. Following the suggested approach, I'll expose the mocks returned from mockNuxtImport so they can be accessed directly in tests, with tests and docs. Could this be assigned to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions