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.

How to test components using getModule with vue-test-util

See original GitHub issue

How would you go about stubbing actions and other methods inside of your store when using the getModule functionality inside of a component for testing?

MyComponent.vue

<template>
  <div>
    {{ this.exampleStore.someData  }}
    <button v-on:click="handleClick()">Do An Action</button>
    <LoadingIcon
      :v-if="this.exampleStore.isLoading"
    ></LoadingIcon>
  </div>
</template>

<script lang="ts">
// imports.....

@Component({
  components: {
    LoadingIcon
  }
})
export default class MyComponent extends Vue {
  private exampleStore: ExampleStore = getModule(ExampleStore)
  
  private created() {
   this.exampleStore.fetchSomeData()
  }

  private handleClick() {
    this.exampleStore.fetchSomeData()
  }
}
</script>

ExampleStore.ts

// imports...
@Module({ dynamic: true, namespaced: true, store, name: 'exampleStore' })
export default class ExampleStore extends VuexModule {
  public someData: any = ''

  @Action({ commit: 'someMutation')}
  public async fetchSomeData() {
  // async stuff

   return data
  }

  @Mutation
  public someMutation(payload: any) {
    return this.someData = payload
  }
}

Test

const localVue = createLocalVue()
localVue.use(Vuex)
let store: Vuex.Store<any>

beforeEach(() => {
  store = new Vuex.Store({})
})

describe('test component', () => {
  it('should have the correct html structure', () => {
    const component = shallowMount(MyComponent, {
     store, localVue
    } as any)
    expect(component).toMatchSnapshot()
  })
})

In the above example I would need to stub the fetchSomeData action

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:22

github_iconTop GitHub Comments

8reactions
Robin-Hoodiecommented, Nov 11, 2020

Is there still not a better solution or any documentation related to this ? We’re nearing 2021 and this issue is so far still the best source of documentation I can find on testing dynamic Vuex modules.

2reactions
jubairsaidicommented, Sep 21, 2019

sorry for the delay! been a bit busy 😕

so lets assume you use getModule(FooModule, this.$store) from inside a component computed property like

get fooModule() {
  return getModule(FooModule, this.$store)
}

then all you have to do is override fooModule in your computed attributes when creating your wrapper, i.e.

    const wrapper = shallowMount(FooComponent, {
      computed: {
        fooModule: () => ({}),
      },
      localVue,
    });

You can also return your own mock version of the store if you like. personally I don’t like testing store logic in my components so I override it with an empty object, and mock the other getters that I use to access the store getters, and methods used to access the store mutations and actions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Test A Vuex Module Action Is Called In Vue ... - ADocLib
One approach to testing a Vuex store is to test the store parts separately.The benefit of testing parts separately is that the unit...
Read more >
Using with Vuex - Vue Test Utils
Using with Vuex. In this guide, we'll see how to test Vuex in components with Vue Test Utils, and how to approach testing...
Read more >
How to use Jest with vuex-module-decorators - Stack Overflow
My code works well but when i try to write a unit test using vue-test-utils and jest i'm not able to inject my...
Read more >
Things You Might Not Know About Vue-Test-Utils - WebDevEtc
Another common thing to see in tests which use nextTick() after a trigger() : async function someTest() { wrapper = mount(Component); ...
Read more >
Top 5 @vue/test-utils Code Examples - Snyk
Learn more about how to use @vue/test-utils, based on @vue/test-utils code examples ... Vue }); // same component, no props, should match in...
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