0
0
Vueframework~20 mins

Mocking composables and stores in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery in Vue
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when mocking a composable's return value?

Given a Vue component that uses a composable returning a reactive count, and the composable is mocked to return a fixed count, what will the component render?

Vue
/* Composable: useCounter.js */
import { ref } from 'vue';
export function useCounter() {
  const count = ref(0);
  function increment() { count.value++; }
  return { count, increment };
}

/* Component: CounterDisplay.vue */
<template>
  <div>{{ count }}</div>
</template>
<script setup>
import { useCounter } from './useCounter';
const { count } = useCounter();
</script>

/* Test file snippet */
vi.mock('./useCounter', () => ({
  useCounter: () => ({ count: 42 })
}));
AThe component throws a runtime error due to missing ref wrapper.
BThe component renders '0' because the mock does not update reactivity.
CThe component renders '42' as the count value.
DThe component renders 'undefined' because count is not reactive.
Attempts:
2 left
💡 Hint

Think about what the mock returns and how the component uses it.

🔧 Debug
intermediate
2:00remaining
Why does mocking a Pinia store's state fail with a TypeError?

In a test, mocking a Pinia store's state directly causes a TypeError: Cannot read property 'value' of undefined. What is the likely cause?

Vue
import { setActivePinia, createPinia } from 'pinia';
import { useUserStore } from '@/stores/user';

setActivePinia(createPinia());

vi.mock('@/stores/user', () => ({
  useUserStore: () => ({
    state: { name: 'Alice' }
  })
}));

const store = useUserStore();
console.log(store.name);
AThe mock returns a plain object instead of a reactive store, so accessing reactive properties fails.
BPinia stores cannot be mocked at all, causing all mocks to fail.
CThe test environment lacks a proper Vue instance, causing reactivity errors.
DThe store is not registered in Pinia, so useUserStore returns undefined.
Attempts:
2 left
💡 Hint

Consider how Pinia stores expose state and reactivity.

📝 Syntax
advanced
2:00remaining
Which mock setup correctly mocks a composable returning a reactive ref?

Choose the correct way to mock a composable that returns a reactive ref count initialized to 10.

Vue
/* Original composable: useCount.js */
import { ref } from 'vue';
export function useCount() {
  const count = ref(0);
  return { count };
}
Avi.mock('./useCount', () => ({ useCount: () => ({ count: 10 }) }));
Bvi.mock('./useCount', () => ({ useCount: () => ({ count: computed(() => 10) }) }));
Cvi.mock('./useCount', () => ({ useCount: () => ({ count: reactive(10) }) }));
Dvi.mock('./useCount', () => ({ useCount: () => ({ count: ref(10) }) }));
Attempts:
2 left
💡 Hint

Remember how the original composable returns a ref.

state_output
advanced
2:00remaining
What is the value of store.count after mocking and incrementing?

Given a Pinia store with a count state and an increment action, if the store is mocked to start with count = 5, what is the value of count after calling increment() once?

Vue
/* Store definition */
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() { this.count++; }
  }
});

/* Test mock */
vi.mock('@/stores/counter', () => ({
  useCounterStore: () => ({
    count: 5,
    increment() { this.count++; }
  })
}));

const store = useCounterStore();
store.increment();
const result = store.count;
A5
B6
CNaN
Dundefined
Attempts:
2 left
💡 Hint

Check how the mocked increment method modifies the count property.

🧠 Conceptual
expert
3:00remaining
Why is it important to mock composables with the same reactive structure as originals?

When mocking Vue composables or Pinia stores, why must the mock preserve the original's reactive structure (e.g., refs, reactive objects) instead of returning plain values?

ABecause Vue components rely on reactivity to update the UI, and mocks without reactive wrappers break this, causing stale or missing updates.
BBecause plain values cause syntax errors in Vue templates when used inside components.
CBecause testing frameworks require all mocks to be reactive to run tests successfully.
DBecause only reactive objects can be imported in Vue components without errors.
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes to update the screen.