0
0
Vueframework~20 mins

Vitest setup for unit testing in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vitest Vue Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
  })
})
AThe test passes because the component renders 'Hello Vitest' correctly.
BThe test fails because the component does not render any text.
CThe test throws a runtime error due to missing props.
DThe test fails because the greeting is reactive and not rendered as plain text.
Attempts:
2 left
💡 Hint
Check what the component renders and what the test expects.
📝 Syntax
intermediate
2:00remaining
Which Vitest configuration snippet correctly sets up Vue testing?
Choose the correct Vitest config snippet to enable Vue component testing with proper plugins.
A
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    environment: 'node'
  }
})
B
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  test: {
    globals: false,
    environment: 'jsdom'
  },
  plugins: []
})
C
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom'
  },
  plugins: [vue()]
})
D
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'

export default defineConfig({
  test: {
    globals: true
  },
  plugins: [react()]
})
Attempts:
2 left
💡 Hint
Vue testing requires the Vue plugin and jsdom environment.
🔧 Debug
advanced
2: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()
  })
})
AThe component does not emit any events, so the test fails due to missing emits declaration.
BThe test uses 'await' incorrectly on wrapper.trigger causing a syntax error.
CThe click handler is not passed as a prop or event listener to the component, so onClick is never called.
DThe vi.fn() mock function is not imported, causing a TypeError: Cannot read property 'fn' of undefined.
Attempts:
2 left
💡 Hint
Check the imports for mocking utilities.
state_output
advanced
2: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')
  })
})
A0
B2
C1
DNaN
Attempts:
2 left
💡 Hint
Each click triggers the increment function increasing count by 1.
🧠 Conceptual
expert
2: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.
AVitest is a test runner and assertion library that executes tests and verifies component behavior in a simulated browser environment.
BVitest is a UI framework that replaces Vue for building user interfaces with built-in testing features.
CVitest is a browser plugin that automatically records user interactions for test generation.
DVitest is a CSS preprocessor that helps style Vue components and includes testing utilities.
Attempts:
2 left
💡 Hint
Think about what tools you need to run and check tests.