From 1eedc422c942fb0862c457e42fc828d6fd515386 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 30 May 2023 22:01:12 +0100 Subject: [PATCH 1/2] test: add `.expose` test --- playground/components/WrapperTests.vue | 3 ++- playground/tests/nuxt/index.spec.ts | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/playground/components/WrapperTests.vue b/playground/components/WrapperTests.vue index 1dc856ad..8e7a2dcc 100644 --- a/playground/components/WrapperTests.vue +++ b/playground/components/WrapperTests.vue @@ -9,7 +9,8 @@ function testExpose () { } defineExpose({ - testExpose + testExpose, + someRef: ref('thing') }) diff --git a/playground/tests/nuxt/index.spec.ts b/playground/tests/nuxt/index.spec.ts index 51aeb597..78620ce8 100644 --- a/playground/tests/nuxt/index.spec.ts +++ b/playground/tests/nuxt/index.spec.ts @@ -98,6 +98,12 @@ describe('test utils', () => { `) }) + it('can access exposed methods from components mounted within nuxt suspense', async () => { + const component = await mountSuspended(WrapperTests) + expect(component.vm.someRef).toBe('thing') + expect(component.vm.testExpose?.()).toBe('thing') + }) + it('can mock fetch requests', async () => { registerEndpoint('https://jsonplaceholder.typicode.com/todos/1', () => ({ title: 'title from mocked api', From 96daeb0117d6c4df91d4d26c09286487481aa416 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 30 May 2023 22:06:08 +0100 Subject: [PATCH 2/2] fix: try storing exposed properties --- .../src/runtime/mount.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/vitest-environment-nuxt/src/runtime/mount.ts b/packages/vitest-environment-nuxt/src/runtime/mount.ts index 986a178d..e5d5461d 100644 --- a/packages/vitest-environment-nuxt/src/runtime/mount.ts +++ b/packages/vitest-environment-nuxt/src/runtime/mount.ts @@ -36,6 +36,7 @@ export async function mountSuspended( typeof mount > >(resolve => { + let exposed: null | any[] = [] const vm = mount( { setup: (props, ctx) => { @@ -48,7 +49,16 @@ export async function mountSuspended( render: (renderContext: any) => h( Suspense, - { onResolve: () => nextTick().then(() => resolve(vm as any)) }, + { + onResolve: () => + nextTick().then(() => { + for (const expose of exposed || []) { + setupContext.expose(...expose) + } + exposed = null + resolve(vm as any) + }), + }, { default: () => h({ @@ -65,7 +75,15 @@ export async function mountSuspended( : undefined, setup: setup ? (props: Record) => - setup(props, setupContext) + setup(props, { + ...setupContext, + expose: (...args) => { + if (exposed) { + return void exposed.push(args) + } + return setupContext.expose(...args) + }, + }) : undefined, }