
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
vuex-mock-store
Advanced tools
Simple and straightforward mock for Vuex v3.x Store
Supports using mapMutations
and mapActions
as well as directly doing this.$store.commit()
and this.$store.dispatch()
npm install -D vuex-mock-store
# with yarn
yarn add -D vuex-mock-store
ℹ️: All examples use Jest API. See below to use a different mock library.
Usage with vue-test-utils:
Given a component MyComponent.vue
:
<template>
<div>
<p class="count">{{ count }}</p>
<p class="doubleCount">{{ doubleCount }}</p>
<button class="increment" @click="increment">+</button>
<button class="decrement" @click="decrement">-</button>
<hr/>
<button class="save" @click="save({ count })">Save</button>
</div>
</template>
<script>
import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
export default {
computed: {
...mapState(['count']),
...mapGetters(['doubleCount']),
},
methods: {
...mapMutations(['increment', 'decrement']),
...mapActions(['save']),
},
}
</script>
You can test interactions without relying on the behaviour of your actions and mutations:
import { Store } from 'vuex-mock-store'
import { mount } from '@vue/test-utils'
import MyComponent from '@/components/MyComponent.vue'
// create the Store mock
const store = new Store({
state: { count: 0 },
getters: { doubleCount: 0 },
})
// add other mocks here so they are accessible in every component
const mocks = {
$store: store,
}
// reset spies, initial state and getters
afterEach(() => store.reset())
describe('MyComponent.vue', () => {
let wrapper
beforeEach(() => {
wrapper = mount(MyComponent, { mocks })
})
it('calls increment', () => {
wrapper.find('button.increment').trigger('click')
expect(store.commit).toHaveBeenCalledOnce()
expect(store.commit).toHaveBeenCalledWith('increment')
})
it('dispatch save with count', () => {
wrapper.find('button.save').trigger('click')
expect(store.dispatch).toHaveBeenCalledOnce()
expect(store.dispatch).toHaveBeenCalledWith('save', { count: 0 })
})
})
⚠️ The mocked dispatch
method returns undefined
insteadf of a Promise. If you rely on this, you will have to call the appropriate function to make the dispatch
spy return a Promise:
store.dispatch.mockReturnValue(Promise.resolve(42))
If you are using Jest, you can check the documentation here
You can provide a getters
, and state
object to mock them:
const store = new Store({
getters: {
name: 'Eduardo',
},
state: {
counter: 0,
},
})
To mock module's state
, simply provide a nested object for state
:
new Store({
state: {
moduleA: {
moduleC: {
value: 'foo',
},
},
moduleB: {
value: 'bar',
},
},
})
To mock module's getters
, provide the correct name when namespaced:
new Store({
getters: {
'namespaced/getter': 'value',
notNamespaced: 'value',
},
})
Testing actions and mutations depend whether your modules are namespaced or not. If they are namespaced, make sure to provide the full action/mutation name:
// namespaced module
expect(store.commit).toHaveBeenCalledWith('moduleA/setValue')
expect(store.dispatch).toHaveBeenCalledWith('moduleA/postValue')
// non-namespaced
expect(store.commit).toHaveBeenCalledWith('setValue')
expect(store.dispatch).toHaveBeenCalledWith('postValue')
state
, providing custom getters
You can modify the state
and getters
directly for any test. Calling store.reset()
will reset them to the initial values provided.
Store
classconstructor(options)
options
state
: initial state object, default: {}
getters
: getters object, default: {}
spy
: interface to create spies. details belowstate
Store state. You can directly modify it to change state:
store.state.name = 'Jeff'
getters
Store getters. You can directly modify it to change a value:
store.getters.upperCaseName = 'JEFF'
ℹ️ Why no functions?: if you provide a function to a getter, you're reimplementing it. During a test, you know the value, you should be able to provide it directly and be completely sure about the value that will be used in the component you are testing.
reset
Reset commit
and dispatch
spies and restore getters
and state
to their initial values
By default, the Store will call jest.fn()
to create the spies. This will throw an error if you are using mocha
or any other test framework that isn't Jest. In that situation, you will have to provide an interface to create and reset spies. This is the default interface that uses jest.fn()
:
new Store({
spy: {
create: () => jest.fn(),
reset: spy => spy.mockRestore(),
},
})
If you use Jest, you don't need to do anything. If you are using something else like Sinon, you could provide this interface:
import sinon from 'sinon'
new Store({
spy: {
create: () => sinon.spy(),
reset: spy => spy.restore(),
},
})
commit
& dispatch
Spies. Dependent on the testing framework
FAQs
Simple and straightforward mock for Vuex v3.x Store
The npm package vuex-mock-store receives a total of 82,204 weekly downloads. As such, vuex-mock-store popularity was classified as popular.
We found that vuex-mock-store demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.