What could be causing the "TypeError: Unable to convert a Symbol value to a string" message to appear while trying to clone the Vuex store in a Jest unit test?

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.

Answer №1

(Let's dive into another installment of "Dave's quest to solve a seemingly trivial issue after spending way too much time on it...but hey, maybe it will help someone else out in the future):

I made some changes to the structure of my Vuex store:

export const storeConfig = {
 state: {
  index: 0,
  // ...
 },
 getters: {
  currentView(state) {
   // Function code ...
  }
 mutations: {
  // code
 },
 actions: {
  // code
 }
};

const store = new Vuex.Store(storeConfig);

export default store;

All I had to do was make a small adjustment to my Jest test:

// import store from './store'
// updated to:
import { storeConfig } from './store'
})

And just like that, the test now passes without any problems.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using React dangerouslySetInnerHTML with iframes

I am facing an issue with a component that displays a WordPress blog post. The post includes HTML markup as well as YouTube embeds using an iframe element. However, dangerouslySetInnerHTML is removing the iframes. How can I ensure this type of content is ...

What is the process of creating the /dist folder in an unreleased version of an npm package?

Currently working on implementing a pull request for this module: https://github.com/echoulen/react-pull-to-refresh ... It seems that the published module generates the /dist folder in the package.json prepublish npm script. I have my local version of the ...

Store vueJs data in browser's localStorage

Is there a way to save the state of my game even after a page refresh using local or session storage? I've successfully stored wins in localStorage, but I'm struggling to keep the table with "X" and "O" in the same spot after a refresh. Any sugge ...

What sets apart a static website from a dynamic website that have both been created through coding?

Can you clarify the distinction between a static and dynamic website built using html/css/js code? Could you provide some examples to illustrate this difference? ...

The background on my modal remains unchanged

I have integrated a modal using Iframe for user login/registration on my website. Here is the HTML code: <div class="modal" id="ays-popin-connexion" aria-hidden="true"> <div class="modal-body" aria-hidden="true"> <iframe src=" ...

Conceal a Component within an Embedded Frame

Hey there! I've been attempting to hide a header within an iframe, but it seems like my code isn't doing the trick. Could someone take a look and help me diagnose why the header is still visible? Thanks in advance! <iframe id="booking_iframe" ...

Using JavaScript code in ReactJS does not produce the desired results

Just starting out with react and could use a little guidance. I came across this interesting hamburger menu icon on codepen: https://codepen.io/rss/pen/OJxZrR The challenge I'm facing is getting the animation to function properly. import "../.. ...

Having difficulty retrieving the value of a variable obtained from the Google Distance Matrix function

Utilizing the Google distance matrix API to calculate the distance between two locations, I encountered an issue with global variable access. Despite changing the variable within a function, I found that I was unable to retrieve the updated value of the va ...

What is the best way to trigger an onclick event to call another function?

I am having trouble triggering an onclick function when a button on my site (with the id "rum_sst_tab") is clicked. I also need to execute another function that tracks a google conversion using gtag_report_conversion(). I attempted various jQuery options ...

What is the most efficient method for storing dynamically generated data in React and JavaScript?

I have been experimenting with my dynamic form to enhance its functionality and efficiency. I am currently pondering on the optimal method for saving data that is generated through this form. While my current solution is functional, I believe there might ...

I'm experiencing difficulties with app.use() not functioning in my specific case, even though the paths are identical

My goal is to transfer the dist folder from the server to the client. When I use the following command, it works perfectly: app.use('/', express.static( Path.join(__dirname, '..', 'node_modules', 'del-js-webapp', &a ...

I am attempting to implement an Express static middleware as demonstrated in this book, but I am having trouble understanding the intended purpose of the example

I'm currently studying a chapter in this book that talks about Express, specifically concerning the use of express.static to serve files. However, I'm encountering an issue where the code catches an error when no file is found. I've created ...

Tips on using the map and filter methods to narrow down a nested array based on specific object

I am struggling to filter JSON data based on a specific date using Array.Filter and Map. The code I have tried below is not yielding the desired results. Can someone please provide guidance on how to effectively filter JSON data based on a particular date ...

"The error message 'handlebars.engine is undefined' pops up when working with NodeJs, Express,

I've been diving into the teachings of Ethan Brown's "Web Development with Node & Express" from O'Reilly. In this guide, they recommend using handlebars as the view engine. Check out the code snippet I've implemented: var express = r ...

The contents of the div disappear when using jQuery to extract it from a string

Update: I finally uncovered the reason behind the empty content of the #output div. The content is fetched from the server, which takes some time; by the time the document loads, the div remains empty. Does anyone have suggestions on how to extract infor ...

Getting Familiar with Vue3 Router: Exploring the Application of beforeRouteEnter in a Child Route

Is there a way to incorporate an optional feature on the main route that would display a detailed page based on the provided id in the route parameters? If the id does not match any object, I want to redirect the user back to the main route. I attempted to ...

Angular UI grid: Arranging numbers in a straight line at the decimal point

I am interested in aligning decimal numbers in Angular UI Grid as shown below. 11.293 .89 233424 .34345 I have considered different approaches such as using a cell template with aligned divs or transparent 0s. Has anyone successfully imp ...

Modify the color of cells containing prime numbers within a dynamic table generated using JavaScript

I have successfully created a code to generate a table with a user-defined number of cells. I also want the cells containing prime numbers to be colored differently. I've written a function to find prime numbers, but I'm having trouble integratin ...

The JavaScript library known as Microsoft JScript Angular is not recognized

While integrating Angular into my .Net MVC website, I keep running into a runtime error that reads as follows: 0x800a1391 - Microsoft JScript Runtime Error: 'angular' is undefined. This essentially means that the 'angular' object ...

What is the best way to access data from a static config.json file in TypeScript within a Vue component following the execution of a build:electron command in Vue CLI 3?

Hey there! I've been considering whether it's feasible to include a config.json file in a Vue CLI 3 project that can be dynamically read at runtime, both during development and production stages. This config.json file will hold key strings that ...