TypeError: Cannot read property 'getters' of undefined
See original GitHub issueHi,
I’m facing an issue here with TypeScript when I want to unit test my module.
My index.ts
for my store
:
import { StoreOptions } from 'vuex';
import { EndUsersModule } from './end-user-module';
const store: StoreOptions<{}> = {
modules: {
endUsers: EndUsersModule,
},
};
My end-user-module.ts
:
import { Action, Module, Mutation, VuexModule } from 'vuex-module-decorators';
import { EndUser } from './models';
import store from '@/shared/store'
@Module({
dynamic: true,
name: 'endUsers',
store,
})
export class EndUsersModule extends VuexModule {
private users: EndUser[] = [];
public get endUsers() {
return this.users;
}
@Action
public async createEndUser(user: EndUser) {
this.context.commit('createUserStart');
try {
await api.createUser(user);
this.context.commit('createUserSuccess', user);
} catch (err) {
this.context.commit('createUserFail', err);
}
}
@Mutation
public createEndUserFail(err: any) { /* implementation */ }
@Mutation
public createEndUserStart() { /* implementation */ }
@Mutation
public createEndUserSuccess(user: EndUser) { /* implementation */ }
}
Now, I want to test the action createEndUser
, but my problem is that when I do this at the top of my file:
import { EndUsersModule } from './end-user-module';
import { createModule } from 'vuex-module-decorators';
const endUsers = getModule(EndUsersModule);
describe('...', () => {
// ...
});
And run my tests, I see the following error message:
TypeError: Cannot read property 'getters' of undefined
Can you please tell me how I can unit test my actions, or the whole module for that matter?
I was also trying to do it with vuex-class
, but that also has its complications for actions. Then, I found your library, and I thought everything should be fine now, but that’s not the case 😦
Issue Analytics
- State:
- Created 5 years ago
- Comments:18 (5 by maintainers)
Top Results From Across the Web
vue.js - TypeError: Cannot read property 'getters' of undefined
When you declare the getter in the component, make sure to define the variable that is going to be used. @Component({ props: {...
Read more >Cannot read property 'getters' of undefined - Vue Forum
Hi, I'm trying to work with latest vue webpack template and I have an error in web browser console saying that: Cannot read...
Read more >Cannot read property 'getters' of undefined
Is this simply a case of case? new Vue({ store: Store, render: (h) => h(App), }). $mount('#app');
Read more >Cannot read property 'getters' of undefined · Issue #363
For jest testing with vuex, if vuex contains both namespaced modules and global actions or states, it doesn't work. It will produce Cannot...
Read more >TypeError: Cannot read property 'getters' of undefined
To solve the "Cannot read properties of undefined" error, make sure to insert the JS script tag at the bottom of the body....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just ran into this as well. Seems like there is no example on the internet on how to successfully unit test while using vuex-module-decorators.
These example repos even don’t have real tests scenario, my friend. Take a look yourself.