Bird
Raised Fist0
Vueframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Vitest when working with Vue projects?
easy
A. To run unit tests quickly and efficiently
B. To build and bundle Vue components
C. To style Vue components with CSS
D. To deploy Vue applications to servers

Solution

  1. Step 1: Understand Vitest's role

    Vitest is a testing framework designed to run tests fast and provide feedback.
  2. Step 2: Compare with other options

    Building, styling, and deploying are not testing tasks, so they don't match Vitest's purpose.
  3. Final Answer:

    To run unit tests quickly and efficiently -> Option A
  4. Quick Check:

    Vitest = fast unit testing [OK]
Hint: Vitest is for testing, not building or styling [OK]
Common Mistakes:
  • Confusing Vitest with build tools
  • Thinking Vitest handles styling
  • Assuming Vitest deploys apps
2. Which of the following is the correct way to import Vue Test Utils in a Vitest test file?
easy
A. import mount from 'vue-test-utils';
B. import { mount } from '@vue/test-utils';
C. const mount = require('@vue/test-utils');
D. import { mount } from 'vitest/vue';

Solution

  1. Step 1: Check correct import syntax

    Vue Test Utils exports mount as a named export, so use curly braces and the correct package name '@vue/test-utils'.
  2. Step 2: Eliminate incorrect options

    import mount from 'vue-test-utils'; uses wrong package name and default import, C uses CommonJS require which is not standard ES modules, D uses wrong package name.
  3. Final Answer:

    import { mount } from '@vue/test-utils'; -> Option B
  4. Quick Check:

    Correct import = import { mount } from '@vue/test-utils'; [OK]
Hint: Use named imports with curly braces from '@vue/test-utils' [OK]
Common Mistakes:
  • Using default import instead of named import
  • Using require instead of import
  • Wrong package name in import
3. Given this Vitest test code for a Vue component:
import { mount } from '@vue/test-utils';
import { describe, it, expect } from 'vitest';
import HelloWorld from './HelloWorld.vue';

describe('HelloWorld', () => {
  it('renders greeting', () => {
    const wrapper = mount(HelloWorld);
    expect(wrapper.text()).toContain('Hello');
  });
});

What will happen when this test runs?
medium
A. The test passes only if the component has a button
B. The test fails because mount is not imported correctly
C. The test passes if the component's text includes 'Hello'
D. The test throws a runtime error due to missing props

Solution

  1. Step 1: Analyze the test logic

    The test mounts the HelloWorld component and checks if its text includes 'Hello'.
  2. Step 2: Check for errors or conditions

    Mount is imported correctly, no props are required, and the test expects 'Hello' in text.
  3. Final Answer:

    The test passes if the component's text includes 'Hello' -> Option C
  4. Quick Check:

    Text includes 'Hello' = test passes [OK]
Hint: Check what the expect statement verifies in the test [OK]
Common Mistakes:
  • Assuming mount import is wrong
  • Thinking props are mandatory here
  • Confusing test condition with button presence
4. You wrote this Vitest test but it fails with "ReferenceError: describe is not defined":
import { mount } from '@vue/test-utils';
import HelloWorld from './HelloWorld.vue';

describe('HelloWorld', () => {
  it('renders greeting', () => {
    const wrapper = mount(HelloWorld);
    expect(wrapper.text()).toContain('Hello');
  });
});

What is the most likely fix?
medium
A. Remove the mount import
B. Change describe to test and it to check
C. Wrap the test code inside a Vue component
D. Add import { describe, it, expect } from 'vitest'; at the top

Solution

  1. Step 1: Identify missing imports

    The error says 'describe' is not defined, meaning it is not imported from Vitest.
  2. Step 2: Add necessary imports

    Importing describe, it, and expect from 'vitest' fixes the error.
  3. Final Answer:

    Add import { describe, it, expect } from 'vitest'; at the top -> Option D
  4. Quick Check:

    Missing test functions import = add import [OK]
Hint: Always import test functions from 'vitest' [OK]
Common Mistakes:
  • Assuming describe is globally available
  • Changing test function names incorrectly
  • Removing needed imports
5. You want to test a Vue component that uses a global plugin. Which Vitest setup step is necessary to ensure the plugin works during testing?
hard
A. Configure the test to mount the component with the plugin registered globally
B. Mock the plugin by replacing it with an empty object
C. Import the plugin inside the test file but do not register it
D. Run the test without mounting the component

Solution

  1. Step 1: Understand plugin usage in tests

    Global plugins must be registered in the test mount options to behave like in the app.
  2. Step 2: Choose correct setup

    Mounting with the plugin registered globally ensures the component can use it during the test.
  3. Final Answer:

    Configure the test to mount the component with the plugin registered globally -> Option A
  4. Quick Check:

    Global plugin = register in mount options [OK]
Hint: Register global plugins in mount options for tests [OK]
Common Mistakes:
  • Mocking plugin without registering it
  • Importing plugin but not registering
  • Testing without mounting component
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard