Skip to content

Instantly share code, notes, and snippets.

@wobsoriano
Created June 1, 2022 23:20
Show Gist options
  • Save wobsoriano/56239c3d738802c7a4273651ee819677 to your computer and use it in GitHub Desktop.
Save wobsoriano/56239c3d738802c7a4273651ee819677 to your computer and use it in GitHub Desktop.
Test Vue 3 composables
import { defineComponent, h, nextTick as vueNextTick } from "vue";
import { mount } from "@vue/test-utils";
import waitFor from 'p-wait-for';
export interface RenderComposableResult<T> {
result: T;
waitFor: typeof waitFor,
nextTick: typeof vueNextTick;
}
export function renderComposable<T>(
composable: () => T
): RenderComposableResult<T> {
const Child = defineComponent({
setup() {
const result = composable();
const wrapper = () => result;
return { wrapper };
},
render: () => null
});
const wrapper = mount({
render: () => h(Child, { ref: 'child' }),
});
return {
result: (wrapper.vm.$refs.child as any).wrapper(),
waitFor,
nextTick: wrapper.vm.$nextTick,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment