@tanstack-router-testing/react-start-testing 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +100 -0
- package/dist/index.d.mts +399 -0
- package/dist/index.mjs +337 -0
- package/dist/index.mjs.map +1 -0
- package/dist/shim-DR16QaLL.mjs +486 -0
- package/dist/shim-DR16QaLL.mjs.map +1 -0
- package/dist/shim.d.mts +19 -0
- package/dist/shim.mjs +3 -0
- package/dist/vite.d.mts +40 -0
- package/dist/vite.mjs +61 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present eve0415
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# tanstack-router-testing
|
|
2
|
+
|
|
3
|
+
Unit testing utilities for [TanStack Router](https://tanstack.com/router) and [TanStack Start](https://tanstack.com/start).
|
|
4
|
+
|
|
5
|
+
## Quick start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add -D @tanstack-router-testing/react-router-testing @tanstack-router-testing/react-start-testing
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Configure Vitest with the Start shim:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// vitest.config.ts
|
|
15
|
+
import { tanstackStartTesting } from '@tanstack-router-testing/react-start-testing/vite';
|
|
16
|
+
import { defineConfig } from 'vitest/config';
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
plugins: [tanstackStartTesting()],
|
|
20
|
+
test: { environment: 'jsdom' },
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Write your first test — import a single route file, pass it to the harness, and assert:
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
// posts.$postId.test.tsx
|
|
28
|
+
import { render } from '@testing-library/react';
|
|
29
|
+
import { describe, expect, it } from 'vitest';
|
|
30
|
+
import { createRouterHarness } from '@tanstack-router-testing/react-router-testing';
|
|
31
|
+
import { Route } from './routes/posts.$postId';
|
|
32
|
+
|
|
33
|
+
describe('post route', () => {
|
|
34
|
+
it('loads and renders a post', async () => {
|
|
35
|
+
const harness = createRouterHarness({
|
|
36
|
+
route: Route,
|
|
37
|
+
params: { postId: '42' }, // fully typed from route path
|
|
38
|
+
loaderData: { id: '42', title: 'Hello' }, // skip the real loader
|
|
39
|
+
});
|
|
40
|
+
await harness.load();
|
|
41
|
+
const { findByText } = render(<harness.TestRouterProvider />);
|
|
42
|
+
await expect(findByText('Hello')).resolves.toBeTruthy();
|
|
43
|
+
harness.cleanup();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Or use a full route tree for integration-level tests:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { createRouterHarness } from '@tanstack-router-testing/react-router-testing';
|
|
52
|
+
import { routeTree } from './routeTree.gen';
|
|
53
|
+
|
|
54
|
+
const harness = createRouterHarness({ routeTree, initialEntries: ['/posts/7'] });
|
|
55
|
+
await harness.load();
|
|
56
|
+
expect(harness.getLoaderData('/posts/$postId')).toBeDefined();
|
|
57
|
+
harness.cleanup();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Packages
|
|
61
|
+
|
|
62
|
+
| Package | Purpose |
|
|
63
|
+
| --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
|
|
64
|
+
| [`react-router-testing`](./packages/react-router-testing) | `createRouterHarness` (file route or full tree), `createTestRouter`, SSR harness |
|
|
65
|
+
| [`react-start-testing`](./packages/react-start-testing) | `mockServerFn`, `mockMiddleware`, `createStartTestRuntime`, RSC runtime, Vite plugin |
|
|
66
|
+
| [`router-testing-core`](./packages/router-testing-core) | Internal registry/env runtime (transitive dependency) |
|
|
67
|
+
| [`react-start-testing-storybook`](./packages/react-start-testing-storybook) | Storybook decorator for Start stories |
|
|
68
|
+
|
|
69
|
+
All packages are scoped under `@tanstack-router-testing/`.
|
|
70
|
+
|
|
71
|
+
## Design principles
|
|
72
|
+
|
|
73
|
+
1. **Real router, not route stubs.** Tests exercise `createRouter`, route trees, memory history, loaders, guards, params, search, and redirects through the router itself.
|
|
74
|
+
2. **Production-shaped Start calls.** Server functions are called as `await fn({ data })` — the harness supplies mocks and leaves the call shape alone.
|
|
75
|
+
3. **Upstream-mergeable shape.** Public exports are shaped for future first-party subpaths (`@tanstack/react-router/testing`).
|
|
76
|
+
|
|
77
|
+
## Documentation
|
|
78
|
+
|
|
79
|
+
- [Getting Started](./docs/getting-started.md)
|
|
80
|
+
- [API Reference](./docs/api-reference.md)
|
|
81
|
+
- **Guides:** [Loaders](./docs/guides/testing-loaders.md) | [Guards & Redirects](./docs/guides/testing-guards.md) | [Server Functions](./docs/guides/testing-server-functions.md) | [Middleware](./docs/guides/testing-middleware.md) | [SSR](./docs/guides/testing-ssr.md) | [RSC](./docs/guides/testing-rsc.md) | [Query Integration](./docs/guides/testing-with-query.md)
|
|
82
|
+
- [Example Tests](./docs/examples/)
|
|
83
|
+
|
|
84
|
+
## Running tests
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pnpm run build # build all packages
|
|
88
|
+
pnpm test:unit:core # fast core + integration tests
|
|
89
|
+
pnpm test:types # type-level assertions
|
|
90
|
+
pnpm test:unit:vendored # 110 vendored TanStack app smoke tests
|
|
91
|
+
pnpm run lint:check # oxlint + oxfmt
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Contributing
|
|
95
|
+
|
|
96
|
+
This project aims to become an upstream contribution to `tanstack/router`. Feedback, issues, and PRs welcome.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
[MIT](./LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { AnyFn, CallMiddlewareOptions, CallMiddlewareResult, TestEnv, callMiddleware } from "@tanstack-router-testing/router-testing-core";
|
|
2
|
+
import { ComponentType } from "react";
|
|
3
|
+
import { StartHandlerType } from "@tanstack/start-storage-context";
|
|
4
|
+
import { AnyRouter } from "@tanstack/react-router";
|
|
5
|
+
import { AnyStartInstanceOptions } from "@tanstack/start-client-core";
|
|
6
|
+
|
|
7
|
+
//#region src/clearStartMocks.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Clear every server-function and middleware mock installed by
|
|
10
|
+
* {@link mockServerFn} or {@link mockMiddleware}.
|
|
11
|
+
*
|
|
12
|
+
* @returns `void`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { afterEach } from 'vitest';
|
|
17
|
+
* import { clearStartMocks } from '@tanstack-router-testing/react-start-testing';
|
|
18
|
+
*
|
|
19
|
+
* afterEach(() => {
|
|
20
|
+
* clearStartMocks();
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* This is the same function wired to {@link StartTestRuntime.cleanup}, so you
|
|
26
|
+
* only need to call it explicitly when installing mocks outside of a runtime
|
|
27
|
+
* (i.e. via {@link mockServerFn} / {@link mockMiddleware} directly).
|
|
28
|
+
*/
|
|
29
|
+
declare const clearStartMocks: () => void;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/mockServerFn.d.ts
|
|
32
|
+
/**
|
|
33
|
+
* Catch-all type representing any TanStack Start server function.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Used as a generic constraint in {@link mockServerFn} and {@link ServerFnMock}
|
|
37
|
+
* to accept any function created by `createServerFn().handler(...)`.
|
|
38
|
+
*/
|
|
39
|
+
type AnyServerFn = (...args: never[]) => unknown;
|
|
40
|
+
/**
|
|
41
|
+
* The mock implementation signature for a server function of type `TFn`.
|
|
42
|
+
*
|
|
43
|
+
* @typeParam TFn - The server function type being mocked.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* The mock receives the same callable-shape arguments as the real server
|
|
47
|
+
* function (e.g. `{ data }`) and must return a compatible result. Full generic
|
|
48
|
+
* inference is preserved so autocomplete works identically to the real call.
|
|
49
|
+
*
|
|
50
|
+
* @see {@link mockServerFn}
|
|
51
|
+
*/
|
|
52
|
+
type ServerFnMock<TFn extends AnyServerFn> = (...args: Parameters<TFn>) => ReturnType<TFn>;
|
|
53
|
+
/**
|
|
54
|
+
* Install a mock implementation for a TanStack Start server function.
|
|
55
|
+
*
|
|
56
|
+
* @param fn - The server function created by `createServerFn().handler(...)`.
|
|
57
|
+
* @param impl - A {@link ServerFnMock} that replaces the real handler for the
|
|
58
|
+
* duration of the test. It receives the same callable-shape arguments (e.g.
|
|
59
|
+
* `{ data }`) as the original function.
|
|
60
|
+
* @returns A disposer function that restores the original implementation when
|
|
61
|
+
* called. Alternatively, call {@link clearStartMocks} to remove all mocks at
|
|
62
|
+
* once.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { createServerFn } from '@tanstack/react-start';
|
|
67
|
+
* import { mockServerFn } from '@tanstack-router-testing/react-start-testing';
|
|
68
|
+
*
|
|
69
|
+
* const listOrders = createServerFn()
|
|
70
|
+
* .validator((input: { userId: string }) => input)
|
|
71
|
+
* .handler(async ({ data }) => db.orders.findMany(data.userId));
|
|
72
|
+
*
|
|
73
|
+
* const dispose = mockServerFn(listOrders, async ({ data }) => [
|
|
74
|
+
* { id: '1', userId: data.userId, total: 42 },
|
|
75
|
+
* ]);
|
|
76
|
+
*
|
|
77
|
+
* // ... run your test ...
|
|
78
|
+
* dispose();
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* The mock is authored against the public callable shape, so a server function
|
|
83
|
+
* normally called as `listOrders({ data })` is mocked with that same signature.
|
|
84
|
+
* Internal context fields are normalised automatically before reaching the mock.
|
|
85
|
+
*/
|
|
86
|
+
declare const mockServerFn: <TFn extends AnyServerFn>(fn: TFn, impl: ServerFnMock<TFn>) => (() => void);
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/mockMiddleware.d.ts
|
|
89
|
+
/**
|
|
90
|
+
* Phase override options for {@link mockMiddleware}.
|
|
91
|
+
*
|
|
92
|
+
* @remarks
|
|
93
|
+
* Provide `client`, `server`, or both. Omitted keys leave the corresponding
|
|
94
|
+
* phase untouched, so you can mock only the server side without affecting the
|
|
95
|
+
* client phase and vice-versa.
|
|
96
|
+
*/
|
|
97
|
+
interface MockMiddlewareOptions {
|
|
98
|
+
/** Override for the `.client()` phase. When omitted the real client handler runs. */
|
|
99
|
+
readonly client?: AnyFn;
|
|
100
|
+
/** Override for the `.server()` phase. When omitted the real server handler runs. */
|
|
101
|
+
readonly server?: AnyFn;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Override one or both phases of a registered middleware.
|
|
105
|
+
*
|
|
106
|
+
* @param mw - The middleware object returned by
|
|
107
|
+
* `createMiddleware().server(...).client(...)`.
|
|
108
|
+
* @param options - A {@link MockMiddlewareOptions} object specifying which
|
|
109
|
+
* phases to replace.
|
|
110
|
+
* @returns A disposer function that restores the original middleware handlers.
|
|
111
|
+
* Alternatively, call {@link clearStartMocks} to remove all mocks at once.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { createMiddleware } from '@tanstack/react-start';
|
|
116
|
+
* import { mockMiddleware } from '@tanstack-router-testing/react-start-testing';
|
|
117
|
+
*
|
|
118
|
+
* const authMiddleware = createMiddleware()
|
|
119
|
+
* .server(async ({ next }) => next({ context: { user: await getUser() } }))
|
|
120
|
+
* .client(async ({ next }) => next());
|
|
121
|
+
*
|
|
122
|
+
* const dispose = mockMiddleware(authMiddleware, {
|
|
123
|
+
* server: async ({ next }) => next({ context: { user: { id: 'test-user' } } }),
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // ... run your test ...
|
|
127
|
+
* dispose();
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @throws If `mw` is not registered. Registration is the shim's
|
|
131
|
+
* responsibility (see ADR 0001).
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* Server functions are invoked directly and mocked middleware participates in
|
|
135
|
+
* that normal call path. Only the phases you specify in `options` are replaced;
|
|
136
|
+
* the rest continue to run their real implementations.
|
|
137
|
+
*/
|
|
138
|
+
declare const mockMiddleware: (mw: object, options: MockMiddlewareOptions) => (() => void);
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/isomorphic.d.ts
|
|
141
|
+
/**
|
|
142
|
+
* Execute `fn` with the simulated TanStack Start environment forced to `env`,
|
|
143
|
+
* then restore the prior value.
|
|
144
|
+
*
|
|
145
|
+
* @typeParam T - The return type of `fn`.
|
|
146
|
+
* @param env - The environment to simulate: `'server'` or `'client'`.
|
|
147
|
+
* @param fn - A synchronous or asynchronous function to run inside the
|
|
148
|
+
* simulated environment.
|
|
149
|
+
* @returns A `Promise` resolving to the return value of `fn`.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { runInStartEnv } from '@tanstack-router-testing/react-start-testing';
|
|
154
|
+
*
|
|
155
|
+
* const result = await runInStartEnv('server', () => {
|
|
156
|
+
* // Code here sees `import.meta.env.SSR === true`
|
|
157
|
+
* return fetchDataOnServer();
|
|
158
|
+
* });
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @remarks
|
|
162
|
+
* The environment is scoped to the execution of `fn` and is restored
|
|
163
|
+
* automatically even if `fn` throws. Used internally by
|
|
164
|
+
* {@link createStartTestRuntime} to set the environment for each
|
|
165
|
+
* {@link StartTestRuntime.run | run} invocation.
|
|
166
|
+
*/
|
|
167
|
+
declare const runInStartEnv: <T>(env: TestEnv, fn: () => T | Promise<T>) => Promise<T>;
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/runtime.d.ts
|
|
170
|
+
/**
|
|
171
|
+
* Configuration for {@link createStartTestRuntime}.
|
|
172
|
+
*
|
|
173
|
+
* @remarks
|
|
174
|
+
* At most one of `startInstance` and `startOptions` should be provided. If both
|
|
175
|
+
* are given, `startOptions` takes precedence. When neither is supplied the
|
|
176
|
+
* runtime creates an empty options object.
|
|
177
|
+
*/
|
|
178
|
+
interface StartTestRuntimeOptions {
|
|
179
|
+
/**
|
|
180
|
+
* A Start instance whose `getOptions()` method is called once during
|
|
181
|
+
* runtime creation. Ignored when {@link startOptions} is also provided.
|
|
182
|
+
*/
|
|
183
|
+
readonly startInstance?: {
|
|
184
|
+
readonly getOptions: () => AnyStartInstanceOptions | Promise<AnyStartInstanceOptions>;
|
|
185
|
+
};
|
|
186
|
+
/** Explicit Start options to use instead of resolving from a `startInstance`. */
|
|
187
|
+
readonly startOptions?: AnyStartInstanceOptions;
|
|
188
|
+
/**
|
|
189
|
+
* The incoming request available to server functions via `getRequest()`.
|
|
190
|
+
* Accepts a full `Request`, a URL string, or a `URL` object.
|
|
191
|
+
* Defaults to `http://tanstack-router-testing.test/`.
|
|
192
|
+
*/
|
|
193
|
+
readonly request?: Request | string | URL;
|
|
194
|
+
/** The TanStack Router instance made available via `getRouter()` inside server functions. */
|
|
195
|
+
readonly router?: AnyRouter;
|
|
196
|
+
/** Initial middleware context. Merged into each call's context before global request middlewares run. */
|
|
197
|
+
readonly context?: unknown;
|
|
198
|
+
/** Default simulated environment. Defaults to `'server'`. */
|
|
199
|
+
readonly env?: TestEnv;
|
|
200
|
+
/** Default handler type passed to the storage context. Defaults to `'serverFn'`. */
|
|
201
|
+
readonly handlerType?: StartHandlerType;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Per-invocation overrides passed to {@link StartTestRuntime.run} and
|
|
205
|
+
* {@link StartTestRuntime.call}.
|
|
206
|
+
*
|
|
207
|
+
* @remarks
|
|
208
|
+
* Every property mirrors a field from {@link StartTestRuntimeOptions}. When
|
|
209
|
+
* provided here it takes precedence over the runtime-level default for that
|
|
210
|
+
* single invocation only.
|
|
211
|
+
*/
|
|
212
|
+
interface StartTestRunOptions {
|
|
213
|
+
/** Override the request for this invocation. */
|
|
214
|
+
readonly request?: Request | string | URL;
|
|
215
|
+
/** Override the middleware context for this invocation. */
|
|
216
|
+
readonly context?: unknown;
|
|
217
|
+
/** Override the simulated environment (`'server'` or `'client'`) for this invocation. */
|
|
218
|
+
readonly env?: TestEnv;
|
|
219
|
+
/** Override the handler type for this invocation. */
|
|
220
|
+
readonly handlerType?: StartHandlerType;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* A test runtime that simulates the TanStack Start server environment.
|
|
224
|
+
*
|
|
225
|
+
* @remarks
|
|
226
|
+
* Created by {@link createStartTestRuntime}. Provides the storage context,
|
|
227
|
+
* request, and environment that server functions expect at runtime. Call
|
|
228
|
+
* {@link cleanup} (or {@link clearStartMocks}) in `afterEach` to restore all
|
|
229
|
+
* mocks.
|
|
230
|
+
*/
|
|
231
|
+
interface StartTestRuntime {
|
|
232
|
+
/** The `Request` object available to server functions via `getRequest()`. */
|
|
233
|
+
readonly request: Request;
|
|
234
|
+
/** The resolved Start options used by the runtime. */
|
|
235
|
+
readonly startOptions: AnyStartInstanceOptions;
|
|
236
|
+
/**
|
|
237
|
+
* Run an arbitrary function inside the Start storage context.
|
|
238
|
+
*
|
|
239
|
+
* @typeParam T - The return type of `fn`.
|
|
240
|
+
* @param fn - The function to execute.
|
|
241
|
+
* @param options - Optional per-invocation overrides. See {@link StartTestRunOptions}.
|
|
242
|
+
* @returns A `Promise` resolving to the return value of `fn`.
|
|
243
|
+
*/
|
|
244
|
+
readonly run: <T>(fn: () => T | Promise<T>, options?: StartTestRunOptions) => Promise<T>;
|
|
245
|
+
/**
|
|
246
|
+
* Invoke a server function (or any callable) with explicit arguments inside
|
|
247
|
+
* the Start storage context.
|
|
248
|
+
*
|
|
249
|
+
* @typeParam TArgs - Tuple of argument types.
|
|
250
|
+
* @typeParam TReturn - The return type of `fn`.
|
|
251
|
+
* @param fn - The function to call.
|
|
252
|
+
* @param args - Arguments forwarded to `fn`.
|
|
253
|
+
* @param options - Optional per-invocation overrides. See {@link StartTestRunOptions}.
|
|
254
|
+
* @returns A `Promise` resolving to the awaited return value of `fn`.
|
|
255
|
+
*/
|
|
256
|
+
readonly call: <TArgs extends readonly unknown[], TReturn>(fn: (...args: TArgs) => TReturn | Promise<TReturn>, args: TArgs, options?: StartTestRunOptions) => Promise<Awaited<TReturn>>;
|
|
257
|
+
/**
|
|
258
|
+
* Remove all server-function and middleware mocks. Equivalent to calling
|
|
259
|
+
* {@link clearStartMocks}.
|
|
260
|
+
*/
|
|
261
|
+
readonly cleanup: () => void;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Create a {@link StartTestRuntime} for executing server functions in a
|
|
265
|
+
* simulated TanStack Start environment.
|
|
266
|
+
*
|
|
267
|
+
* @param options - Runtime configuration. See {@link StartTestRuntimeOptions}.
|
|
268
|
+
* @returns A `Promise` resolving to a {@link StartTestRuntime} instance.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* import { createServerFn } from '@tanstack/react-start';
|
|
273
|
+
* import {
|
|
274
|
+
* createStartTestRuntime,
|
|
275
|
+
* mockServerFn,
|
|
276
|
+
* } from '@tanstack-router-testing/react-start-testing';
|
|
277
|
+
*
|
|
278
|
+
* const runtime = await createStartTestRuntime({
|
|
279
|
+
* request: 'http://localhost:3000/api/orders',
|
|
280
|
+
* env: 'server',
|
|
281
|
+
* });
|
|
282
|
+
*
|
|
283
|
+
* const listOrders = createServerFn()
|
|
284
|
+
* .validator((input: { userId: string }) => input)
|
|
285
|
+
* .handler(async ({ data }) => [{ id: '1', userId: data.userId }]);
|
|
286
|
+
*
|
|
287
|
+
* mockServerFn(listOrders, async ({ data }) => [
|
|
288
|
+
* { id: 'mock-1', userId: data.userId },
|
|
289
|
+
* ]);
|
|
290
|
+
*
|
|
291
|
+
* const orders = await runtime.call(listOrders, [{ data: { userId: 'u1' } }]);
|
|
292
|
+
* // orders === [{ id: 'mock-1', userId: 'u1' }]
|
|
293
|
+
*
|
|
294
|
+
* runtime.cleanup();
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* @remarks
|
|
298
|
+
* The runtime resolves Start options from either `startOptions` or
|
|
299
|
+
* `startInstance.getOptions()`, sets up the Start storage context, and
|
|
300
|
+
* executes global request middlewares before each invocation.
|
|
301
|
+
*/
|
|
302
|
+
declare const createStartTestRuntime: (options?: StartTestRuntimeOptions) => Promise<StartTestRuntime>;
|
|
303
|
+
//#endregion
|
|
304
|
+
//#region src/rsc.d.ts
|
|
305
|
+
/**
|
|
306
|
+
* Configuration for {@link createRscTestRuntime}.
|
|
307
|
+
*
|
|
308
|
+
* @remarks
|
|
309
|
+
* Extends {@link StartTestRuntimeOptions} with an additional `streaming` flag
|
|
310
|
+
* that controls the server rendering mode.
|
|
311
|
+
*/
|
|
312
|
+
interface RscTestRuntimeOptions extends StartTestRuntimeOptions {
|
|
313
|
+
/**
|
|
314
|
+
* When `true`, renders via `renderToReadableStream` and collects individual
|
|
315
|
+
* chunks. When `false` (default), uses `renderToString` for a single
|
|
316
|
+
* synchronous HTML output.
|
|
317
|
+
*/
|
|
318
|
+
readonly streaming?: boolean;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The result of rendering a React Server Component via
|
|
322
|
+
* {@link RscTestRuntime.renderServerComponent}.
|
|
323
|
+
*/
|
|
324
|
+
interface RscRenderResult {
|
|
325
|
+
/** The complete HTML string produced by the render. */
|
|
326
|
+
readonly html: string;
|
|
327
|
+
/**
|
|
328
|
+
* A cloned `ReadableStream` of the raw bytes. Only present when the runtime
|
|
329
|
+
* was created with `streaming: true`; otherwise `null`.
|
|
330
|
+
*/
|
|
331
|
+
readonly stream: ReadableStream<Uint8Array> | null;
|
|
332
|
+
/**
|
|
333
|
+
* Decoded text chunks collected from the stream. Empty when `streaming` is
|
|
334
|
+
* `false`.
|
|
335
|
+
*/
|
|
336
|
+
readonly chunks: readonly string[];
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Extended test runtime that adds React Server Component rendering on top of
|
|
340
|
+
* the base {@link StartTestRuntime}.
|
|
341
|
+
*
|
|
342
|
+
* @remarks
|
|
343
|
+
* Created by {@link createRscTestRuntime}. Inherits all members from
|
|
344
|
+
* {@link StartTestRuntime} and adds {@link renderServerComponent}.
|
|
345
|
+
*/
|
|
346
|
+
interface RscTestRuntime extends StartTestRuntime {
|
|
347
|
+
/**
|
|
348
|
+
* Render a React Server Component to HTML.
|
|
349
|
+
*
|
|
350
|
+
* @typeParam TProps - The component's props type.
|
|
351
|
+
* @param component - The React component to render.
|
|
352
|
+
* @param props - Props forwarded to `createElement(component, props)`.
|
|
353
|
+
* @returns A `Promise` resolving to an {@link RscRenderResult} containing the
|
|
354
|
+
* rendered HTML and, when streaming is enabled, the raw stream and decoded
|
|
355
|
+
* chunks.
|
|
356
|
+
*/
|
|
357
|
+
readonly renderServerComponent: <TProps extends Record<string, unknown>>(component: ComponentType<TProps>, props: TProps) => Promise<RscRenderResult>;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Create an {@link RscTestRuntime} for rendering React Server Components in a
|
|
361
|
+
* simulated TanStack Start environment.
|
|
362
|
+
*
|
|
363
|
+
* @param options - Runtime configuration. See {@link RscTestRuntimeOptions}.
|
|
364
|
+
* @returns A `Promise` resolving to an {@link RscTestRuntime} instance.
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* import { createRscTestRuntime } from '@tanstack-router-testing/react-start-testing';
|
|
369
|
+
*
|
|
370
|
+
* const runtime = await createRscTestRuntime({ streaming: true });
|
|
371
|
+
*
|
|
372
|
+
* function Greeting({ name }: { name: string }) {
|
|
373
|
+
* return <h1>Hello, {name}!</h1>;
|
|
374
|
+
* }
|
|
375
|
+
*
|
|
376
|
+
* const { html, chunks } = await runtime.renderServerComponent(Greeting, {
|
|
377
|
+
* name: 'TanStack',
|
|
378
|
+
* });
|
|
379
|
+
* // html === '<h1>Hello, TanStack!</h1>'
|
|
380
|
+
*
|
|
381
|
+
* runtime.cleanup();
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* @remarks
|
|
385
|
+
* Delegates to {@link createStartTestRuntime} for the base runtime, then layers
|
|
386
|
+
* on `renderServerComponent` which uses `react-dom/server` under the hood. Set
|
|
387
|
+
* `streaming: true` in options to render via `renderToReadableStream` instead of
|
|
388
|
+
* the default `renderToString`.
|
|
389
|
+
*/
|
|
390
|
+
declare const createRscTestRuntime: (options?: RscTestRuntimeOptions) => Promise<RscTestRuntime>;
|
|
391
|
+
//#endregion
|
|
392
|
+
//#region src/index.d.ts
|
|
393
|
+
/**
|
|
394
|
+
* Current package version. Bumped at release time.
|
|
395
|
+
*/
|
|
396
|
+
declare const VERSION = "0.0.0";
|
|
397
|
+
//#endregion
|
|
398
|
+
export { type AnyServerFn, type CallMiddlewareOptions, type CallMiddlewareResult, type MockMiddlewareOptions, type RscRenderResult, type RscTestRuntime, type RscTestRuntimeOptions, type ServerFnMock, type StartTestRunOptions, type StartTestRuntime, type StartTestRuntimeOptions, VERSION, callMiddleware, clearStartMocks, createRscTestRuntime, createStartTestRuntime, mockMiddleware, mockServerFn, runInStartEnv };
|
|
399
|
+
//# sourceMappingURL=index.d.mts.map
|