I recently had to add unit tests to my Vue 2.6 / Vuex 3.6 / TypeScript application. Before diving into some complex refactoring, I wanted to ensure the existing functionality was covered by tests. After setting up Jest and Vue Test Utils according to the official guide, I adapted the instructions to fit my project like this:
import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vue'
import store from 'store'
import { cloneDeep } from 'lodash'
test("SET_CURRENT_VTK_INDEX_SLICES should update the VTK index slices", () => {
const localVue = createLocalVue()
localVue.use(Vuex)
const store = new Vuex.Store(cloneDeep(storeConfig))
expect(store.state.iIndexSlice).toBe(0)
store.commit('SET_CURRENT_VTK_INDEX_SLICES', { indexAxis: 'i', value: 1 })
})
However, when running npm run test:unit
, I encountered an error message stating:
"TypeError: Cannot convert a Symbol value to a string"
Despite not finding any symbols in the store upon inspection using a recursive function, I went down various paths trying to identify the issue:
function findSymbolInStore(store) {
for (const key in store) {
console.log(key);
if (store.hasOwnProperty(key)) {
const value = store[key];
if (typeof value === 'object') {
if (value instanceof Symbol) {
console.log(`Symbol found: ${key}`);
} else {
findSymbolInStore(value);
}
}
}
}
}
findSymbolInStore(store.state);
After encountering errors with attempts to stringify the store, including circular structure issues, using flatted
seemed to make progress but led to another error about undefined properties.
It was ultimately pointed out that the entire store.state
was undefined, prompting me to revisit how the Vuex store was defined and exported:
const store = new Vuex.Store({
state: {
iIndexSlice: 0,
// ...
},
getters: {
currentView(state) {
// Function code ...
}
mutations: {
// code
},
actions: {
// code
}
});
export default store;
Special thanks to Mujeeb for assistance with debugging involving symbols. Hopefully, sharing these challenges can help others facing similar obstacles in their testing process.