Skip to content

Commit 7c122d2

Browse files
committed
Add type test
1 parent 568328d commit 7c122d2

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

packages/start-client-core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@
8484
"seroval": "^1.3.2",
8585
"tiny-invariant": "^1.3.3",
8686
"tiny-warning": "^1.0.3"
87+
},
88+
"devDependencies": {
89+
"zod": "^3.24.2"
8790
}
8891
}

packages/start-client-core/src/tests/createServerFn.test-d.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expectTypeOf, test } from 'vitest'
2+
import { z } from 'zod'
23
import { createMiddleware } from '../createMiddleware'
34
import { createServerFn } from '../createServerFn'
45
import { TSS_SERVER_FUNCTION } from '../constants'
@@ -62,6 +63,76 @@ test('createServerFn with validator', () => {
6263
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
6364
})
6465

66+
test('createServerFn with standard validator', () => {
67+
const schema = z
68+
.object({ input: z.string() })
69+
.transform(({ input }) => ({ value: { a: input } }))
70+
71+
const fnAfterValidator = createServerFn({
72+
method: 'GET',
73+
}).inputValidator(schema)
74+
75+
expectTypeOf(fnAfterValidator).toHaveProperty('handler')
76+
expectTypeOf(fnAfterValidator).toHaveProperty('middleware')
77+
expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator')
78+
79+
const fn = fnAfterValidator.handler((options) => {
80+
expectTypeOf(options).toEqualTypeOf<{
81+
method: 'GET'
82+
context: undefined
83+
data: {
84+
value: { a: string }
85+
}
86+
signal: AbortSignal
87+
}>()
88+
})
89+
90+
expectTypeOf(fn).parameter(0).toEqualTypeOf<{
91+
data: { input: string }
92+
headers?: HeadersInit
93+
signal?: AbortSignal
94+
}>()
95+
96+
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
97+
})
98+
99+
test('createServerFn with async standard validator', () => {
100+
const schema = z
101+
.object({
102+
input: z
103+
.string()
104+
.refine(async (val) => Promise.resolve(val !== 'invalid')),
105+
})
106+
.transform(({ input }) => ({ value: { a: input } }))
107+
108+
const fnAfterValidator = createServerFn({
109+
method: 'GET',
110+
}).inputValidator(schema)
111+
112+
expectTypeOf(fnAfterValidator).toHaveProperty('handler')
113+
expectTypeOf(fnAfterValidator).toHaveProperty('middleware')
114+
expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator')
115+
116+
const fn = fnAfterValidator.handler((options) => {
117+
expectTypeOf(options).toEqualTypeOf<{
118+
method: 'GET'
119+
context: undefined
120+
data: {
121+
value: { a: string }
122+
}
123+
signal: AbortSignal
124+
}>()
125+
})
126+
127+
expectTypeOf(fn).parameter(0).toEqualTypeOf<{
128+
data: { input: string }
129+
headers?: HeadersInit
130+
signal?: AbortSignal
131+
}>()
132+
133+
expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>()
134+
})
135+
65136
test('createServerFn with middleware and context', () => {
66137
const middleware1 = createMiddleware({ type: 'function' }).server(
67138
({ next }) => {

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)