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?
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.
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.
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.
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.
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.
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!
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
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With