What is mocking in the context of Vue composables and stores?
Mocking means creating fake versions of composables or stores to test components without using real data or logic. It helps isolate parts during testing.
Click to reveal answer
beginner
Why do we mock composables or stores in Vue tests?
We mock them to control their behavior, avoid side effects, and test components independently from actual data or network calls.
Click to reveal answer
intermediate
How can you mock a composable in Vue 3 using the Composition API?
You can replace the composable import with a mock function that returns fixed values or reactive refs during tests.
Click to reveal answer
intermediate
What is a common tool to mock Pinia stores in Vue tests?
Pinia stores can be mocked by creating a fake store instance or using testing utilities like 'createTestingPinia' to simulate store behavior.
Click to reveal answer
beginner
How does mocking improve test reliability in Vue applications?
Mocking removes dependencies on real data or APIs, making tests faster, more predictable, and easier to debug.
Click to reveal answer
What is the main purpose of mocking a composable in Vue tests?
ATo add new features to the composable
BTo improve component styling
CTo increase app performance in production
DTo isolate component tests from real composable logic
✗ Incorrect
Mocking isolates tests by replacing real composable logic with controlled fake data.
Which Vue store library is commonly mocked using 'createTestingPinia'?
APinia
BVuex
CRedux
DMobX
✗ Incorrect
'createTestingPinia' is a utility for mocking Pinia stores in tests.
When mocking a composable, what should the mock return?
ARandom values each time
BThe original composable's code
CFixed or controlled reactive values
DNothing at all
✗ Incorrect
Mocks return fixed or controlled reactive values to simulate expected behavior.
Which Vue API is most relevant when mocking composables?
AComposition API
BOptions API
CVue Router
DVue CLI
✗ Incorrect
The Composition API is used to create and mock composables.
Mocking stores helps tests by:
AMaking tests slower
BRemoving side effects and dependencies
CChanging production code
DAdding network calls
✗ Incorrect
Mocking removes side effects and external dependencies, improving test reliability.
Explain how you would mock a Pinia store in a Vue 3 test.
Think about how to isolate store logic from the component during testing.
You got /4 concepts.
Describe why mocking composables is useful when testing Vue components.
Consider what problems mocking solves in testing.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to mock composables or stores in Vue component tests?
easy
A. To automatically generate UI snapshots
B. To improve the component's runtime performance
C. To isolate the component test from real logic and external dependencies
D. To enable two-way data binding in tests
Solution
Step 1: Understand the purpose of mocking
Mocking replaces real implementations with fake ones to isolate tests from external logic.
Step 2: Apply this to composables and stores
Mocking composables and stores prevents side effects and focuses tests on the component itself.
Final Answer:
To isolate the component test from real logic and external dependencies -> Option C
Quick Check:
Mocking isolates tests = A [OK]
Hint: Mock to isolate component logic from external code [OK]
Common Mistakes:
Thinking mocking improves runtime performance
Confusing mocking with UI snapshot generation
Believing mocking enables data binding
2. Which syntax correctly mocks a composable module named useAuth using Vitest's vi.mock()?
A. The mock should return a function, not an object directly
B. The vi.fn() should be replaced with a real function
C. The module path './useCounter' is incorrect
D. The mock should not include the increment function
Solution
Step 1: Understand composable mocking pattern
Composable modules export functions, so mocks must return a function, not an object.
Step 2: Identify the incorrect mock structure
The code returns an object directly instead of a function returning the object.
Final Answer:
The mock should return a function, not an object directly -> Option A
Quick Check:
Composable mocks return functions = D [OK]
Hint: Mock composables as functions returning objects [OK]
Common Mistakes:
Returning object directly instead of function
Replacing vi.fn() unnecessarily
Incorrect module path assumptions
5. You want to test a Vue component that uses a Pinia store with a fetchData action. How do you mock the store to verify fetchData is called without running its real logic?
hard
A. Replace fetchData with an empty async function in the real store
B. Import the real store and spy on fetchData without mocking
C. Mock the component instead of the store
D. Use vi.mock() to replace the store module with a mock where fetchData is a vi.fn()
Solution
Step 1: Understand mocking for store actions
To isolate tests, replace the store module with a mock that has fetchData as a mock function.
Step 2: Use vi.mock() to replace the store module
This allows you to track calls to fetchData without executing its real code.
Final Answer:
Use vi.mock() to replace the store module with a mock where fetchData is a vi.fn() -> Option D
Quick Check:
Mock store action with vi.fn() = A [OK]
Hint: Mock store actions with vi.fn() to track calls without side effects [OK]
Common Mistakes:
Not mocking the store and calling real logic
Mocking the component instead of the store
Replacing functions with empty but untracked implementations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.