Challenge - 5 Problems
Vitest Vue Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Vitest test for a Vue component?
Given the following Vue component and Vitest test, what will the test output when run?
Vue
/* Vue component: HelloWorld.vue */ <template> <h1>{{ greeting }}</h1> </template> <script setup> import { ref } from 'vue' const greeting = ref('Hello Vitest') </script> /* Vitest test file: HelloWorld.test.js */ import { mount } from '@vue/test-utils' import { describe, it, expect } from 'vitest' import HelloWorld from './HelloWorld.vue' describe('HelloWorld', () => { it('renders greeting text', () => { const wrapper = mount(HelloWorld) expect(wrapper.text()).toBe('Hello Vitest') }) })
Attempts:
2 left
💡 Hint
Check what the component renders and what the test expects.
✗ Incorrect
The component uses a ref with the string 'Hello Vitest' and renders it inside an
. The test mounts the component and checks the text content, which matches exactly, so the test passes.
📝 Syntax
intermediate2:00remaining
Which Vitest configuration snippet correctly sets up Vue testing?
Choose the correct Vitest config snippet to enable Vue component testing with proper plugins.
Attempts:
2 left
💡 Hint
Vue testing requires the Vue plugin and jsdom environment.
✗ Incorrect
Option C correctly imports the Vue plugin and sets the test environment to 'jsdom' with globals enabled, which is needed for Vue component testing in Vitest.
🔧 Debug
advanced2:00remaining
Why does this Vitest test for a Vue component fail with 'TypeError: Cannot read property'?
Examine the test code below and select the reason for the TypeError during test execution.
Vue
import { mount } from '@vue/test-utils' import { describe, it, expect } from 'vitest' import MyButton from './MyButton.vue' describe('MyButton', () => { it('calls click handler', async () => { const onClick = vi.fn() const wrapper = mount(MyButton) await wrapper.trigger('click') expect(onClick).toHaveBeenCalled() }) })
Attempts:
2 left
💡 Hint
Check the imports for mocking utilities.
✗ Incorrect
The `vi` object from Vitest, which provides `vi.fn()` for creating mock functions, is not imported. Attempting `vi.fn()` throws TypeError: Cannot read property 'fn' of undefined.
❓ state_output
advanced2:00remaining
What is the value of 'count' after this Vitest test runs?
Given the Vue component and test below, what is the final value of the reactive 'count' after the test?
Vue
/* Counter.vue */ <template> <button @click="increment">Count: {{ count }}</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> /* Counter.test.js */ import { mount } from '@vue/test-utils' import { describe, it, expect } from 'vitest' import Counter from './Counter.vue' describe('Counter', () => { it('increments count on click', async () => { const wrapper = mount(Counter) await wrapper.trigger('click') await wrapper.trigger('click') expect(wrapper.text()).toContain('Count: 2') }) })
Attempts:
2 left
💡 Hint
Each click triggers the increment function increasing count by 1.
✗ Incorrect
The component starts with count 0. Each click triggers increment which adds 1. Two clicks increase count to 2, which the test expects and verifies.
🧠 Conceptual
expert2:00remaining
Which statement best describes Vitest's role in Vue unit testing?
Select the most accurate description of what Vitest provides when testing Vue components.
Attempts:
2 left
💡 Hint
Think about what tools you need to run and check tests.
✗ Incorrect
Vitest is a modern test runner and assertion library designed to run tests quickly and provide feedback. It works with Vue by simulating browser-like environments and checking component outputs.