Trying out the Nuxt, Vuex, Vuetify combo setup using Jest for testing

My day was consumed by addressing uncharted problems related to setting up testing for a combination of Nuxt, Vuex, and Vuetify with Jest.

The challenges I encountered included:

  • Error: Unknown custom element: <nuxt-link> - When running jest unit tests

  • Error: Cannot read property 'register' of undefined

  • [Vuetify] Warning: Multiple instances of Vue detected

Answer №1

After researching from various sources, I have curated the following setup which I am sharing here to help anyone looking to save themselves from a headache.

Effective Configuration:

// ./jest.config.js

module.exports = {
    // ... other configurations
     setupFilesAfterEnv: ['./test/jest.setup.js']
}
// ./test/jest.setup.js

import Vue from 'vue'
import Vuetify from 'vuetify'
import VueTestUtils from '@vue/test-utils'

Vue.use(Vuetify)

// Mocking Nuxt components
VueTestUtils.config.stubs['nuxt'] = '<div />'
VueTestUtils.config.stubs['nuxt-link'] = '<a><slot /></a>'
VueTestUtils.config.stubs['no-ssr'] = '<span><slot /></span>'
// ./test/Header.test.js

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

import Header from '~/components/layout/Header'

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

let wrapper

beforeEach(() => {
    let vuetify = new Vuetify()

    wrapper = mount(Header, {
        store: new Vuex.Store({
            state: { products: [] }
        }),
        localVue,
        vuetify
    })
})

afterEach(() => {
    wrapper.destroy()
})

describe('Header', () => {
    test('is fully operational', () => {
        expect(wrapper.element).toMatchSnapshot()
    })
})

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

Resizable table example: Columns cannot be resized in fixed-data-table

I've implemented a feature similar to the Facebook example found here: https://facebook.github.io/fixed-data-table/example-resize.html You can find my source code (using the "old" style with React.createClass) here: https://github.com/facebook/fixed- ...

Sharing a unique JSON message on Slack via a webhook

Is there a way to send a custom JSON message with indentation using a Slack webhook in a Node.js app? var Slack = require('slack-node'); var JsonMessage = process.argv[2]; webhookUri = "https://hooks.slack.com/services/XXXX/xxxx/xxxxxxxx"; sla ...

HTML generated in the header of a bootstrap-vue table

I'm currently developing a website using NuxtJS, Bootstrap Vue, and vue-i18n. Within the site, I have a table (<b-table>) that displays areas in square meters. The header of this table should show "sq m" in English and "m2" (m<sup>2</s ...

State management at its core with integrated functionalities

I'm exploring different ways to manage application state within Angular 18 using its built-in features. Would it be effective to start with a simple service that utilizes dependency injection (DI)? As someone new to Angular 18, I'm a bit confuse ...

Guide to modifying component properties in Storybook

Currently, I am experimenting with storybook (here) to test my components in isolation. My goal is to simulate the entire flux cycle (typically handled using redux in the full app) and modify a property by utilizing a simple object within the story. Howeve ...

PHP header malfunctioning post AJAX request triggered by JavaScript

Hey there, I have a query that might sound silly to some, but I'm curious if it's feasible to utilize the header function within a php file when receiving an AJAX response. In my scenario, I'm working on a login form where I use AJAX to com ...

Create interactive PDFs using HTML with embedded Javascript for a dynamic reading experience

Currently, I am working on creating a PDF from a basic html page that includes several charts generated in Javascript using Highcharts. We have ABCPdf installed to generate the PDF, but unfortunately it is not capturing any of the charts. After researchin ...

Node.js and Express: tackling the problem of duplicate variables

I have a checkUser middleware that saves the user information when a JWT is verified. However, when I integrate it into my routes and attempt to log res.locals.user.username, the username appears twice in the console. This duplication is causing issues wit ...

VueJS offers a mixin that is accessible worldwide

I'm looking to create a global mixin that is accessible everywhere, but not automatically inserted into every component. I don't want to use Vue.mixin({...}); Following the guidelines from this source, I have structured my project accordingly. Y ...

What is the best way to display a component based on the route using GatsbyJS?

Currently working with GatsbyJS and attempting to dynamically render different header components based on the URL route. For instance: mydomain.com/ should display HeaderLanding mydomain.com/blog should display HeaderMain Seeking guidance on how to imp ...

Unlock the powers of Express, Passport, and Redis sessions!

Lately, I have been utilizing the default MemoryStore for my Express sessions and everything has been running smoothly. However, I encountered a setback where all session data was lost between restarts. To address this issue, I am now attempting to configu ...

Encountering CORS Error while trying to access Guest App in Virtualbox using Vue, Express, and Axios

I encountered an issue while trying to access my Vue app in Virtualbox from the host, both running on Linux Mint 20. Although I can now reach the login page from my host, I am consistently faced with a CORS error during login attempts: Cross-Origin Request ...

Ajax: client dilemma created by the interaction of two functions

My university homework assignment requires me to develop a small e-commerce website. After logging in, the user will be directed to the homepage where they will receive a JSON object from the server containing product information to dynamically generate th ...

Styling the alert box with Bootstrap

My login error project utilizes the bootstrap alert to display errors. However, the issue I am facing is that when there is no error, it shows an empty box. Can someone help me figure out how to hide this div initially and only show it when an error occurs ...

Convert a date from the format of YYYY-MM-DD HH:MM:SS to MM-DD-YYYY using Javascript

Looking to transform YYYY-MM-DD HH:MM:SS into MM-DD-YYYY For instance: Given a date string in the format: 2013-06-15 03:00:00 The goal is to convert this string to 06-15-2013 using JavaScript. Is there a library available for this task, or should I rely ...

Retrieve weight measurements from a serial port by using Node JS

I'm a novice in node.js and I'm trying to retrieve weight data from a serial port. I have nodejs(v14.19.0) and npm(6.14.16) installed, and I'm attempting to obtain weight data using localhost:8080/get_weight. However, the script isn't f ...

A guide on how to access a Vue data property within another data property

I am delving into Vue.JS and facing an issue where every time I try to reference the dashboards property from the currentDashboard data expression, it returns 'dashboards is not defined'. Could it be possible that Vue is evaluating the currentDas ...

RegEx: determining the smallest sum of digits required in a character sequence

I'm trying to figure out a way to count the number of digits in a string that resembles a password. Currently, I am using this regular expression: ^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$ It works well when there are 3 consecutive digits, but not ...

Obtaining a substantial pdf file using html2pdf

While using html2pdf to generate a PDF of my website, I noticed that the downloaded PDF ends up being 14 pages long. However, after approximately 12 pages, all the colored elements seem to disappear. On mobile screens, this issue occurs even sooner, around ...

Determine the remaining days until the end of the month using moment.js

I need help figuring out how to dynamically display or hide a link based on whether there are less than two weeks remaining in the current month using moment.js. Currently, my code snippet looks like this: if (moment().endOf('month') <= (13, ...