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
📚 Is your documentation request related to a problem? Please describe.
I think it's a good idea to add some reference of how to write tests using the accessor. I had a few troubles setting up some unit tests. Check *additional context for the details.
🔍 Where should you find it?
A new page for Testing or just adding some tests to the current example projects
ℹ️ Additional context
This came up when i was trying to write some tests for actions and mutations without mounting a vue component or using @vue/test-utils. This way my tests will be focused on the operation of the store. The project I'm working on uses Nuxt and jest. In order to illustrate this case, here is a simplified version of the store structure and the actions and mutations:
Structure
- store
- index.ts
- submodule.ts
import*assubmodulefrom'./submodule'exporttypeRootState={// Some state}exportconststate=(): RootState=>({// Default state})exportconstmutations=mutationTree(state,{// Some mutations})exportconstactions=actionTree({ state, mutations },{asyncinitializeStore({ commit }): Promise<void>{// Some commitsthis.app.$accessor.submodule.initializeSubmodule()}})exportconstAccessorType=getAccessorType({
state,
mutations,
actions,modules: {
submodule
}})
submodule.ts
exporttypeSubmoduleState={// Some state}exportconststate=(): SubmoduleState=>({// Default state})exportconstmutations=mutationTree(state,{// Some mutations})exportconstactions=actionTree({ state, mutations },{asyncinitializeSubmodule({ commit }): Promise<void>{// Some commits}})
In order to test these files i decided to setup the store as if I weren't using Nuxt (like the setup described in the docs)
import*assubmodulefrom'../submodule'import{state,mutations,actions}from'../index'constpattern={
state,
mutations,
actions,modules: {
submodule
}}constlocalVue=createLocalVue()localVue.use(Vuex)conststore=newVuex.Store(pattern)constaccessor=useAccessor(store,pattern)describe('RootStore',()=>{describe('when initialize store',()=>{beforeAll(async()=>{awaitaccessor.initializeStore()})it('should setup general configuration',()=>{// Some assertions})})})
The problem with this configuration is that NuxtAppOptions will be undefined so when you try to reference another module within an action there will be an exception like this one: TypeError: Cannot read property '$accessor' of undefined . So in this example, the problem is in this line:
// TypeError: Cannot read property '$accessor' of undefinedthis.app.$accessor.submodule.initializeSubmodule()
After a quick research it seems you need to mock Nuxt global plugins as it's described here. However I think that's just too much for this case. My solution was simply mock the NuxtAppOptions and assign it the to the store:
conststore=newVuex.Store(pattern)asanyconstaccessor=useAccessor(store,pattern)// Trick to use accessor within actionsstore.app={$accessor: accessor}
Maybe it isn't the best solution but it works perfectly for this scenario. I hope this can be helpful.
The text was updated successfully, but these errors were encountered:
📚 Is your documentation request related to a problem? Please describe.
I think it's a good idea to add some reference of how to write tests using the accessor. I had a few troubles setting up some unit tests. Check *additional context for the details.
🔍 Where should you find it?
A new page for Testing or just adding some tests to the current example projects
ℹ️ Additional context
This came up when i was trying to write some tests for actions and mutations without mounting a vue component or using
@vue/test-utils
. This way my tests will be focused on the operation of the store. The project I'm working on uses Nuxt and jest. In order to illustrate this case, here is a simplified version of the store structure and the actions and mutations:Structure
submodule.ts
In order to test these files i decided to setup the store as if I weren't using Nuxt (like the setup described in the docs)
The problem with this configuration is that
NuxtAppOptions
will be undefined so when you try to reference another module within an action there will be an exception like this one:TypeError: Cannot read property '$accessor' of undefined
. So in this example, the problem is in this line:After a quick research it seems you need to mock Nuxt global plugins as it's described here. However I think that's just too much for this case. My solution was simply mock the
NuxtAppOptions
and assign it the to the store:Maybe it isn't the best solution but it works perfectly for this scenario. I hope this can be helpful.
The text was updated successfully, but these errors were encountered: