Skip to content

Commit 1636ced

Browse files
committedApr 17, 2024
fix(runtime): improve types for renderSuspended and mountSuspended
1 parent 21aa7a2 commit 1636ced

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed
 

‎src/runtime-utils/mount.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export type MountSuspendedOptions<T> = ComponentMountingOptions<T> & {
4444
export async function mountSuspended<T>(
4545
component: T,
4646
options?: MountSuspendedOptions<T>,
47-
): Promise<ReturnType<typeof mount<T>> & { setupState: any }> {
47+
): Promise<ReturnType<typeof mount<T>> & { setupState: Record<string, unknown> }> {
4848
const {
4949
props = {},
5050
attrs = {},
@@ -55,46 +55,47 @@ export async function mountSuspended<T>(
5555

5656
// @ts-expect-error untyped global __unctx__
5757
const vueApp = globalThis.__unctx__.get('nuxt-app').tryUse().vueApp
58-
const { render, setup } = component as DefineComponent<any, any>
58+
const { render, setup } = component as DefineComponent<Record<string, unknown>, Record <string, unknown>>
5959

6060
let setupContext: SetupContext
61-
let setupState: any
62-
const setProps = reactive<Record<string, any>>({})
61+
let setupState: Record<string, unknown>
62+
const setProps = reactive<Record<string, unknown>>({})
6363

64-
let passedProps: Record<string, any>
64+
let passedProps: Record<string, unknown>
6565
const wrappedSetup = async (
66-
props: Record<string, any>,
66+
props: Record<string, unknown>,
6767
setupContext: SetupContext,
6868
) => {
6969
passedProps = props
7070
if (setup) {
71-
setupState = await setup(props, setupContext)
71+
const result = await setup(props, setupContext)
72+
setupState = result && typeof result === 'object' ? result : {}
7273
return setupState
7374
}
7475
}
7576

76-
return new Promise<ReturnType<typeof mount<T>> & { setupState: any }>(
77+
return new Promise<ReturnType<typeof mount<T>> & { setupState: Record<string, unknown> }>(
7778
(resolve) => {
7879
const vm = mount(
7980
{
80-
setup: (props: Record<string, any>, ctx: SetupContext) => {
81+
setup: (props: Record<string, unknown>, ctx: SetupContext) => {
8182
setupContext = ctx
8283
return NuxtRoot.setup(props, {
8384
...ctx,
8485
expose: () => {},
8586
})
8687
},
87-
render: (renderContext: any) =>
88+
render: (renderContext: Record<string, unknown>) =>
8889
h(
8990
Suspense,
9091
{
9192
onResolve: () =>
9293
nextTick().then(() => {
93-
(vm as any).setupState = setupState;
94-
(vm as any).__setProps = (props: Record<string, any>) => {
94+
(vm as unknown as AugmentedVueInstance).setupState = setupState;
95+
(vm as unknown as AugmentedVueInstance).__setProps = (props: Record<string, unknown>) => {
9596
Object.assign(setProps, props)
9697
}
97-
resolve(vm as any)
98+
resolve(vm as ReturnType<typeof mount<T>> & { setupState: Record<string, unknown> })
9899
}),
99100
},
100101
{
@@ -110,7 +111,7 @@ export async function mountSuspended<T>(
110111
name: 'MountSuspendedComponent',
111112
...component,
112113
render: render
113-
? function (this: any, _ctx: any, ...args: any[]) {
114+
? function (this: unknown, _ctx: Record<string, unknown>, ...args: unknown[]) {
114115
for (const key in setupState || {}) {
115116
renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key]
116117
}
@@ -123,7 +124,7 @@ export async function mountSuspended<T>(
123124
return render.call(this, renderContext, ...args)
124125
}
125126
: undefined,
126-
setup: setup ? (props: Record<string, any>) => wrappedSetup(props, setupContext) : undefined,
127+
setup: setup ? (props: Record<string, unknown>) => wrappedSetup(props, setupContext) : undefined,
127128
}
128129

129130
return () => h(clonedComponent, { ...defu(setProps, props) as typeof props, ...attrs }, slots)
@@ -144,7 +145,7 @@ export async function mountSuspended<T>(
144145
stubs: {
145146
Suspense: false,
146147
MountSuspendedHelper: false,
147-
[typeof (component as any).name === 'string' ? (component as any).name : 'MountSuspendedComponent']: false,
148+
[component && typeof component === 'object' && 'name' in component && typeof component.name === 'string' ? component.name : 'MountSuspendedComponent']: false,
148149
},
149150
components: { RouterLink },
150151
},
@@ -154,3 +155,8 @@ export async function mountSuspended<T>(
154155
},
155156
)
156157
}
158+
159+
interface AugmentedVueInstance {
160+
setupState?: Record<string, unknown>
161+
__setProps?: (props: Record<string, unknown>) => void
162+
}

‎src/runtime-utils/render.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function renderSuspended<T>(
6969

7070
// @ts-expect-error untyped global __unctx__
7171
const { vueApp } = globalThis.__unctx__.get('nuxt-app').tryUse()
72-
const { render, setup } = component as DefineComponent<any, any>
72+
const { render, setup } = component as DefineComponent<Record<string, unknown>, Record <string, unknown>>
7373

7474
// cleanup previously mounted test wrappers
7575
document.querySelector(`#${WRAPPER_EL_ID}`)?.remove()
@@ -80,15 +80,15 @@ export async function renderSuspended<T>(
8080
const utils = renderFromTestingLibrary(
8181
{
8282

83-
setup: (props: any, ctx: any) => {
83+
setup: (props: Record<string, unknown>, ctx: SetupContext) => {
8484
setupContext = ctx
8585

8686
return NuxtRoot.setup(props, {
8787
...ctx,
8888
expose: () => {},
8989
})
9090
},
91-
render: (renderContext: any) =>
91+
render: (renderContext: unknown) =>
9292
// See discussions in https://github.com/testing-library/vue-testing-library/issues/230
9393
// we add this additional root element because otherwise testing-library breaks
9494
// because there's no root element while Suspense is resolving
@@ -109,11 +109,11 @@ export async function renderSuspended<T>(
109109
const clonedComponent = {
110110
...component,
111111
render: render
112-
? (_ctx: any, ...args: any[]) =>
112+
? (_ctx: unknown, ...args: unknown[]) =>
113113
render(renderContext, ...args)
114114
: undefined,
115115
setup: setup
116-
? (props: Record<string, any>) =>
116+
? (props: Record<string, unknown>) =>
117117
setup(props, setupContext)
118118
: undefined,
119119
}

0 commit comments

Comments
 (0)
Please sign in to comment.