@@ -94,24 +94,22 @@ regarding the testing environment configuration. Since you're using this library
9494testing environment, making this error redundant.
9595
9696``` ts filename="useState.test.ts"
97- import {expect , test } from ' vitest' ;
9897import {act , renderHook } from ' @ver0/react-hooks-testing' ;
98+ import {useState } from ' react' ;
99+ import {expect , test } from ' vitest' ;
100+ import {expectResultValue } from ' ./test-helpers.test.js' ;
99101
100102test (' should use setState value' , async () => {
101103 const {result} = await renderHook (() => useState (' foo' ));
102104
103- expect (result .value ).toBe (' foo' );
104- expect (result .error ).toBe (undefined );
105-
106- if (result .value === undefined ) {
107- return ;
108- }
105+ const value = expectResultValue (result );
106+ expect (value [0 ]).toBe (' foo' );
109107
110108 await act (async () => {
111- result . value [1 ](' bar' );
109+ value [1 ](' bar' );
112110 });
113111
114- expect (result . value [0 ]).toBe (' bar' );
112+ expect (expectResultValue ( result ) [0 ]).toBe (' bar' );
115113});
116114```
117115
@@ -150,13 +148,44 @@ each render.
150148Another notable difference is the ` error ` property. This property captures any error thrown during the hook’s rendering,
151149thanks to an Error Boundary component wrapped around the hook’s harness component.
152150
153- Each result object is immutable and contains either a ` value ` or an ` error ` property—but never both. The hook’s result
154- object follows the same principle. Although the ` value ` and ` error ` properties are implemented as getters and always
155- exist, the values they return correspond to the most recent render result from the ` all ` array.
151+ Each result object is immutable and has either a ` value ` or an ` error ` property set. The hook's result object follows
152+ the same principle. Although the ` value ` and ` error ` properties are implemented as getters and always exist, the values
153+ they return correspond to the most recent render result from the ` all ` array.
154+
155+ > Though it is possible for both values to become ` undefined ` simultaneously, when a hook returns ` undefined ` as a valid
156+ > value, it is developer's responsibility to check error prior to using the value. The other theoretical possibility is
157+ > ` undefined ` being thrown, but that is considered a bad practice and thus not handled specifically.
156158
157159Otherwise, the API is similar to ` @testing-library/react ` . Note that the ` waitForNextUpdate ` function is not provided,
158160as modern testing frameworks include their own ` waitFor ` function, which serves the same purpose.
159161
162+ ** Type-Safety Helper**
163+
164+ While the discriminated union type provides strong type safety, manually checking for errors in every test can be
165+ annoyingly verbose. To ease usage while ensuring both type-safety and test coverage, you can introduce a helper
166+ function:
167+
168+ ``` ts filename="test-helpers.test.ts"
169+ import type {ResultValue } from ' @ver0/react-hooks-testing' ;
170+ import {expect } from ' vitest' ;
171+
172+ /**
173+ * Helper to assert that a hook result is successful and extract its value in a type-safe way.
174+ */
175+ export function expectResultValue<T >(result : ResultValue <T >) {
176+ expect (result .error ).toBeUndefined ();
177+
178+ if (result .error ) {
179+ throw new Error (' result has unexpected error' );
180+ }
181+
182+ return result .value ;
183+ }
184+ ```
185+
186+ This helper both asserts that no error occurred and narrows the TypeScript type, allowing you to work with
187+ ` result.value ` directly, as demonstrated in the example above.
188+
160189#### Testing server-side hooks
161190
162191The primary purpose of this package is to provide out-of-the-box SSR (Server-Side Rendering) support through the
0 commit comments