@rsc-kit/mcp 0.41.0 → 0.42.0
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/dist/recipes.js +12 -2
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +206 -6
- package/guides/api-routes.md +159 -215
- package/guides/authorization.md +103 -114
- package/guides/backend-answered-pages.md +301 -252
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +300 -169
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +2 -2
- package/guides/commands.md +251 -200
- package/guides/connection.md +50 -54
- package/guides/content-security-policy.md +133 -84
- package/guides/domains.md +190 -149
- package/guides/edge-caching.md +242 -268
- package/guides/emails.md +96 -45
- package/guides/errors.md +116 -137
- package/guides/feature-flags.md +100 -30
- package/guides/file-uploads.md +101 -26
- package/guides/fonts.md +139 -110
- package/guides/forms.md +520 -542
- package/guides/images.md +87 -28
- package/guides/index.json +13 -33
- package/guides/instrumentation.md +134 -95
- package/guides/isr.md +3 -3
- package/guides/live-data-in-production.md +695 -0
- package/guides/live-data.md +170 -839
- package/guides/mcp.md +83 -59
- package/guides/metadata.md +124 -179
- package/guides/navigation.md +118 -147
- package/guides/no-javascript.md +49 -57
- package/guides/offline.md +118 -153
- package/guides/openapi.md +100 -35
- package/guides/pages.md +160 -50
- package/guides/ppr.md +178 -256
- package/guides/pwa.md +251 -218
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +269 -147
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -539
- package/guides/laravel.md +0 -787
- package/guides/your-own-backend.md +0 -602
package/guides/testing.md
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
# Testing
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Call actions and routes as functions, fetch the whole app without a server, and keep a browser for the round trip.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
runtime of ours. The examples use `bun:test`.
|
|
5
|
+
Most of an app is functions, and a function is tested by calling it. Any test
|
|
6
|
+
runner works: Bun's, Vitest or Jest. The examples use `bun:test`.
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
```ts title="tests/actions.test.ts"
|
|
9
|
+
import { placeOrder } from '../src/actions'
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
test('placeOrder accepts an item', async () => {
|
|
12
|
+
expect(await placeOrder('a rubber duck')).toEqual({ ok: true })
|
|
13
|
+
})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
For the app as a whole, `createTestApp()` answers a path the way production
|
|
17
|
+
does, with no port and no process:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const app = await createTestApp()
|
|
21
|
+
|
|
22
|
+
expect((await app.fetch('/orders')).status).toBe(200)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Testing a server action
|
|
26
|
+
|
|
27
|
+
`"use server"` is a directive for the bundler. In a test file it is just a
|
|
28
|
+
string, so the function it marks can be imported and called:
|
|
13
29
|
|
|
14
30
|
```ts title="tests/actions.test.ts"
|
|
15
31
|
import { placeOrder } from '../src/actions'
|
|
@@ -25,7 +41,7 @@ test('getListings answers with a list', async () => {
|
|
|
25
41
|
```
|
|
26
42
|
|
|
27
43
|
An action built on the [action client](/guides/authorization/) runs its whole
|
|
28
|
-
middleware chain when called, so a check that refuses a stranger is
|
|
44
|
+
middleware chain when called, so a check that refuses a stranger is tested by
|
|
29
45
|
calling it as one. It **returns** its failures, so assert on the result:
|
|
30
46
|
|
|
31
47
|
```ts
|
|
@@ -34,57 +50,54 @@ const result = await createPost({ title: '' })
|
|
|
34
50
|
expect(result.validationErrors).toEqual({ title: ['too short'] })
|
|
35
51
|
```
|
|
36
52
|
|
|
37
|
-
|
|
53
|
+
To call it as a signed-in visitor, open a request with their cookie
|
|
54
|
+
([below](#reading-cookies-and-headers-in-a-test)):
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
const result = await withRequest(
|
|
58
|
+
new Request('https://app.test/', { headers: { Cookie: 'session=abc' } }),
|
|
59
|
+
() => createPost({ title: 'Hello' }),
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Testing an API route
|
|
64
|
+
|
|
65
|
+
Call the handler with a `Request` and the context the engine would pass:
|
|
38
66
|
|
|
39
67
|
```ts title="tests/api.test.ts"
|
|
40
68
|
import { GET } from '../src/app/api/greet/[name]/route'
|
|
41
69
|
|
|
42
70
|
test('greets by name', async () => {
|
|
43
71
|
const res = await GET(new Request('https://app.test/api/greet/ada'), {
|
|
44
|
-
params: Promise.resolve({ name: 'ada' }),
|
|
72
|
+
params: Promise.resolve({ name: 'ada' }), // a promise, as the engine gives it - RouteContext types it so
|
|
45
73
|
})
|
|
46
74
|
|
|
47
75
|
expect(await res.json()).toEqual({ greeting: 'Hello, ada' })
|
|
48
76
|
})
|
|
49
77
|
```
|
|
50
78
|
|
|
51
|
-
|
|
52
|
-
Next's advice for server actions is end-to-end, because the interesting part
|
|
53
|
-
there is the round trip. Here the function *is* the interesting part — the
|
|
54
|
-
validation, the middleware, the authorisation — and it is a unit test.
|
|
55
|
-
:::
|
|
56
|
-
|
|
57
|
-
One line a port brings needs a stub: `import 'server-only'`. Keep it — the
|
|
58
|
-
build honours it, and a client file that imports the module fails to build
|
|
59
|
-
rather than shipping a secret. But under Vite the package resolves to an
|
|
60
|
-
empty module on the server, and under `bun test` it is the real package,
|
|
61
|
-
which throws on import; an action file that carries the line is not callable
|
|
62
|
-
in a unit test until a preload stubs it. The scaffold ships the two files:
|
|
63
|
-
|
|
64
|
-
```toml title="bunfig.toml"
|
|
65
|
-
[test]
|
|
66
|
-
preload = ["./tests/preload.ts"]
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
```ts title="tests/preload.ts"
|
|
70
|
-
import { mock } from 'bun:test'
|
|
79
|
+
A `POST` is the same, with a body:
|
|
71
80
|
|
|
72
|
-
|
|
73
|
-
|
|
81
|
+
```ts
|
|
82
|
+
import { POST } from '../src/app/api/orders/route'
|
|
83
|
+
|
|
84
|
+
const res = await POST(
|
|
85
|
+
new Request('https://app.test/api/orders', {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: { 'Content-Type': 'application/json' },
|
|
88
|
+
body: JSON.stringify({ item: 'a rubber duck' }),
|
|
89
|
+
}),
|
|
90
|
+
{ params: Promise.resolve({}) },
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
expect(res.status).toBe(201)
|
|
74
94
|
```
|
|
75
95
|
|
|
76
|
-
|
|
77
|
-
with this package's `headers()` and `cookies()`, which is how a library
|
|
78
|
-
written for Next — the [Flags SDK](/guides/feature-flags)'s `flags/next` —
|
|
79
|
-
runs here without Next. A test is not built, so the import finds Next's own
|
|
80
|
-
module, which throws outside a Next request, or finds nothing. The line
|
|
81
|
-
answers it the way the build does, and a flag read in an action or a page
|
|
82
|
-
evaluates under `withRequest()` as it does in the app.
|
|
83
|
-
|
|
84
|
-
### Reading the request
|
|
96
|
+
## Reading cookies and headers in a test
|
|
85
97
|
|
|
86
|
-
`cookies()`, `headers()` and the rest read from a scope the
|
|
87
|
-
request.
|
|
98
|
+
`cookies()`, `headers()` and the rest read from a scope the server opens for
|
|
99
|
+
each request. A test has no server, so open the scope yourself with
|
|
100
|
+
`withRequest`:
|
|
88
101
|
|
|
89
102
|
```ts title="tests/session.test.ts"
|
|
90
103
|
import { withRequest } from '@rsc-kit/core/request'
|
|
@@ -104,15 +117,41 @@ test('and no cookie is nobody', async () => {
|
|
|
104
117
|
})
|
|
105
118
|
```
|
|
106
119
|
|
|
107
|
-
The scope
|
|
108
|
-
each other's cookies.
|
|
120
|
+
The scope belongs to one call, so two requests in flight in the same test don't
|
|
121
|
+
see each other's cookies.
|
|
109
122
|
|
|
110
|
-
|
|
123
|
+
### `server-only` and `next/headers`
|
|
111
124
|
|
|
112
|
-
|
|
113
|
-
the
|
|
114
|
-
|
|
115
|
-
|
|
125
|
+
A file with `import 'server-only'` throws when `bun test` imports it, because
|
|
126
|
+
the test loads the real package. Keep the line: the build honours it, and a
|
|
127
|
+
client file that imports the module fails to build instead of shipping a
|
|
128
|
+
secret. Under Vite the package is an empty module on the server. In a test,
|
|
129
|
+
stub it in a preload. The scaffold ships these two files:
|
|
130
|
+
|
|
131
|
+
```toml title="bunfig.toml"
|
|
132
|
+
[test]
|
|
133
|
+
preload = ["./tests/preload.ts"]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```ts title="tests/preload.ts"
|
|
137
|
+
import { mock } from 'bun:test'
|
|
138
|
+
|
|
139
|
+
mock.module('server-only', () => ({}))
|
|
140
|
+
mock.module('next/headers', () => import('@rsc-kit/core/request'))
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The second line covers libraries written for Next, such as the
|
|
144
|
+
[Flags SDK](/guides/feature-flags)'s `flags/next`. The build answers
|
|
145
|
+
`next/headers` with this package's `headers()` and `cookies()`. A test isn't
|
|
146
|
+
built, so the import would find Next's own module, which throws outside a Next
|
|
147
|
+
request, or nothing at all. With the stub, a flag read in an action or a page
|
|
148
|
+
evaluates under `withRequest()` as it does in the app.
|
|
149
|
+
|
|
150
|
+
## Testing the whole app
|
|
151
|
+
|
|
152
|
+
`createTestApp()` loads the built app and answers `Request → Response` through
|
|
153
|
+
the real router, the real middleware, the real API routes and the pages the
|
|
154
|
+
build stored, with no port and no process:
|
|
116
155
|
|
|
117
156
|
```ts title="tests/app.test.ts"
|
|
118
157
|
import { createTestApp } from '@rsc-kit/core/testing'
|
|
@@ -132,43 +171,148 @@ test('a guarded page turns a stranger away', async () => {
|
|
|
132
171
|
expect(res.headers.get('Location')).toBe('/login')
|
|
133
172
|
})
|
|
134
173
|
|
|
174
|
+
test('a signed-in visitor gets in', async () => {
|
|
175
|
+
const res = await app.fetch('/admin', { headers: { Cookie: 'session=abc' } })
|
|
176
|
+
|
|
177
|
+
expect(res.status).toBe(200)
|
|
178
|
+
})
|
|
179
|
+
|
|
135
180
|
test('a method a route does not export is 405', async () => {
|
|
136
181
|
const res = await app.fetch('/api/health', { method: 'DELETE' })
|
|
137
182
|
|
|
138
183
|
expect(res.headers.get('Allow')).toContain('GET')
|
|
139
184
|
})
|
|
185
|
+
|
|
186
|
+
test('an unknown url is a 404', async () => {
|
|
187
|
+
expect((await app.fetch('/nope')).status).toBe(404)
|
|
188
|
+
})
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
This is where a route that renders fine but is served wrong shows up: a guard
|
|
192
|
+
that never ran, a page stored that shouldn't have been, or a `404` that came
|
|
193
|
+
back `200`.
|
|
194
|
+
|
|
195
|
+
Files the build wrote to `.output/public` are answered too, the way Nitro's
|
|
196
|
+
static layer answers them in production: the hashed assets, `sw.js`,
|
|
197
|
+
`manifest.webmanifest` and the icons. So a test can check that a page's links
|
|
198
|
+
resolve, not only that the page renders:
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
expect((await app.fetch('/sw.js')).status).toBe(200) // an app with a service worker
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### When it builds
|
|
205
|
+
|
|
206
|
+
`createTestApp()` builds only when something the build is made from is newer
|
|
207
|
+
than the last build: your source, your Vite config, `package.json`, the
|
|
208
|
+
lockfile or the installed `@rsc-kit/core`. The first run pays for the build,
|
|
209
|
+
later runs don't, and an edit or an upgrade is picked up.
|
|
210
|
+
|
|
211
|
+
- It runs your own `build` script, `bun run build` under Bun and `npm run build`
|
|
212
|
+
under Node, so what is tested is what ships, on the runtime it ships on.
|
|
213
|
+
- Pass `{ build: false }` in a CI step that already built. It then fails
|
|
214
|
+
loudly if there is no build to read.
|
|
215
|
+
- There is one build and one loaded module per test run, shared across files.
|
|
216
|
+
Two copies of the server bundle in one process would be two client-reference
|
|
217
|
+
registries.
|
|
218
|
+
|
|
219
|
+
## Standing in for a Go or Laravel backend
|
|
220
|
+
|
|
221
|
+
`host` answers `rpc()` calls and host guards in the test, by name. The app
|
|
222
|
+
reaches it through the same client it uses against a real backend, so nothing
|
|
223
|
+
listens and `RSC_BACKEND` isn't called:
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
import { createTestApp, hostReply, HOST_MIDDLEWARE } from '@rsc-kit/core/testing'
|
|
227
|
+
|
|
228
|
+
const app = await createTestApp({
|
|
229
|
+
host: {
|
|
230
|
+
'Apps.list': ({ headers }) =>
|
|
231
|
+
headers.get('cookie')?.includes('session=') ? [{ id: 1, name: 'Shop' }] : hostReply.unauthenticated(),
|
|
232
|
+
'Apps.create': () => hostReply.invalid({ name: ['Taken.'] }),
|
|
233
|
+
[HOST_MIDDLEWARE]: () => true, // the page's host guards let it render
|
|
234
|
+
},
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('a stranger gets a 401', async () => {
|
|
238
|
+
expect((await app.fetch('/apps')).status).toBe(401)
|
|
239
|
+
})
|
|
140
240
|
```
|
|
141
241
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
`
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
242
|
+
A function the test forgot fails the call rather than answering `null`, so a
|
|
243
|
+
test can't pass against nothing. `hostReply` has the protocol's other answers:
|
|
244
|
+
|
|
245
|
+
| Reply | What the app does |
|
|
246
|
+
| --- | --- |
|
|
247
|
+
| `hostReply.unauthenticated()` | The page answers 401, an action its auth error |
|
|
248
|
+
| `hostReply.unauthorized()` | 403 |
|
|
249
|
+
| `hostReply.redirect('/login')` | Sends the visitor there |
|
|
250
|
+
| `hostReply.refuse(404)` | Refuses with that status, with optional data for the page |
|
|
251
|
+
| `hostReply.invalid({ name: ['Taken.'] })` | An action returns these as `validationErrors` |
|
|
252
|
+
| `hostReply.fail()` | An unexpected failure, not a refusal |
|
|
253
|
+
| `hostReply.revalidating(result, 'apps')` | A result, and the regions it changed |
|
|
254
|
+
| `hostReply.settingCookies(result, 'session=abc; Path=/')` | A result, with cookies on the response |
|
|
255
|
+
|
|
256
|
+
A URL the app forwards to the backend, such as a Laravel page or `/login`, goes
|
|
257
|
+
to `backend`. Without one it is forwarded to `RSC_BACKEND`, and in a test
|
|
258
|
+
that's usually a 502:
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
const app = await createTestApp({
|
|
262
|
+
backend: () => new Response('the login page'),
|
|
263
|
+
})
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Checking what a page shows
|
|
267
|
+
|
|
268
|
+
`fetch()` returns the document as it is sent. That document carries the page's
|
|
269
|
+
payload for the browser to hydrate from, including everything the page was
|
|
270
|
+
given, such as a client component's props, whether or not it is on screen. To
|
|
271
|
+
assert that something is **not** shown, read `markup()`, the same document with
|
|
272
|
+
every `<script>` taken out:
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
expect(await app.markup('/teams/acme')).not.toContain('internal note')
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
A region that streamed in later is in the markup where it arrived. A
|
|
279
|
+
`<Suspense>` fallback it replaced is still there too, as the server sent it.
|
|
280
|
+
|
|
281
|
+
## What a page watches
|
|
282
|
+
|
|
283
|
+
`watched()` renders a page and says what each region refreshes on, without
|
|
284
|
+
reading the signed names out of the payload: `page` for the page's own
|
|
285
|
+
`refreshOn`, and a section's name for each section's.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
expect(await app.watched('/teams/acme')).toEqual({ page: ['team:acme'], apps: ['team:acme:apps'] })
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
A region whose `refreshOn` gave no names is listed with `[]`. A region with no
|
|
292
|
+
`refreshOn` isn't listed.
|
|
293
|
+
|
|
294
|
+
With a Go or Laravel backend stood in by `host`, `testChanges()` answers the
|
|
295
|
+
version questions the way the backend would. Its `changed` moves a name, as the
|
|
296
|
+
backend's webhook would:
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
import { createTestApp, testChanges } from '@rsc-kit/core/testing'
|
|
300
|
+
|
|
301
|
+
const names = testChanges()
|
|
302
|
+
const app = await createTestApp({ host: { ...names.host, 'Repos.list': () => repos } })
|
|
303
|
+
|
|
304
|
+
names.changed('team:1:repos')
|
|
305
|
+
```
|
|
160
306
|
|
|
161
307
|
## Components
|
|
162
308
|
|
|
163
309
|
`<Form>`, `useField`, `useOnline` and the rest are client components. Test them
|
|
164
|
-
with React's own tools and any DOM
|
|
165
|
-
with `react-dom/client` and `act`, and
|
|
166
|
-
same way.
|
|
310
|
+
with React's own tools and any DOM. The package's own suite uses `happy-dom`
|
|
311
|
+
with `react-dom/client` and `act`, and Testing Library works the same way.
|
|
167
312
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
and click inside `act`:
|
|
313
|
+
Test your own component by mounting it and clicking, not by calling React's
|
|
314
|
+
hooks yourself. Register a DOM in a module the test file imports **first**,
|
|
315
|
+
replace the module the action stubs come from, and click inside `act`:
|
|
172
316
|
|
|
173
317
|
```ts title="tests/dom.ts"
|
|
174
318
|
import { GlobalRegistrator } from '@happy-dom/global-registrator'
|
|
@@ -216,92 +360,70 @@ test('says nothing when the call redirected', async () => {
|
|
|
216
360
|
})
|
|
217
361
|
```
|
|
218
362
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
DOM
|
|
224
|
-
|
|
363
|
+
The second test matters for a backend action. Any call to one can answer
|
|
364
|
+
`{ redirected }`, and a handler that reports success without checking
|
|
365
|
+
[`isRedirected`](/guides/redirects) is a bug this test finds.
|
|
366
|
+
|
|
367
|
+
**The DOM has to exist before any component is imported.** That's why `./dom`
|
|
368
|
+
is the first line, not a `beforeAll`. Imports run in order, and a library that
|
|
369
|
+
looks for a DOM when it is imported keeps the answer: Base UI picks its layout
|
|
370
|
+
effect from `typeof document` once, so a component loaded before the DOM
|
|
371
|
+
existed never opens its dialogs. The component is imported with `await import()`
|
|
372
|
+
after `mock.module`, so the stubs are in place before it loads.
|
|
225
373
|
|
|
226
374
|
<Aside type="caution" title="Never register a DOM globally, or in a preload">
|
|
227
375
|
`GlobalRegistrator` replaces `Request`, `Response`, `Headers`, `fetch`, `Blob`,
|
|
228
|
-
`FormData`, `URL` and `AbortController` for every test in the process.
|
|
229
|
-
browser's `Request` will not carry a `Cookie` header a script set, so with a DOM
|
|
230
|
-
in a shared preload every signed-in request `createTestApp` makes arrives with
|
|
231
|
-
no session and bounces to sign-in. `Bun.write(path, new Blob([bytes]))` writes
|
|
232
|
-
the text `[object Blob]`. `createTestApp()` refuses to run while a DOM is
|
|
233
|
-
registered, and says so. Register in the component test file, as above, and
|
|
234
|
-
`unregister()` after it.
|
|
235
|
-
|
|
236
|
-
Run the suites isolated, `bun test --isolate`: `mock.module` replaces a module
|
|
237
|
-
for the whole process, so without it one file's stubs are the next file's.
|
|
238
|
-
</Aside>
|
|
376
|
+
`FormData`, `URL` and `AbortController` for every test in the process.
|
|
239
377
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
## What still needs a browser
|
|
378
|
+
- A browser's `Request` won't carry a `Cookie` header a script set, so every
|
|
379
|
+
signed-in request `createTestApp` makes arrives with no session and bounces
|
|
380
|
+
to sign-in.
|
|
381
|
+
- `Bun.write(path, new Blob([bytes]))` writes the text `[object Blob]`.
|
|
245
382
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
call carries an id that React keeps private, so a test holding the source
|
|
249
|
-
function cannot address the built one.
|
|
250
|
-
|
|
251
|
-
That is the same limit Next has, and it is narrower here: it is the encoding of
|
|
252
|
-
one round trip, not the action. Everything the action *does* is a unit test
|
|
253
|
-
above. For the round trip itself, and for anything that depends on hydration —
|
|
254
|
-
`useField` re-rendering, a navigation, an optimistic update reverting — use
|
|
255
|
-
Playwright against `vite preview`, which serves the real build.
|
|
256
|
-
|
|
257
|
-
## What a page shows
|
|
258
|
-
|
|
259
|
-
`fetch()` returns the document as it is sent, and the document carries the
|
|
260
|
-
page's payload for the browser to hydrate from — everything the page was
|
|
261
|
-
given, a client component's props included, whether or not it is on screen.
|
|
262
|
-
To assert that something is **not** shown, read `markup()`: the same document
|
|
263
|
-
with every `<script>` taken out.
|
|
264
|
-
|
|
265
|
-
```ts
|
|
266
|
-
expect(await app.markup('/teams/acme')).not.toContain('internal note')
|
|
267
|
-
```
|
|
383
|
+
`createTestApp()` refuses to run while a DOM is registered, and says so.
|
|
384
|
+
Register in the component test file, as above, and `unregister()` after it.
|
|
268
385
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
386
|
+
Run the suites isolated with `bun test --isolate`. `mock.module` replaces a
|
|
387
|
+
module for the whole process, so without it one file's stubs are the next
|
|
388
|
+
file's.
|
|
389
|
+
</Aside>
|
|
273
390
|
|
|
274
|
-
|
|
275
|
-
for the page's own `refreshOn`, a section's name for each section's - without
|
|
276
|
-
reading the signed names out of the payload:
|
|
391
|
+
## What still needs a browser
|
|
277
392
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
393
|
+
One thing: **a server action called over the wire.** The function is testable
|
|
394
|
+
directly, and every URL is testable through `createTestApp()`. But the wire
|
|
395
|
+
call carries an ID that React keeps private, so a test holding the source
|
|
396
|
+
function can't address the built one.
|
|
281
397
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
398
|
+
That's the encoding of one round trip, not the action. Everything the action
|
|
399
|
+
*does* is tested by calling it. For the round trip, and for anything that
|
|
400
|
+
depends on hydration (`useField` re-rendering, a navigation, an optimistic
|
|
401
|
+
update reverting), use Playwright against `vite preview`, which serves the real
|
|
402
|
+
build:
|
|
285
403
|
|
|
286
|
-
```ts
|
|
287
|
-
import {
|
|
404
|
+
```ts title="e2e/order.spec.ts"
|
|
405
|
+
import { expect, test } from '@playwright/test'
|
|
288
406
|
|
|
289
|
-
|
|
290
|
-
|
|
407
|
+
test('placing an order shows it in the list', async ({ page }) => {
|
|
408
|
+
await page.goto('/orders')
|
|
409
|
+
await page.getByLabel('Item').fill('a rubber duck')
|
|
410
|
+
await page.getByRole('button', { name: 'Place order' }).click()
|
|
291
411
|
|
|
292
|
-
|
|
412
|
+
await expect(page.getByText('a rubber duck')).toBeVisible()
|
|
413
|
+
})
|
|
293
414
|
```
|
|
294
415
|
|
|
295
416
|
## What to test where
|
|
296
417
|
|
|
297
|
-
|
|
|
418
|
+
| What | How |
|
|
298
419
|
| --- | --- |
|
|
299
|
-
|
|
|
300
|
-
|
|
|
301
|
-
|
|
|
302
|
-
|
|
|
303
|
-
|
|
|
304
|
-
|
|
|
305
|
-
|
|
|
306
|
-
| a
|
|
307
|
-
|
|
|
420
|
+
| An action's logic, validation, middleware | Call it |
|
|
421
|
+
| A query | Call it |
|
|
422
|
+
| An API route | Call `GET` or `POST` with a `Request` |
|
|
423
|
+
| Anything that reads cookies or headers | `withRequest()` |
|
|
424
|
+
| Routing, guards, stored pages, status codes | `createTestApp().fetch()` |
|
|
425
|
+
| A Go or Laravel backend's answers | `createTestApp({ host })` |
|
|
426
|
+
| What a page shows, or doesn't | `createTestApp().markup()` |
|
|
427
|
+
| What a page refreshes on | `createTestApp().watched()` |
|
|
428
|
+
| A client component | React and a DOM |
|
|
429
|
+
| An action over the wire, hydration, navigation | Playwright |
|