Skip to content

Commit 5e368da

Browse files
committed
Add types for render function
1 parent 3ecde9e commit 5e368da

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@
6868
"lodash.merge": "^4.6.2",
6969
"msw": "^1.3.2",
7070
"tsd": "^0.29.0",
71+
"type-fest": "~2.19",
7172
"typescript": "^5.2.2",
7273
"vee-validate": "^4.11.8",
7374
"vue": "^3.3.5",
75+
"vue-component-type-helpers": "^2.0.19",
7476
"vue-eslint-parser": "^9.3.2",
7577
"vue-i18n": "^9.5.0",
7678
"vue-router": "^4.2.5",

types/index.d.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Minimum TypeScript Version: 4.0
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33

4-
import {MountingOptions} from '@vue/test-utils'
5-
import {queries, EventType, BoundFunctions} from '@testing-library/dom'
4+
import { VNodeChild } from 'vue';
5+
import { MountingOptions } from '@vue/test-utils'
6+
import { queries, EventType, BoundFunctions } from '@testing-library/dom'
67
// eslint-disable-next-line import/no-extraneous-dependencies
7-
import {OptionsReceived as PrettyFormatOptions} from 'pretty-format'
8+
import { OptionsReceived as PrettyFormatOptions } from 'pretty-format'
9+
import type { ComponentProps, ComponentSlots } from "vue-component-type-helpers";
10+
import { RemoveIndexSignature } from "type-fest";
811

912
// NOTE: fireEvent is overridden below
1013
export * from '@testing-library/dom'
@@ -44,12 +47,22 @@ interface VueTestingLibraryRenderOptions {
4447
container?: Element
4548
baseElement?: Element
4649
}
47-
export type RenderOptions = VueTestingLibraryRenderOptions &
48-
VueTestUtilsRenderOptions
4950

50-
export function render(
51-
TestComponent: any, // this makes me sad :sob:
52-
options?: RenderOptions,
51+
52+
type AllowNonFunctionSlots<Slots> = {
53+
[K in keyof Slots]: Slots[K] | VNodeChild;
54+
};
55+
type ExtractSlots<C> = AllowNonFunctionSlots<Partial<RemoveIndexSignature<ComponentSlots<C>>>>;
56+
57+
export interface RenderOptions<C> extends Omit<VueTestingLibraryRenderOptions & VueTestUtilsRenderOptions
58+
, "props" | "slots"> {
59+
props?: ComponentProps<C>;
60+
slots?: ExtractSlots<C>;
61+
}
62+
63+
export function render<C>(
64+
TestComponent: C,
65+
options?: RenderOptions<C>,
5366
): RenderResult
5467

5568
export type AsyncFireObject = {

types/index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function testWaitFor() {
6868
export function testOptions() {
6969
render(SomeComponent, {
7070
attrs: {a: 1},
71-
props: {c: 1}, // ideally it would fail because `c` is not an existing prop…
71+
props: {foo: 1},
7272
data: () => ({b: 2}),
7373
slots: {
7474
default: '<div />',

0 commit comments

Comments
 (0)