javascript / expert
Snippet
Composable Reactive Lifecycle Verification Using Synthetic Mount Contexts
Vue composables that rely on lifecycle hooks like `onMounted` or `onUnmounted` require an active component instance during execution. Wrapping composable calls inside a synthetic inline component shell allows comprehensive unit testing of cleanup side effects.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { mount } from '@vue/test-utils';function mountComposable(composableFn) {let result;const TestComponent = {setup() {result = composableFn();return () => null;}};const wrapper = mount(TestComponent);return { result, unmount: () => wrapper.unmount() };}export { mountComposable };
vue
Breakdown
1
setup() {
Executes the target composable within an active Vue component setup context.
2
return () => null;
Returns an empty render function avoiding overhead while maintaining valid component semantics.
3
return { result, unmount: () => wrapper.unmount() };
Exposes the composable return values and a trigger to exercise onUnmounted cleanup hooks.