Blog>
Snippets

End-to-End Testing with Cypress

Provide an example setup for Cypress in a Vue.js 3 application to carry out end-to-end testing, ensuring the application flow works as expected.
// cypress/plugins/index.js
const { startDevServer } = require('@cypress/webpack-dev-server')
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('dev-server:start', async (options) => startDevServer({ options }))
      return config
    },
  },
})
This snippet of code sets up Cypress plugins for a Vue.js 3 application. It hooks into the 'dev-server:start' event, starting the development server using Cypress's built-in method startDevServer, which allows Cypress to serve the Vue application for testing.
// cypress/support/commands.js
// Custom commands to add to Cypress, if any, go here.
This file is where custom Cypress commands can be defined which can enhance or simplify the test code. For this Vue.js application, no custom commands are added right now.
// cypress/support/index.js
import './commands'

// This file runs before each Cypress test. You can set global hooks or configuration here.
Every time Cypress runs, it will load this file after loading the browser. It sets up the testing environment by importing custom commands or global configurations.
// sypress.json or cypress.config.js
{
  "baseUrl": "http://localhost:8080",
  "integrationFolder": "cypress/integration",
  "supportFile": "cypress/support/index.js",
  "fixturesFolder": "cypress/fixtures",
  "videosFolder": "cypress/videos",
  "screenshotsFolder": "cypress/screenshots"
}
This is the Cypress configuration file. It specifies the base URL for the tests corresponding to where the Vue.js app is running locally (e.g., localhost:8080) and directories where tests, fixtures, and other relevant files are located.
// cypress/integration/app.spec.js

describe('App E2E', () => {
  it('Visits the app root url', () => {
    cy.visit('/')
    cy.contains('h1', 'Welcome to Your Vue.js App')
  })

  // Add more tests to verify app behavior
})
This is an example of a test file where end-to-end tests would be located for the Vue.js application. It currently includes a simple test that visits the root URL of the Vue app and checks for an h1 tag containing a specific text.