question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'getters' of undefined

See original GitHub issue

Hi,

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:open
  • Created 5 years ago
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
Pitacommented, Nov 14, 2019

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.

5reactions
aislanmaiacommented, Nov 19, 2019

I mean these my friend,

image

These are actual projects

These example repos even don’t have real tests scenario, my friend. Take a look yourself.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found