Challenge - 5 Problems
Vue Test Utils Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the rendered output of this Vue component test?
Given the following Vue component and test code, what will be the text content of the mounted component?
Vue
/* Component: HelloWorld.vue */ <template> <h1>{{ greeting }}</h1> </template> <script setup> import { ref } from 'vue' const greeting = ref('Hello Vue Test Utils') </script> /* Test file: HelloWorld.spec.js */ import { mount } from '@vue/test-utils' import HelloWorld from './HelloWorld.vue' test('renders greeting text', () => { const wrapper = mount(HelloWorld) expect(wrapper.text()).toBe('Hello Vue Test Utils') })
Attempts:
2 left
💡 Hint
Check what wrapper.text() returns in Vue Test Utils.
✗ Incorrect
The wrapper.text() method returns the text content inside the component's root element, which is 'Hello Vue Test Utils'. It does not include HTML tags or variable names.
❓ state_output
intermediate2:00remaining
What is the value of count after clicking the button twice?
Consider this Vue component and test. What will be the value of the count variable after the test simulates two button clicks?
Vue
<template> <button @click="increment">Count: {{ count }}</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> // Test file import { mount } from '@vue/test-utils' import Counter from './Counter.vue' test('increments count on button click', async () => { const wrapper = mount(Counter) const button = wrapper.get('button') await button.trigger('click') await button.trigger('click') expect(wrapper.text()).toContain('Count: 2') })
Attempts:
2 left
💡 Hint
Each click triggers the increment function increasing count by 1.
✗ Incorrect
The count starts at 0 and increments by 1 on each button click. After two clicks, count is 2, so the text includes 'Count: 2'.
🔧 Debug
advanced2:30remaining
Why does this Vue test fail with 'TypeError: wrapper.vm.increment is not a function'?
Examine the component and test code below. Why does the test fail with the error 'TypeError: wrapper.vm.increment is not a function'?
Vue
<template> <button @click="increment">Click me</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> // Test file import { mount } from '@vue/test-utils' import Clicker from './Clicker.vue' test('calls increment method', () => { const wrapper = mount(Clicker) wrapper.vm.increment() expect(wrapper.text()).toContain('1') })
Attempts:
2 left
💡 Hint
Think about how