Skip to content

Commit 5ba6480

Browse files
committed
fix(tests): fix all failing tests
1. Remove `/\\.` from `vitest.config.ts`. This was necessary to fix the error `No test files found`. This can also be fixed by updating Vitest to version 4, but this has a change to how snapshots are stored. See [the migration guide](https://vitest.dev/guide/migration.html#changes-to-mocking). All of the tests are found by removing `/\\.`, so the Vitest upgrade is better in a later PR. 1. Inlined `z` in `vitest.config.ts` to spy on `parse()` from `zod/v4/core`. Without this, a `TypeError: Cannot redefine property: parse` error occurs. 1. Change `bun test` to `bun run test` in CONTRIBUTING.md. `bun test` runs Bun's native test runner, not the `test` script. 1. Add `Test` stage to `.github/workflows/main.yml`. It was removed in [this PR](react-hook-form#699) and the tests haven't been running in CI. 1. Fix failing tests Notes: 1. All * Fixed tests that called `useForm` outside of a component. This fixed the errors `Error: Invalid hook call. Hooks can only be called inside of the body of a function component.` and `TypeError: Cannot read properties of null (reading 'useRef')`. 1. Arktype * [This PR](react-hook-form#753) changed from directly calling Arktype to using standard schema. Part of this transition was changing `type: error.code` to `type: ''` (because `error.code` is not part of the standard schema). This required an update to the snapshot. 1. Superstruct * Fix validation of schemas that perform coercion (`{ coerce: true }` must be passed in) * Added additional overload for type definition `superstructResolver` that improves handling of coerced schemas (This fixes a type error with the form values). * Fix definition of coercion schema so that it correctly converts from number to string * As mentioned in [this issue](ianstormtaylor/superstruct#1159), there is no way to infer the input type of a coerced schema. A [separate PR](ianstormtaylor/superstruct#1181) would be necessary. Some of the type inference tests have been changed to manually provide the input type (this enables `watch` to return the correct value for a coerced schema). 1. Typeschema * The `for` loop was looping through the `schemaErrors` variable, but the current error was based on the `typeschemaErrors[0]` variable. This caused only the first error to be returned. * Change from spying on `typeschema.validate` to watching `schema["~standard"]` 1. Zod * Fixed Zod tests that spied on `schema.parse()` instead of `z4.parse()` * Added missing snapshots
1 parent 2e9bc7c commit 5ba6480

25 files changed

Lines changed: 1646 additions & 146 deletions

File tree

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828

2929
- name: Lint
3030
run: bun lint
31+
32+
- name: Test
33+
run: bun run test
3134

3235
- name: TS
3336
run: bun lint:types

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Here is a quick guide to doing code contributions to the library.
1616
1717
4. If you've added a code that should be tested, ensure the test suite still passes.
1818

19-
> bun test
19+
> bun run test
2020
2121
5. Try to write some unit tests to cover as much of your code as possible.
2222

arktype/src/__tests__/__snapshots__/arktype.ts.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,41 @@ exports[`arktypeResolver > should return a single error from arktypeResolver whe
66
"accessToken": {
77
"message": "accessToken must be a number or a string (was missing)",
88
"ref": undefined,
9-
"type": "required",
9+
"type": "",
1010
},
1111
"birthYear": {
1212
"message": "birthYear must be a number (was a string)",
1313
"ref": undefined,
14-
"type": "domain",
14+
"type": "",
1515
},
1616
"dateStr": {
1717
"message": "dateStr must be a Date (was missing)",
1818
"ref": undefined,
19-
"type": "required",
19+
"type": "",
2020
},
2121
"email": {
2222
"message": "email must be an email address (was "")",
2323
"ref": {
2424
"name": "email",
2525
},
26-
"type": "pattern",
26+
"type": "",
2727
},
2828
"enabled": {
2929
"message": "enabled must be boolean (was missing)",
3030
"ref": undefined,
31-
"type": "required",
31+
"type": "",
3232
},
3333
"like": [
3434
{
3535
"id": {
3636
"message": "like[0].id must be a number (was a string)",
3737
"ref": undefined,
38-
"type": "domain",
38+
"type": "",
3939
},
4040
"name": {
4141
"message": "like[0].name must be a string (was missing)",
4242
"ref": undefined,
43-
"type": "required",
43+
"type": "",
4444
},
4545
},
4646
],
@@ -49,24 +49,24 @@ exports[`arktypeResolver > should return a single error from arktypeResolver whe
4949
"ref": {
5050
"name": "password",
5151
},
52-
"type": "union",
52+
"type": "",
5353
},
5454
"repeatPassword": {
5555
"message": "repeatPassword must be a string (was missing)",
5656
"ref": undefined,
57-
"type": "required",
57+
"type": "",
5858
},
5959
"tags": {
6060
"message": "tags must be an array (was missing)",
6161
"ref": undefined,
62-
"type": "required",
62+
"type": "",
6363
},
6464
"username": {
6565
"message": "username must be a string (was missing)",
6666
"ref": {
6767
"name": "username",
6868
},
69-
"type": "required",
69+
"type": "",
7070
},
7171
},
7272
"values": {},

arktype/src/__tests__/arktype.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { renderHook } from '@testing-library/react';
12
import { type } from 'arktype';
23
import { Resolver, useForm } from 'react-hook-form';
34
import { SubmitHandler } from 'react-hook-form';
@@ -51,9 +52,13 @@ describe('arktypeResolver', () => {
5152
it('should correctly infer the output type from a arktype schema for the handleSubmit function in useForm', () => {
5253
const schema = type({ id: 'number' });
5354

54-
const form = useForm({
55-
resolver: arktypeResolver(schema),
56-
});
55+
const {
56+
result: { current: form },
57+
} = renderHook(() =>
58+
useForm({
59+
resolver: arktypeResolver(schema),
60+
}),
61+
);
5762

5863
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
5964

@@ -67,9 +72,13 @@ describe('arktypeResolver', () => {
6772
it('should correctly infer the output type from a arktype schema with a transform for the handleSubmit function in useForm', () => {
6873
const schema = type({ id: type('string').pipe((s) => Number.parseInt(s)) });
6974

70-
const form = useForm({
71-
resolver: arktypeResolver(schema),
72-
});
75+
const {
76+
result: { current: form },
77+
} = renderHook(() =>
78+
useForm({
79+
resolver: arktypeResolver(schema),
80+
}),
81+
);
7382

7483
expectTypeOf(form.watch('id')).toEqualTypeOf<string>();
7584

bun.lock

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

computed-types/src/__tests__/computed-types.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { renderHook } from '@testing-library/react';
12
import Schema, { number } from 'computed-types';
23
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
34
import { computedTypesResolver } from '..';
@@ -65,9 +66,13 @@ describe('computedTypesResolver', () => {
6566
it('should correctly infer the output type from a computedTypes schema for the handleSubmit function in useForm', () => {
6667
const schema = Schema({ id: number });
6768

68-
const form = useForm({
69-
resolver: computedTypesResolver(schema),
70-
});
69+
const {
70+
result: { current: form },
71+
} = renderHook(() =>
72+
useForm({
73+
resolver: computedTypesResolver(schema),
74+
}),
75+
);
7176

7277
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
7378

@@ -81,9 +86,13 @@ describe('computedTypesResolver', () => {
8186
it('should correctly infer the output type from a computedTypes schema with a transform for the handleSubmit function in useForm', () => {
8287
const schema = Schema({ id: number.transform((val) => String(val)) });
8388

84-
const form = useForm({
85-
resolver: computedTypesResolver(schema),
86-
});
89+
const {
90+
result: { current: form },
91+
} = renderHook(() =>
92+
useForm({
93+
resolver: computedTypesResolver(schema),
94+
}),
95+
);
8796

8897
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
8998

effect-ts/src/__tests__/effect-ts.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { renderHook } from '@testing-library/react';
12
import { Schema } from 'effect';
23
import { Resolver, useForm } from 'react-hook-form';
34
import { SubmitHandler } from 'react-hook-form';
@@ -114,9 +115,13 @@ describe('effectTsResolver', () => {
114115
it('should correctly infer the output type from a effectTs schema for the handleSubmit function in useForm', () => {
115116
const schema = Schema.Struct({ id: Schema.Number });
116117

117-
const form = useForm({
118-
resolver: effectTsResolver(schema),
119-
});
118+
const {
119+
result: { current: form },
120+
} = renderHook(() =>
121+
useForm({
122+
resolver: effectTsResolver(schema),
123+
}),
124+
);
120125

121126
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
122127

@@ -133,9 +138,13 @@ describe('effectTsResolver', () => {
133138
}),
134139
});
135140

136-
const form = useForm({
137-
resolver: effectTsResolver(schema),
138-
});
141+
const {
142+
result: { current: form },
143+
} = renderHook(() =>
144+
useForm({
145+
resolver: effectTsResolver(schema),
146+
}),
147+
);
139148

140149
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
141150

io-ts/src/__tests__/io-ts.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { renderHook } from '@testing-library/react';
12
import * as t from 'io-ts';
23
import * as tt from 'io-ts-types';
34
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
@@ -60,9 +61,13 @@ describe('ioTsResolver', () => {
6061
it('should correctly infer the output type from a io-ts schema for the handleSubmit function in useForm', () => {
6162
const schema = t.type({ id: t.number });
6263

63-
const form = useForm({
64-
resolver: ioTsResolver(schema),
65-
});
64+
const {
65+
result: { current: form },
66+
} = renderHook(() =>
67+
useForm({
68+
resolver: ioTsResolver(schema),
69+
}),
70+
);
6671

6772
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
6873

@@ -76,9 +81,13 @@ describe('ioTsResolver', () => {
7681
it('should correctly infer the output type from a io-ts schema with a transform for the handleSubmit function in useForm', () => {
7782
const schema = t.type({ id: tt.NumberFromString });
7883

79-
const form = useForm({
80-
resolver: ioTsResolver(schema),
81-
});
84+
const {
85+
result: { current: form },
86+
} = renderHook(() =>
87+
useForm({
88+
resolver: ioTsResolver(schema),
89+
}),
90+
);
8291

8392
expectTypeOf(form.watch('id')).toEqualTypeOf<string>();
8493

standard-schema/src/__tests__/standard-schema.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { renderHook } from '@testing-library/react';
12
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
23
import { z } from 'zod/v3';
34
import { standardSchemaResolver } from '..';
@@ -113,13 +114,16 @@ describe('standardSchemaResolver', () => {
113114

114115
it('should correctly infer the output type from a standardSchema schema for the handleSubmit function in useForm', () => {
115116
const schema = z.object({ id: z.number() });
116-
117-
const form = useForm({
118-
resolver: standardSchemaResolver(schema),
119-
defaultValues: {
120-
id: 3,
121-
},
122-
});
117+
const {
118+
result: { current: form },
119+
} = renderHook(() =>
120+
useForm({
121+
resolver: standardSchemaResolver(schema),
122+
defaultValues: {
123+
id: 3,
124+
},
125+
}),
126+
);
123127

124128
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
125129

@@ -133,12 +137,16 @@ describe('standardSchemaResolver', () => {
133137
it('should correctly infer the output type from a standardSchema schema with a transform for the handleSubmit function in useForm', () => {
134138
const schema = z.object({ id: z.number().transform((val) => String(val)) });
135139

136-
const form = useForm({
137-
resolver: standardSchemaResolver(schema),
138-
defaultValues: {
139-
id: 3,
140-
},
141-
});
140+
const {
141+
result: { current: form },
142+
} = renderHook(() =>
143+
useForm({
144+
resolver: standardSchemaResolver(schema),
145+
defaultValues: {
146+
id: 3,
147+
},
148+
}),
149+
);
142150

143151
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
144152

superstruct/src/__tests__/superstruct.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { renderHook } from '@testing-library/react';
12
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
23
import * as s from 'superstruct';
34
import { superstructResolver } from '..';
@@ -18,8 +19,9 @@ describe('superstructResolver', () => {
1819
it('should return values from superstructResolver with coerced values', async () => {
1920
const result = await superstructResolver(
2021
s.object({
21-
id: s.coerce(s.number(), s.string(), (val) => String(val)),
22+
id: s.coerce(s.string(), s.number(), (val) => String(val)),
2223
}),
24+
{ coerce: true },
2325
)({ id: 1 }, undefined, {
2426
fields,
2527
shouldUseNativeValidation,
@@ -64,9 +66,13 @@ describe('superstructResolver', () => {
6466
it('should correctly infer the output type from a superstruct schema for the handleSubmit function in useForm', () => {
6567
const schema = s.object({ id: s.number() });
6668

67-
const form = useForm({
68-
resolver: superstructResolver(schema),
69-
});
69+
const {
70+
result: { current: form },
71+
} = renderHook(() =>
72+
useForm({
73+
resolver: superstructResolver(schema, {}),
74+
}),
75+
);
7076

7177
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
7278

@@ -82,11 +88,19 @@ describe('superstructResolver', () => {
8288
id: s.coerce(s.string(), s.number(), (val) => String(val)),
8389
});
8490

85-
const form = useForm({
86-
resolver: superstructResolver(schema),
87-
});
91+
const {
92+
result: { current: form },
93+
} = renderHook(() =>
94+
useForm({
95+
// With coerce, there is no way to infer the input type of the schema
96+
resolver: superstructResolver<{ id: number }, unknown, { id: string }>(
97+
schema,
98+
{ coerce: true },
99+
),
100+
}),
101+
);
88102

89-
expectTypeOf(form.watch('id')).toEqualTypeOf<string>();
103+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
90104

91105
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
92106
SubmitHandler<{

0 commit comments

Comments
 (0)