When using vitest, createTestPinia not working for stores outside .vue files #2463
-
In my project i have multiple stores, inside the components when i create a testPinia instance, they work fine, but at the same time i have som helper/hook functions, in a .js/.ts files. It seems that in that files pinia is not created so i get this error:
So the test doesn't work. I'm creating the pinia like:
Any idea? I can't find a solution. maybe not getting tableStorage as import and getting it from function parameter? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
In useStore, it gets the current vue component instance and can be used as such if used outside of .vue file in store index.ts import { createPinia } from 'pinia';
const pinia = createPinia();
export default pinia; in modules test.ts import pinia from './store';
export const useTestStore = defineStore('test', {
state: (() => {
return {
a:1
}
})
})
// Allow access in a non-component state
export function useAppOutsideStore() {
return useTestStore(pinia);
} In xxx.ts import { useAppOutsideStore } from './store'
const test = useAppOutsideStore()
console.log(test.a) // 1 |
Beta Was this translation helpful? Give feedback.
-
You are likely using the store in an incorrect place. See https://pinia.vuejs.org/core-concepts/outside-component-usage.html#Using-a-store-outside-of-a-component |
Beta Was this translation helpful? Give feedback.
You are likely using the store in an incorrect place. See https://pinia.vuejs.org/core-concepts/outside-component-usage.html#Using-a-store-outside-of-a-component