Component test with with Vitest throws [Vue warn]: injection "Symbol(pinia)" not found.
#1100
-
Reproductionpackages used:
tests/MyComponent.spec.ts import { describe, it, expect, fn } from 'vitest'
import { createTestingPinia } from '@pinia/testing'
import { mount } from '@vue/test-utils'
import MyComponent from '../MyComponent.vue'
describe('MyComponent', () => {
// Create store
const pinia = { global: { plugins: [ createTestingPinia({ createSpy: fn }) ] }}
it('rendery properly', () => {
const wrapper = mount(MyComponent, { pinia, props: {} })
expect(wrapper.text()).toBe('Hello World')
})
}) Steps to reproduce the behavior
Expected behaviorI expect no errors thrown. Actual behaviorIssue is similar to #629, but is not solved since I am using vue 3. The test runs, and it passes. But it keeps showing those error messages:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
It's const wrapper = mount(MyComponent, { global: { plugins: [ createTestingPinia({ createSpy: fn }) ] }, props: {} }) If you don't create a pinia for each test, each test will share the same state object, ending up in unexpected problems. My recommendation is to wrap the whole describe('my test', () => {
function factory(propsOrOptions) {
return mount(MyComponent, { global: { plugins: [ createTestingPinia({ createSpy: fn }) ] }, props: {} })
}
}) Adapt the argument to pass options to the component |
Beta Was this translation helpful? Give feedback.
-
The following code worked for me: import { createPinia } from 'pinia';
import MyComponent from './MyComponent.vue';
appWrapper = mount(MyComponent, { global: { plugins: [ createPinia() ] } } ); |
Beta Was this translation helpful? Give feedback.
-
If you use pinia in multiple places, that worked form me: const pinia = createPinia()
setActivePinia(pinia)
mount(Component, {
global: {
plugins: [ pinia ],
},
} |
Beta Was this translation helpful? Give feedback.
It's
If you don't create a pinia for each test, each test will share the same state object, ending up in unexpected problems. My recommendation is to wrap the whole
mount()
call within a function:Adapt the argument to pass options to the component