Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wrap a vue component during cypress component testing?

I am using component testing in Cypress on Vue. My project components use the vuetify plugin.

Currently, tested components load with Vuetify:

import DebuggingTemporaryComponent from "./DebuggingTemporaryComponent";
import {mount} from "@cypress/vue";
import vuetify from '../../resources/js/vuetify'

it('mounts the component with vuetify', () => {
    
    mount(DebuggingTemporaryComponent,{vuetify,})

    cy.contains('Hello World') ✅

}

However, the stylings are not functioning correctly because Vuetify components need to be wrapped in a <v-app> at least once on the page. In component testing this is not happening.

I need to customise the wrapper as suggested here in the docs for the React equivalent. However whenever I try to make my own function to do this, I get an error that the appropriate webpack loader isn't there. Vue loader is there and working.

import {mount as cypressMount} from '@cypress/vue'

export function mount (component){
    return cypressMount(<v-app>component</v-app>, prepareComponent(props, options))
}

Can anyone help me with where to go next with this?

like image 972
Rory Avatar asked Sep 03 '21 14:09

Rory


People also ask

How do you mount a component in Cypress?

Using cy.mount() in any component test without having to import the framework-specific mount command. You can customize cy. mount() to fit your needs. For instance, if you are using providers or other global app-level setups in your React app, you can configure them here.

What is Cypress Vue JS?

e2e-cypress plugin for vue-cli. This adds E2E testing support using Cypress. Cypress offers a rich interactive interface for running E2E tests in Firefox and Chromium based browsers (Chrome, MS Edge, Brave, Electron). To learn more about cross browser testing, visit the Cypress Cross Browser Testing Guide.

Is the Cypress component test runner the same as VUE test utils?

The mounting options are the same as Vue Test Utils, so if you've used Vue Test Utils before, you'll feel right at home. The Cypress Component Test Runner is an alternative to a jsdom based testing environment, such as Jest and Vue Test Utils.

What is the best way to run a component test in Cypress?

Jest is a great test runner and will stand up the mounted component using jsdom to simulate a browser environment. Cypress’ component test runner itself uses Vue Test Utils to mount Vue components, so the main difference between the two approaches is context. Cypress already runs end-to-end tests in a browser, and component tests work the same way.

Can I use @cypress with VUE 3?

Cypress will re-run your test (almost) instantly. This makes for a great red-green-refactor loop. Everything works the same with Vue 3. Just make sure you have a Vue 3 project and the correct adapter: The mount function exported by @cypress/vue has the same API for both Vue 2 and Vue 3.

How do I start writing component tests for Vue?

The quickest way to get started writing component tests for Vue is to use Vue's own project scaffolding tool. Follow the prompts to create your app. During the setup, you will be asked if you would like to install Cypress. You can do so now or in the next step. Open it and follow the Launchpad's prompts!


1 Answers

You can construct a simple wrapper in the test, for example

Component to test - Button.vue

<template>
  <v-btn color="red lighten-2" dark>
    Click Me
  </v-btn>
</template>

Test

import Button from "./Button";
import {mount} from "@cypress/vue";
import vuetify from '../plugins/vuetify'
import { VApp } from 'vuetify/lib/components'

const WrapperComp = {
  template: `
    <v-app>
      <Button />
    </v-app>
  `,
  components: {
    VApp,
    Button
  }
}

it('mounts the component with vuetify', () => {

  mount(WrapperComp, { vuetify })

  const lightRed = 'rgb(229, 115, 115)'
  cy.contains('button', 'Click Me')        // ✅
    .should('have.css', 'background-color', lightRed)  // fails if no <v-app> used above
})
like image 127
Fody Avatar answered Sep 21 '22 14:09

Fody