Vue.js course ยท Module 12: Testing & Deploy
Integration & E2E Testing
In this lesson5
Integration tests verify how systems work together - just like rocket systems must cooperate before launch to Mars.
Every subassembly of the panel has already passed its trials in isolation: the counter, the composable, the archive store. That still does not guarantee they will work together. A component may read a field from the store under a different name than the store exposes, and the router may fail to render the view a link points to. No unit test will catch such faults, because they only appear at the joints. That is why we now put the subassemblies back together.
Integration versus unit tests
A unit test checks a single unit in isolation, and an integration test checks how many parts work together: a component with a store, a component with a router, a parent with its children. This is where the difference between two Vue Test Utils functions matters. mount() renders the full component tree together with the children, while shallowMount() replaces the children with stubs, which look like <child-stub> in the HTML. In an integration test you use mount(), because the children must really work.
Component Integration Tests
The first test checks whether the gallery displays data from a Pinia store. The global.plugins option installs plugins in the test application the same way app.use() does in main.js:
// ArtworkGallery.spec.js
import { describe, it, expect, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import ArtworkGallery from './ArtworkGallery.vue'
import { useGalleryStore } from '@/stores/gallery'
describe('ArtworkGallery Integration', () => {
beforeEach(() => {
setActivePinia(createPinia())
})
it('displays artworks from store', async () => {
const wrapper = mount(ArtworkGallery, {
global: {
plugins: [createPinia()]
}
})
const store = useGalleryStore()
store.artworks = [
{ id: 1, title: 'Mona Lisa', artist: 'Leonardo' },
{ id: 2, title: 'David', artist: 'Michelangelo' }
]
await wrapper.vm.$nextTick()
const cards = wrapper.findAll('.artwork-card')
expect(cards).toHaveLength(2)
})Installing Pinia through global.plugins makes that instance the active one, so useGalleryStore() in the test and in the component reach the same store. After changing the data we wait for $nextTick(), because Vue re-renders the DOM asynchronously. findAll returns a list of matching elements, and toHaveLength(2) checks how many there are. The second case touches three layers at once: the search field, the filtering getter in the store and the list of cards. The setValue method types text into the field and sends an input event, just as a technician would:
// ArtworkGallery.spec.js - continuation of describe
it('filters artworks on search input', async () => {
const wrapper = mount(ArtworkGallery, {
global: {
plugins: [createPinia()]
}
})
const store = useGalleryStore()
store.artworks = [
{ id: 1, title: 'Mona Lisa', artist: 'Leonardo' },
{ id: 2, title: 'David', artist: 'Michelangelo' }
]
await wrapper.vm.$nextTick()
const searchInput = wrapper.find('input[type="search"]')
await searchInput.setValue('mona')
await wrapper.vm.$nextTick()
const cards = wrapper.findAll('.artwork-card')
expect(cards).toHaveLength(1)
expect(cards[0].text()).toContain('Mona Lisa')
})
})The test does not call the getter directly; it goes through the interface, so it also checks that the template is really wired to the store. The store itself has not been replaced with anything - it is the same code that will fly to Mars.
Router Integration
A component with <RouterView> needs a working router. In a test you create it with the createRouter function and the createMemoryHistory history, which keeps the address in memory instead of the browser's address bar, and then you mount the application with the router in global.plugins:
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import App from './App.vue'
describe('App with Router', () => {
it('navigates to gallery page', async () => {
const router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Home</div>' } },
{ path: '/gallery', component: { template: '<div>Gallery</div>' } }
]
})
const wrapper = mount(App, {
global: {
plugins: [router]
}
})
await router.push('/gallery')
await router.isReady()
expect(wrapper.html()).toContain('Gallery')
})
})router.push returns a navigation promise, and router.isReady() waits for the initial navigation to finish, which is why both calls have await. Placeholder views written as an object with a template field are enough in a test; in an application built by Vite you will use .vue files, because the default browser build of Vue does not compile templates on the fly.
E2E Tests (Cypress/Playwright)
E2E tests work one floor higher: they launch a real browser, open the application at its address and click like a human. You do not mount components here - you visit the page. The Vue documentation recommends Playwright and Cypress for this, and the create-vue scaffolding tool offers Playwright when you create a project. This is what a scenario looks like in Cypress:
// cypress/e2e/gallery.cy.js
describe('Gallery E2E', () => {
beforeEach(() => {
cy.visit('/')
})
it('displays artworks', () => {
cy.get('.artwork-card').should('have.length.greaterThan', 0)
})
it('filters artworks by search', () => {
cy.get('input[type="search"]').type('mona')
cy.get('.artwork-card').should('have.length', 1)
cy.get('.artwork-card').first().should('contain', 'Mona Lisa')
})cy.visit('/') opens the page relative to the base address (baseUrl) from the Cypress configuration. should assertions retry on their own until the condition is met or the timeout expires, so you do not wait for the render by hand. The next cases continue along the operator's path - to the artwork details and favourites:
// cypress/e2e/gallery.cy.js - continuation of describe
it('opens artwork detail on click', () => {
cy.get('.artwork-card').first().click()
cy.url().should('include', '/artwork/')
cy.get('.artwork-detail').should('be.visible')
})
it('adds artwork to favorites', () => {
cy.get('.artwork-card').first().find('.favorite-btn').click()
cy.get('.favorites-count').should('contain', '1')
})
})The test knows no component, store or route - it sees only what the user sees: the address, the elements and their text.
Playwright Example
The same scenario in Playwright. Each test receives a page object, a browser tab, and the steps are always the same: open the application (page.goto), find an element (page.locator), perform an action (click, fill) and check the result (expect):
// tests/gallery.spec.js
import { test, expect } from '@playwright/test'
test.describe('Gallery', () => {
test('should display artworks', async ({ page }) => {
await page.goto('/')
const cards = page.locator('.artwork-card')
await expect(cards).toHaveCount(12)
})
test('should filter artworks', async ({ page }) => {
await page.goto('/')
await page.locator('input[type="search"]').fill('leonardo')
const cards = page.locator('.artwork-card')
await expect(cards).toHaveCount(2)
})The page.fill() and page.click() methods that take a selector argument still exist, but the Playwright documentation marks them as discouraged and recommends actions on locators, such as locator.fill(). Assertions like toHaveCount wait for the condition on their own. The third test moves on to the details: first() picks the first matching element, and toHaveURL checks the address with a regular expression:
// tests/gallery.spec.js - continuation of test.describe
test('should navigate to artwork detail', async ({ page }) => {
await page.goto('/')
await page.locator('.artwork-card').first().click()
await expect(page).toHaveURL(/.*artwork\/\d+/)
await expect(page.locator('.artwork-detail')).toBeVisible()
})
})The toBeVisible assertion confirms that the details panel can actually be seen. The shortest smoke test checks the browser tab's title with the toHaveTitle assertion:
test('shows the gallery title', async ({ page }) => {
await page.goto('/')
await expect(page).toHaveTitle('Gallery')
})The title comes from the <title> element in index.html, so when this test fails, the application most likely did not start at all. All the tests call page.goto('/') with a relative path, which works only with a base address in playwright.config.js:
// playwright.config.js
import { defineConfig } from '@playwright/test'
export default defineConfig({
use: {
baseURL: 'http://localhost:4173'
},
webServer: {
command: 'npm run build && npm run preview',
url: 'http://localhost:4173'
}
})The webServer section tells Playwright to build the application itself and start vite preview, whose default port is 4173 - so you test exactly the payload that will fly to production.
My advice: keep the proportions of the pyramid - dozens of unit tests, a few integration tests for the key connections and a handful of E2E tests for the paths without which the mission loses its point. The Vitest coverage report will tell you which parts of the code no test has visited yet. When everything shines green, what remains is to build the payload for shipping - we will take care of that in the next lesson.
Remember: unit tests check the valves, integration tests the whole installations, and E2E is a test flight of the entire rocket.
Code for this lesson: App.vue
<script setup>
import { ref, computed } from 'vue'
// NOVA LAB - Production Build Checklist
const checklist = ref([
{ id: 1, task: 'Remove console.log statements', completed: false, critical: true },
{ id: 2, task: 'Enable production mode', completed: false, critical: true },
{ id: 3, task: 'Optimize images and assets', completed: false, critical: false },
{ id: 4, task: 'Enable HTTPS/SSL', completed: false, critical: true },
{ id: 5, task: 'Set up error tracking', completed: false, critical: false },
{ id: 6, task: 'Configure CDN', completed: false, critical: false },
{ id: 7, task: 'Enable compression (gzip/brotli)', completed: false, critical: true },
{ id: 8, task: 'Test on multiple devices', completed: false, critical: false },
{ id: 9, task: 'Set up monitoring', completed: false, critical: false },
{ id: 10, task: 'Configure caching headers', completed: false, critical: true },
{ id: 11, task: 'Review security headers', completed: false, critical: true },
{ id: 12, task: 'Backup production data', completed: false, critical: true }
])
const completedCount = computed(() => {
return checklist.value.filter(item => item.completed).length
})
const criticalCompleted = computed(() => {
const critical = checklist.value.filter(item => item.critical)
const completed = critical.filter(item => item.completed)
return completed.length
})
const criticalTotal = computed(() => {
return checklist.value.filter(item => item.critical).length
})
const progress = computed(() => {
return Math.round((completedCount.value / checklist.value.length) * 100)
})
const isReadyForLaunch = computed(() => {
return criticalCompleted.value === criticalTotal.value
})
function toggleTask(id) {
const task = checklist.value.find(item => item.id === id)
if (task) {
task.completed = !task.completed
}
}
function resetChecklist() {
checklist.value.forEach(item => {
item.completed = false
})
}
</script>
<template>
<div class="checklist-app">
<div class="header">
<h1>Pre-Launch Checklist</h1>
<p>NOVA LAB Production Deployment</p>
</div>
<div class="progress-panel">
<div class="progress-bar">
<div class="progress-fill" :style="{ width: progress + '%' }"></div>
</div>
<div class="progress-stats">
<span>Total: {{ completedCount }}/{{ checklist.length }}</span>
<span>Critical: {{ criticalCompleted }}/{{ criticalTotal }}</span>
<span>{{ progress }}%</span>
</div>
</div>
<div class="launch-status" :class="isReadyForLaunch ? 'ready' : 'not-ready'">
<template v-if="isReadyForLaunch">
ALL SYSTEMS GO! Ready for deployment!
</template>
<template v-else>
{{ criticalTotal - criticalCompleted }} critical task(s) remaining
</template>
</div>
<div class="checklist">
<div
v-for="item in checklist"
:key="item.id"
class="checklist-item"
:class="{
completed: item.completed,
critical: item.critical && !item.completed
}"
@click="toggleTask(item.id)"
>
<div class="checkbox">
<span v-if="item.completed">x</span>
</div>
<div class="task-content">
<span class="task-text">{{ item.task }}</span>
<span v-if="item.critical" class="badge">CRITICAL</span>
</div>
</div>
</div>
<div class="actions">
<button @click="resetChecklist" class="reset-btn">
Reset Checklist
</button>
</div>
</div>
</template>
<style scoped>
.checklist-app {
background: linear-gradient(135deg, #0a0e27 0%, #1a1e3f 100%);
color: #00ff88;
padding: 2rem;
min-height: 100vh;
font-family: 'Courier New', monospace;
}
.header {
text-align: center;
margin-bottom: 2rem;
border-bottom: 2px solid #00b4d8;
padding-bottom: 1rem;
}
.header h1 {
color: #00ff88;
text-shadow: 0 0 10px #00ff88;
margin: 0;
}
.header p {
color: #00b4d8;
margin: 0.5rem 0 0 0;
}
.progress-panel {
background: rgba(0, 180, 216, 0.1);
border: 1px solid #00b4d8;
border-radius: 8px;
padding: 1.5rem;
margin-bottom: 1.5rem;
}
.progress-bar {
height: 30px;
background: rgba(0, 255, 136, 0.1);
border-radius: 15px;
overflow: hidden;
margin-bottom: 1rem;
border: 1px solid #00b4d8;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #00b4d8, #00ff88);
transition: width 0.5s ease;
box-shadow: 0 0 10px #00ff88;
}
.progress-stats {
display: flex;
justify-content: space-between;
color: #00b4d8;
font-size: 0.9rem;
}
.launch-status {
text-align: center;
padding: 1.5rem;
margin-bottom: 1.5rem;
border-radius: 8px;
font-size: 1.2rem;
font-weight: bold;
}
.launch-status.ready {
background: rgba(0, 255, 136, 0.2);
color: #00ff88;
border: 2px solid #00ff88;
box-shadow: 0 0 20px rgba(0, 255, 136, 0.3);
}
.launch-status.not-ready {
background: rgba(255, 165, 0, 0.2);
color: #ffa500;
border: 2px solid #ffa500;
}
.checklist {
display: flex;
flex-direction: column;
gap: 0.75rem;
margin-bottom: 2rem;
}
.checklist-item {
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem;
background: rgba(0, 180, 216, 0.1);
border: 1px solid #00b4d8;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s;
}
.checklist-item:hover {
background: rgba(0, 180, 216, 0.2);
transform: translateX(5px);
}
.checklist-item.completed {
background: rgba(0, 255, 136, 0.1);
border-color: #00ff88;
}
.checklist-item.critical {
border-color: #ffa500;
border-width: 2px;
}
.checkbox {
width: 24px;
height: 24px;
border: 2px solid #00b4d8;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
flex-shrink: 0;
}
.completed .checkbox {
background: #00ff88;
border-color: #00ff88;
color: #0a0e27;
}
.task-content {
flex: 1;
display: flex;
justify-content: space-between;
align-items: center;
}
.task-text {
color: #fff;
}
.completed .task-text {
text-decoration: line-through;
opacity: 0.7;
}
.badge {
background: #ffa500;
color: #0a0e27;
padding: 0.25rem 0.75rem;
border-radius: 12px;
font-size: 0.75rem;
font-weight: bold;
}
.actions {
text-align: center;
}
.reset-btn {
padding: 1rem 2rem;
background: rgba(0, 180, 216, 0.2);
border: 2px solid #00b4d8;
color: #00b4d8;
font-size: 1rem;
font-family: 'Courier New', monospace;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s;
}
.reset-btn:hover {
background: #00b4d8;
color: #0a0e27;
box-shadow: 0 0 20px #00b4d8;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. How do integration tests differ from unit tests?
2. What is the difference between mount() and shallowMount() in Vue Test Utils?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Write an integration test for a component with a Pinia store - verify that the component displays data from the store.
- Vertical ordering
Arrange the steps of an end-to-end test in order:
- Click in order
Arrange the Playwright code that opens a page and checks its title:
- Code editor
Write a Playwright test that verifies gallery filtering - typing text in search and verifying results.
- Horizontal ordering
Arrange the code for mounting a component with router in a test:
Tasks after this lesson
- QuestionWhat are integration tests
- Questionmount() vs shallowMount()
- Coding taskPractice: Integration test with Pinia
- Put in orderE2E test steps
- QuestionPlaywright - E2E tool
- Click in orderPlaywright - navigation and assertion
- Coding taskPractice: E2E test with Playwright
- Put in orderRouter test - mounting with a plugin
- QuestionTest coverage