0
0
Vueframework~20 mins

Component testing with Vue Test Utils - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Test Utils Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
})
AHello Vue Test Utils
Bgreeting
Cundefined
D<h1>Hello Vue Test Utils</h1>
Attempts:
2 left
💡 Hint
Check what wrapper.text() returns in Vue Test Utils.
state_output
intermediate
2: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')
})
A2
B1
C0
DNaN
Attempts:
2 left
💡 Hint
Each click triggers the increment function increasing count by 1.
🔧 Debug
advanced
2: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')
})
ABecause the button element is missing a click event
BBecause the increment function is not imported in the test file
CBecause the count variable is not reactive
DBecause functions declared in <script setup> are not exposed on wrapper.vm
Attempts:
2 left
💡 Hint
Think about how