You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
📚 What are you trying to do? Please describe.
Hi,
how to mock injection?
injection was working fine, but its become undefined when running test.
Nuxt2 + Typescript + CompositionApi
I would mock the return value of useContext(). The exact approach will differ depending on the testing library you're using. My examples with be when using Vitest.
To mock it when it's used as a global:
constuseContextMock: Mock<any,any>=vi.fn().mockImplementation(()=>{return{$myFunc: (str: string)=>str}})vi.stubGlobal('useContext',useContextMock)// And to change the implementation for a specific testuseContextMock.mockImplementation(()=>{return{$myFunc: (str: string)=>`Changed - ${str}`}})
To mock it when it's imported directly:
import{useContext}from'@nuxtjs/composition-api'vi.mock('@nuxtjs/composition-api',()=>{return{useContext: vi.fn().mockImplementation(()=>{return{$myFunc: (str: string)=>str}}),}})// And to change the implementation for a specific testvi.mocked(useContext).mockImplementation(()=>{return{$myFunc: (str: string)=>`Changed - ${str}`}})
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
📚 What are you trying to do? Please describe.
Hi,
how to mock injection?
injection was working fine, but its become undefined when running test.
Nuxt2 + Typescript + CompositionApi
🔍 What have you tried?
Have mocks in options, still not works.
ℹ️ Additional context
The text was updated successfully, but these errors were encountered: