3

In spite of my understanding that NUXT does namespacing automatically. Because of this, I am unable to test or reference the store in any of my testing modules. Can anyone give me a tip? Maybe where I can edit the namespacing property in a Nuxt app?

Here is the code below for the component, store, and the test.

ButtonComponent.vue:

<template>
  <v-container>
    <v-btn @buttonClick v-model="value"></v-btn>
  </v-container>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  data: {
      return {
          value: 25
      }
  }
  methods: {
    buttonClick(event) {
      this.$store.dispatch('buttonComponent/setNewValue', valuePassedIn)
    },
  },
}
</script>

<style scoped></style>

buttonComponent.spec.js:

import Component from '../../Component'
import { mount, createLocalVue } from '@vue/test-utils'
import expect from 'expect'
import Vue from 'vue'
import Vuex from 'vuex'
import Vuetify from 'vuetify'

const localVue = createLocalVue()
localVue.use(Vuex)
Vue.use(Vuetify)

describe('Component', () => {
  let store
  let vuetify
  let actions
  beforeEach(() => {
    actions = {
        actionClick: jest.fn()
    }
    store = new Vuex.Store({
      actions,
    })
    vuetify = new Vuetify()
  })



  it('method sends value to store when button is clicked', async () => {
    const wrapper = mount(Component, {
      store,
      localVue,
      vuetify,
    })
    wrapper.find('.v-btn').trigger('click')
    expect(actions.actionClick).toHaveBeenCalledWith('buttonComponent/setNewValue', 25)
  })
})

buttonComponent.js:

export const state = () => ({
  value: 0,
})

export const mutations = {
  SET_TO_NEW_VALUE(state, value) {
    state.value = value
  },
}

export const actions = {
  setNewValue({ commit }, value) {
    commit('SET_TO_NEW_VALUE', value)
  },
}

1 Answer 1

1

Just so that I don't have to write it again here, I'll link you to an article I just posted that walks through the setup process to so you can test your Nuxt stores with Jest: https://medium.com/@brandonaaskov/how-to-test-nuxt-stores-with-jest-9a5d55d54b28

Sign up to request clarification or add additional context in comments.

2 Comments

It takes about 5 seconds to run 1 test on my machine using the nuxt builder. A better option is to mock the nuxt store.
@ioan mocking behavior within your own codebase is a terrible idea. There should have been a better way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.