I am currently facing an issue with testing a namespaced Vuex module using Jest. Despite making mutations to the mocked state, I am not seeing any changes reflected.
Below is the code for my addEvents mutation:
addEvents: (state, infos) => {
try {
state.events = state.events.concat(infos);
} catch (error) {
console.error("[ERR vuex event set] ", e.message);
}
The test file is named event.test.js and contains the following code snippet:
import { createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
//store
import event from '../index.js'
const localVue = createLocalVue();
localVue.use(Vuex);
describe('event', () => {
//intial state
const state = {
events: []
}
const newEvent = {
text: 'Hello',
eventCode: 'importantEvent'
}
//mutate
event.commit('event/addEvents', newEvent);
expect(state.events).toContainEqual(newEvent)
})
})
I have ensured that I am importing from the correct store index which includes multiple modules. However, when attempting to import the specific module for testing, my mutations are not being found by the test.
Upon running Jest, the output displays the following error message:
FAIL vue/vuex/modules/event.test.js
● event › encountered a declaration exception
expect(received).toContainEqual(expected) // deep equality
Expected value: {"eventCode": "importantEvent", "text": "Hello"}
Received array: []
20 | //mutate
21 | event.commit('event/addEvents', newEvent);
> 22 | expect(state.events).toContainEqual(newEvent)
| ^
23 | })
at Suite.<anonymous> (vue/vuex/modules/event.test.js:22:24)
at Object.<anonymous> (vue/vuex/modules/event.test.js:9:1)