remix 3.0.0-beta.3 → 3.0.0-beta.4
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/package.json +31 -30
- package/src/assert/README.md +11 -4
- package/src/fetch-router/README.md +115 -35
- package/src/test/README.md +27 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "remix",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.4",
|
|
4
4
|
"description": "The Remix web framework",
|
|
5
5
|
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -529,50 +529,50 @@
|
|
|
529
529
|
"typescript": "^5.9.3"
|
|
530
530
|
},
|
|
531
531
|
"dependencies": {
|
|
532
|
-
"@remix-run/assets": "^0.4.
|
|
533
|
-
"@remix-run/auth": "^0.2.
|
|
534
|
-
"@remix-run/
|
|
535
|
-
"@remix-run/cop-middleware": "^0.1.5",
|
|
536
|
-
"@remix-run/csrf-middleware": "^0.1.5",
|
|
537
|
-
"@remix-run/cors-middleware": "^0.1.5",
|
|
532
|
+
"@remix-run/assets": "^0.4.3",
|
|
533
|
+
"@remix-run/auth": "^0.2.5",
|
|
534
|
+
"@remix-run/async-context-middleware": "^0.3.3",
|
|
538
535
|
"@remix-run/ui": "^0.3.0",
|
|
539
|
-
"@remix-run/
|
|
540
|
-
"@remix-run/auth-middleware": "^0.2.
|
|
541
|
-
"@remix-run/
|
|
536
|
+
"@remix-run/compression-middleware": "^0.1.11",
|
|
537
|
+
"@remix-run/auth-middleware": "^0.2.3",
|
|
538
|
+
"@remix-run/cop-middleware": "^0.1.6",
|
|
539
|
+
"@remix-run/csrf-middleware": "^0.1.6",
|
|
540
|
+
"@remix-run/cors-middleware": "^0.1.6",
|
|
542
541
|
"@remix-run/data-schema": "^0.3.0",
|
|
542
|
+
"@remix-run/data-table": "^0.3.0",
|
|
543
|
+
"@remix-run/cookie": "^0.5.4",
|
|
543
544
|
"@remix-run/data-table-postgres": "^0.4.0",
|
|
544
|
-
"@remix-run/data-table-sqlite": "^0.5.1",
|
|
545
545
|
"@remix-run/data-table-mysql": "^0.4.0",
|
|
546
|
-
"@remix-run/data-table": "^0.
|
|
547
|
-
"@remix-run/fetch-router": "^0.19.2",
|
|
548
|
-
"@remix-run/file-storage": "^0.13.6",
|
|
546
|
+
"@remix-run/data-table-sqlite": "^0.5.1",
|
|
549
547
|
"@remix-run/fetch-proxy": "^0.8.3",
|
|
548
|
+
"@remix-run/fetch-router": "^0.20.0",
|
|
549
|
+
"@remix-run/file-storage": "^0.13.6",
|
|
550
|
+
"@remix-run/form-data-middleware": "^0.3.3",
|
|
550
551
|
"@remix-run/file-storage-s3": "^0.1.3",
|
|
551
|
-
"@remix-run/form-data-middleware": "^0.3.2",
|
|
552
|
-
"@remix-run/form-data-parser": "^0.17.3",
|
|
553
|
-
"@remix-run/fs": "^0.4.5",
|
|
554
552
|
"@remix-run/headers": "^0.21.1",
|
|
555
|
-
"@remix-run/
|
|
553
|
+
"@remix-run/fs": "^0.4.5",
|
|
554
|
+
"@remix-run/form-data-parser": "^0.17.3",
|
|
556
555
|
"@remix-run/html-template": "^0.3.1",
|
|
557
|
-
"@remix-run/
|
|
558
|
-
"@remix-run/
|
|
559
|
-
"@remix-run/multipart-parser": "^0.16.3",
|
|
556
|
+
"@remix-run/method-override-middleware": "^0.1.11",
|
|
557
|
+
"@remix-run/logger-middleware": "^0.3.3",
|
|
560
558
|
"@remix-run/mime": "^0.4.1",
|
|
559
|
+
"@remix-run/multipart-parser": "^0.16.3",
|
|
560
|
+
"@remix-run/lazy-file": "^5.0.5",
|
|
561
561
|
"@remix-run/node-fetch-server": "^0.13.3",
|
|
562
|
-
"@remix-run/response": "^0.3.6",
|
|
563
562
|
"@remix-run/node-tsx": "^0.1.1",
|
|
564
|
-
"@remix-run/route-pattern": "^0.22.
|
|
563
|
+
"@remix-run/route-pattern": "^0.22.1",
|
|
564
|
+
"@remix-run/response": "^0.3.6",
|
|
565
565
|
"@remix-run/session": "^0.4.2",
|
|
566
|
-
"@remix-run/session-middleware": "^0.3.2",
|
|
567
|
-
"@remix-run/session-storage-memcache": "^0.1.2",
|
|
568
|
-
"@remix-run/static-middleware": "^0.4.11",
|
|
569
566
|
"@remix-run/session-storage-redis": "^0.1.1",
|
|
567
|
+
"@remix-run/session-middleware": "^0.3.3",
|
|
568
|
+
"@remix-run/session-storage-memcache": "^0.1.2",
|
|
569
|
+
"@remix-run/static-middleware": "^0.4.12",
|
|
570
|
+
"@remix-run/assert": "^0.3.0",
|
|
570
571
|
"@remix-run/tar-parser": "^0.7.1",
|
|
571
|
-
"@remix-run/cli": "^0.3.
|
|
572
|
-
"@remix-run/
|
|
572
|
+
"@remix-run/cli": "^0.3.3",
|
|
573
|
+
"@remix-run/test": "^0.5.0",
|
|
573
574
|
"@remix-run/terminal": "^0.1.1",
|
|
574
|
-
"@remix-run/
|
|
575
|
-
"@remix-run/render-middleware": "^0.1.2"
|
|
575
|
+
"@remix-run/render-middleware": "^0.1.3"
|
|
576
576
|
},
|
|
577
577
|
"bin": {
|
|
578
578
|
"remix": "./dist/cli-entry.js"
|
|
@@ -600,6 +600,7 @@
|
|
|
600
600
|
"scripts": {
|
|
601
601
|
"build": "tsc -p tsconfig.build.json",
|
|
602
602
|
"clean": "git clean -fdX",
|
|
603
|
+
"sync-readmes": "node ../../scripts/sync-remix-readmes.ts",
|
|
603
604
|
"test": "remix-test",
|
|
604
605
|
"typecheck": "tsc --noEmit"
|
|
605
606
|
}
|
package/src/assert/README.md
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
A compatible subset of `node:assert/strict` that works in any JavaScript environment, including browsers — plus a vitest-/jest-style `expect` API for tests that prefer chainable matchers.
|
|
4
4
|
|
|
5
|
-
Uses strict equality (
|
|
5
|
+
Uses strict equality (`Object.is`) for all comparisons — no type coercion.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
9
|
- `AssertionError` — compatible with `node:assert.AssertionError` (`actual`, `expected`, `operator`, `name`)
|
|
10
10
|
- `assert.ok` — truthy check
|
|
11
|
-
- `assert.equal` / `assert.notEqual` — strict equality (
|
|
12
|
-
- `assert.deepEqual` / `assert.notDeepEqual` — recursive strict deep equality
|
|
11
|
+
- `assert.equal` / `assert.notEqual` — strict equality (`Object.is` / `!Object.is`)
|
|
12
|
+
- `assert.deepEqual` / `assert.partialDeepEqual` / `assert.notDeepEqual` — recursive strict deep equality
|
|
13
13
|
- `assert.match` — string matches a regexp
|
|
14
14
|
- `assert.fail` — unconditional failure
|
|
15
15
|
- `assert.throws` — synchronous throw assertion with optional error validation
|
|
@@ -24,17 +24,20 @@ npm i remix
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
-
Mirrors `node:assert/strict` — uses strict equality (
|
|
27
|
+
Mirrors `node:assert/strict` — uses strict equality (`Object.is`), so `1 !== '1'`, `null !== undefined`, `NaN` equals `NaN`, and `0` does not equal `-0`.
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
30
|
import assert from 'remix/assert'
|
|
31
31
|
|
|
32
32
|
assert.ok(true)
|
|
33
|
+
assert(true)
|
|
33
34
|
assert.equal(1, 1)
|
|
34
35
|
assert.equal(1, '1') // throws — different types
|
|
36
|
+
assert.equal(NaN, NaN)
|
|
35
37
|
assert.notEqual('a', 'b')
|
|
36
38
|
assert.deepEqual({ a: 1 }, { a: 1 })
|
|
37
39
|
assert.deepEqual({ a: 1 }, { a: '1' }) // throws — different types
|
|
40
|
+
assert.partialDeepEqual({ a: 1, b: 2 }, { a: 1 })
|
|
38
41
|
assert.match('hello world', /world/)
|
|
39
42
|
assert.fail('should not reach here')
|
|
40
43
|
|
|
@@ -64,11 +67,15 @@ import {
|
|
|
64
67
|
equal,
|
|
65
68
|
notEqual,
|
|
66
69
|
deepEqual,
|
|
70
|
+
partialDeepEqual,
|
|
67
71
|
notDeepEqual,
|
|
68
72
|
match,
|
|
73
|
+
doesNotMatch,
|
|
69
74
|
fail,
|
|
70
75
|
throws,
|
|
76
|
+
doesNotThrow,
|
|
71
77
|
rejects,
|
|
78
|
+
doesNotReject,
|
|
72
79
|
} from 'remix/assert'
|
|
73
80
|
```
|
|
74
81
|
|
|
@@ -8,7 +8,7 @@ A minimal, composable router built on the [web Fetch API](https://developer.mozi
|
|
|
8
8
|
- **Type-Safe Routing**: Leverage TypeScript for compile-time route validation and parameter inference
|
|
9
9
|
- **Typed Request Context**: Carry request-scoped context through routers, controllers, and actions
|
|
10
10
|
- **Declarative Route Maps**: Define your route structure upfront with type-safe route names and request methods
|
|
11
|
-
- **Flexible Middleware**:
|
|
11
|
+
- **Flexible Middleware**: Use router, controller, and action middleware for each request boundary
|
|
12
12
|
- **Easy Testing**: Use standard `fetch()` to test your routes - no special test harness required
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
@@ -237,6 +237,85 @@ router.map(routes.contact, {
|
|
|
237
237
|
})
|
|
238
238
|
```
|
|
239
239
|
|
|
240
|
+
### Composing Route Groups
|
|
241
|
+
|
|
242
|
+
As applications grow, it is useful to let one file own the routes for a specific area of the app while the top-level router decides where that area is mounted. Use `router.mount()` with a route installer to register a group of routes under a route pattern prefix.
|
|
243
|
+
|
|
244
|
+
A route installer receives a `RouteBuilder`, not a full `Router`, so it can register routes but cannot dispatch requests. The parent router remains the only router that owns request dispatch, the matcher, and the default handler.
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
import { createRouter, type RouteBuilder } from 'remix/router'
|
|
248
|
+
import { get, route } from 'remix/routes'
|
|
249
|
+
|
|
250
|
+
const adminRouteDefs = {
|
|
251
|
+
index: get('/'),
|
|
252
|
+
users: {
|
|
253
|
+
show: get('/users/:id'),
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Use relative route groups inside installers. These routes are registered below
|
|
258
|
+
// the mount prefix when `installAdminRoutes()` runs.
|
|
259
|
+
const adminRouteGroup = route(adminRouteDefs)
|
|
260
|
+
|
|
261
|
+
// Use full app routes for links and redirects.
|
|
262
|
+
const routes = {
|
|
263
|
+
admin: route('/admin', adminRouteDefs),
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function installAdminRoutes<context extends AppContext>(router: RouteBuilder<context>) {
|
|
267
|
+
router.map(adminRouteGroup, {
|
|
268
|
+
actions: {
|
|
269
|
+
index() {
|
|
270
|
+
return new Response('Admin')
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
router.map(adminRouteGroup.users, {
|
|
276
|
+
actions: {
|
|
277
|
+
show({ params, currentUser }) {
|
|
278
|
+
return new Response(`User ${params.id} for ${currentUser.id}`)
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let router = createRouter<AppContext>({ middleware })
|
|
285
|
+
|
|
286
|
+
router.mount('/admin', installAdminRoutes)
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Mount prefixes are route patterns. They compose with routes registered inside the installer using `joinPatterns()`, so params from the mount prefix are available to mounted handlers:
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
router.mount('/orgs/:orgId', (org) => {
|
|
293
|
+
org.get('/users/:userId', ({ params }) => {
|
|
294
|
+
// params is { orgId: string, userId: string }
|
|
295
|
+
return new Response(`${params.orgId}:${params.userId}`)
|
|
296
|
+
})
|
|
297
|
+
})
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
When a mount prefix and child route use the same param name, the right-most route param wins, matching `route-pattern` behavior.
|
|
301
|
+
|
|
302
|
+
Middleware stays on routers, controllers, and actions. If an entire route group needs auth or another boundary, put that middleware on the controllers or actions owned by the installer:
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
function installAdminRoutes<context extends AppContext>(router: RouteBuilder<context>) {
|
|
306
|
+
router.map(adminRouteGroup, {
|
|
307
|
+
middleware: [requireAdmin()],
|
|
308
|
+
actions: {
|
|
309
|
+
index({ admin }) {
|
|
310
|
+
return new Response(admin.id)
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
})
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Unknown paths below a mounted prefix fall through to the parent router's default handler. If a route group needs its own catch-all response, register one explicitly inside the installer.
|
|
318
|
+
|
|
240
319
|
### Declaring Routes
|
|
241
320
|
|
|
242
321
|
In addition to the `{ method, pattern }` syntax shown above, the router provides a few shorthand methods that help eliminate some of the boilerplate when building complex route maps:
|
|
@@ -504,10 +583,12 @@ type Routes = typeof routes
|
|
|
504
583
|
|
|
505
584
|
Middleware functions run code before and/or after actions. They are a powerful way to add functionality to your app.
|
|
506
585
|
|
|
586
|
+
Every middleware must either return a `Response`, return the response from `next()`, or call `await next()` before it returns. Middleware that returns without calling `next()` throws at runtime.
|
|
587
|
+
|
|
507
588
|
A basic logging middleware might look like this:
|
|
508
589
|
|
|
509
590
|
```ts
|
|
510
|
-
import type
|
|
591
|
+
import { type Middleware } from 'remix/router'
|
|
511
592
|
|
|
512
593
|
// You can use the `Middleware` type to type middleware functions.
|
|
513
594
|
function logger(): Middleware {
|
|
@@ -567,8 +648,9 @@ function loadDatabase(): Middleware<{
|
|
|
567
648
|
value: Database
|
|
568
649
|
property: 'db'
|
|
569
650
|
}> {
|
|
570
|
-
return async (context) => {
|
|
651
|
+
return async (context, next) => {
|
|
571
652
|
context.set(Database, await connectDatabase(), { property: 'db' })
|
|
653
|
+
return next()
|
|
572
654
|
}
|
|
573
655
|
}
|
|
574
656
|
|
|
@@ -580,13 +662,13 @@ router.get('/books', async (context) => {
|
|
|
580
662
|
|
|
581
663
|
Use `context.db` (or `context.get(Database)`). If two values use the same property name, the router throws.
|
|
582
664
|
|
|
583
|
-
Middleware
|
|
665
|
+
Middleware has three API-owned forms: router middleware, controller middleware, and action middleware.
|
|
584
666
|
|
|
585
|
-
|
|
667
|
+
Router middleware is added to the router when it is created using the `createRouter({ middleware })` option. This middleware runs before any routes are matched and is useful for doing things like logging, serving static files, profiling, and a variety of other things. Router middleware runs on every request, so it's important to keep it lightweight and fast.
|
|
586
668
|
|
|
587
|
-
Controller middleware runs for every direct action in a controller. Action middleware runs only for one action, whether that action is registered in a controller or directly with `router.map()` or one of the method-specific helpers like `router.get()`, `router.post()`, `router.put()`, `router.delete()`, etc. The object form for actions is `{ handler, middleware? }`, so you can omit `middleware` entirely when you do not need it.
|
|
669
|
+
Controller middleware runs for every direct action in a controller. Action middleware runs only for one action, whether that action is created with `createAction()`, registered in a controller, or registered directly with `router.map()` or one of the method-specific helpers like `router.get()`, `router.post()`, `router.put()`, `router.delete()`, etc. The object form for actions is `{ handler, middleware? }`, so you can omit `middleware` entirely when you do not need it.
|
|
588
670
|
|
|
589
|
-
A controller's `middleware` applies only to the direct route actions in that controller, and its `actions` object may not include nested route-map keys.
|
|
671
|
+
A controller's `middleware` applies only to the direct route actions in that controller, and its `actions` object may not include nested route-map keys. This is the router's scoped middleware model: map nested route maps explicitly so each controller owns the direct route actions for one route map, and share middleware arrays between controllers that need the same boundary.
|
|
590
672
|
|
|
591
673
|
```tsx
|
|
592
674
|
let routes = route({
|
|
@@ -598,21 +680,21 @@ let routes = route({
|
|
|
598
680
|
})
|
|
599
681
|
|
|
600
682
|
let router = createRouter({
|
|
601
|
-
//
|
|
683
|
+
// Router middleware runs on all requests.
|
|
602
684
|
middleware: [staticFiles('./public')],
|
|
603
685
|
})
|
|
604
686
|
|
|
605
687
|
router.map(routes.home, () => new Response('Home'))
|
|
606
688
|
|
|
607
689
|
router.map(routes.admin, {
|
|
608
|
-
//
|
|
690
|
+
// Controller middleware applies to all direct actions in this controller.
|
|
609
691
|
middleware: [auth({ token: 'secret' })],
|
|
610
692
|
actions: {
|
|
611
693
|
dashboard() {
|
|
612
694
|
return new Response('Dashboard')
|
|
613
695
|
},
|
|
614
696
|
settings: {
|
|
615
|
-
//
|
|
697
|
+
// Action middleware applies only to this action.
|
|
616
698
|
middleware: [requireAdmin()],
|
|
617
699
|
handler() {
|
|
618
700
|
return new Response('Settings')
|
|
@@ -655,17 +737,11 @@ router.get('/posts/:id', (context) => {
|
|
|
655
737
|
|
|
656
738
|
Route params are only half of a handler's type contract. In many apps, handlers also depend on values that middleware loads into request context, like sessions, database connections, or authenticated users.
|
|
657
739
|
|
|
658
|
-
`fetch-router` lets you carry that context contract through the router and into stored controllers and actions. A common pattern is to derive one
|
|
740
|
+
`fetch-router` lets you carry that context contract through the router and into direct route registration, stored controllers, and stored actions. A common pattern is to derive one application context type from your middleware, augment `RouterTypes.context` with it, then use `createAction()` and `createController()` to type stored handlers.
|
|
659
741
|
|
|
660
742
|
```ts
|
|
661
743
|
import { Auth, requireAuth } from 'remix/middleware/auth'
|
|
662
|
-
import {
|
|
663
|
-
createAction,
|
|
664
|
-
createController,
|
|
665
|
-
type AnyParams,
|
|
666
|
-
type ContextWithParams,
|
|
667
|
-
type MiddlewareContext,
|
|
668
|
-
} from 'remix/router'
|
|
744
|
+
import { createAction, createController, createRouter, type RouterContext } from 'remix/router'
|
|
669
745
|
import { route } from 'remix/routes'
|
|
670
746
|
import { loadDatabase } from './middleware/database.ts'
|
|
671
747
|
import { loadSession } from './middleware/session.ts'
|
|
@@ -675,12 +751,12 @@ let routes = route({
|
|
|
675
751
|
})
|
|
676
752
|
|
|
677
753
|
type AuthIdentity = { id: string }
|
|
678
|
-
type RootMiddleware = [ReturnType<typeof loadSession>, ReturnType<typeof loadDatabase>]
|
|
679
754
|
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
755
|
+
export const router = createRouter({
|
|
756
|
+
middleware: [loadSession(), loadDatabase()],
|
|
757
|
+
})
|
|
758
|
+
|
|
759
|
+
type AppContext = RouterContext<typeof router>
|
|
684
760
|
|
|
685
761
|
declare module 'remix/router' {
|
|
686
762
|
interface RouterTypes {
|
|
@@ -688,19 +764,16 @@ declare module 'remix/router' {
|
|
|
688
764
|
}
|
|
689
765
|
}
|
|
690
766
|
|
|
691
|
-
let
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
let accountAction = createAction<typeof routes.account, AccountContext>(routes.account, {
|
|
695
|
-
middleware: accountMiddleware,
|
|
767
|
+
let accountAction = createAction(routes.account, {
|
|
768
|
+
middleware: [requireAuth<AuthIdentity>()],
|
|
696
769
|
handler(context) {
|
|
697
770
|
let auth = context.get(Auth)
|
|
698
771
|
return Response.json({ id: auth.identity.id })
|
|
699
772
|
},
|
|
700
773
|
})
|
|
701
774
|
|
|
702
|
-
let accountController = createController
|
|
703
|
-
middleware:
|
|
775
|
+
let accountController = createController(routes, {
|
|
776
|
+
middleware: [requireAuth<AuthIdentity>()],
|
|
704
777
|
actions: {
|
|
705
778
|
account(context) {
|
|
706
779
|
let auth = context.get(Auth)
|
|
@@ -710,11 +783,13 @@ let accountController = createController<typeof routes, AccountContext>(routes,
|
|
|
710
783
|
})
|
|
711
784
|
```
|
|
712
785
|
|
|
713
|
-
In this example,
|
|
786
|
+
In this example, the router's inline middleware array defines the app context contract. `RouterContext<typeof router>` extracts the request context that the router provides, so `RouterTypes.context` can use that context without storing the middleware chain separately.
|
|
787
|
+
|
|
788
|
+
Prefer plain inline arrays for `middleware` options on routers, controllers, actions, and route helpers. Inline arrays already give TypeScript enough information to infer middleware-provided context for downstream handlers, so `createAction()` and direct action objects see action middleware context, and `createController()` sees controller middleware context without `createMiddleware()`.
|
|
714
789
|
|
|
715
|
-
|
|
790
|
+
Use `createMiddleware()` only when a middleware chain is stored somewhere and its exact tuple type needs to survive that boundary. The common cases are deriving `MiddlewareContext<typeof rootMiddleware>` without a router value, exporting a reusable chain, or returning a chain from a factory. A standalone array like `let middleware = [loadSession(), loadDatabase()]` widens to a normal array, so `MiddlewareContext<typeof middleware>` cannot derive the ordered middleware context.
|
|
716
791
|
|
|
717
|
-
When manually annotating stored handlers
|
|
792
|
+
When manually annotating stored handlers with `Action<typeof route, Context>` or `Controller<typeof routes, Context>`, compose any action or controller middleware chain into `Context` with `MiddlewareContext<typeof actionOrControllerMiddleware, AppContext>`.
|
|
718
793
|
|
|
719
794
|
#### Middleware Provider Guidance
|
|
720
795
|
|
|
@@ -729,7 +804,12 @@ If you're authoring a middleware package that stores values in request context,
|
|
|
729
804
|
Apps can derive request context from the middleware tuple with `MiddlewareContext`. If they need to describe a context shape without a middleware tuple, they can use the core `ContextWithEntry` and `ContextWithEntries` helpers directly.
|
|
730
805
|
|
|
731
806
|
```ts
|
|
732
|
-
import {
|
|
807
|
+
import {
|
|
808
|
+
createContextKey,
|
|
809
|
+
createMiddleware,
|
|
810
|
+
type Middleware,
|
|
811
|
+
type MiddlewareContext,
|
|
812
|
+
} from 'remix/router'
|
|
733
813
|
|
|
734
814
|
// The context key that consumers will need to read from `context.get(...)`
|
|
735
815
|
export const CurrentUser = createContextKey<User | null>()
|
|
@@ -747,7 +827,7 @@ export function loadCurrentUser(): Middleware<{
|
|
|
747
827
|
}
|
|
748
828
|
}
|
|
749
829
|
|
|
750
|
-
let middleware =
|
|
830
|
+
let middleware = createMiddleware(loadCurrentUser())
|
|
751
831
|
type AppContext = MiddlewareContext<typeof middleware>
|
|
752
832
|
|
|
753
833
|
// Use context.currentUser (or context.get(CurrentUser)).
|
package/src/test/README.md
CHANGED
|
@@ -9,6 +9,7 @@ A test framework for JavaScript and TypeScript projects.
|
|
|
9
9
|
- Playwright E2E testing via `t.serve`
|
|
10
10
|
- In-browser component testing (pair with `render` from `remix/ui/test`)
|
|
11
11
|
- Mock functions and method spies via `t.mock.fn` / `t.mock.method`
|
|
12
|
+
- Per-test and hook timeouts with `t.signal` abort support
|
|
12
13
|
- Unified code coverage reporting across unit and E2E tests
|
|
13
14
|
- Watch mode
|
|
14
15
|
- Config file support (`remix-test.config.ts`)
|
|
@@ -193,6 +194,11 @@ describe('My Test Suite', () => {
|
|
|
193
194
|
afterEach(() => {})
|
|
194
195
|
|
|
195
196
|
it('tests something', () => {})
|
|
197
|
+
it('skips with a reason', { skip: 'requires API credentials' }, () => {})
|
|
198
|
+
it('tracks planned work', { todo: 'add retry coverage' }, () => {})
|
|
199
|
+
it('fails if it takes too long', { timeout: 5_000 }, async (t) => {
|
|
200
|
+
await fetchSomething({ signal: t.signal })
|
|
201
|
+
})
|
|
196
202
|
it('tests something else', () => {})
|
|
197
203
|
})
|
|
198
204
|
```
|
|
@@ -229,6 +235,9 @@ Each test callback receives a `TestContext` (`t`) as its first argument with hel
|
|
|
229
235
|
```ts
|
|
230
236
|
// from 'remix/test'
|
|
231
237
|
interface TestContext {
|
|
238
|
+
// Aborts when the test times out or when the user-provided test signal aborts
|
|
239
|
+
signal: AbortSignal
|
|
240
|
+
|
|
232
241
|
// Register a cleanup function to run after the test completes
|
|
233
242
|
after(fn: () => void): void
|
|
234
243
|
|
|
@@ -284,6 +293,24 @@ it('cleanup', (t) => {
|
|
|
284
293
|
})
|
|
285
294
|
```
|
|
286
295
|
|
|
296
|
+
#### Timeouts and Signals
|
|
297
|
+
|
|
298
|
+
Pass `{ timeout: ms }` to `it()` or after any lifecycle hook callback to fail that work if it takes too long. Timed-out tests abort `t.signal`, so async code that accepts an `AbortSignal` can cancel promptly.
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
it('loads data', { timeout: 5_000 }, async (t) => {
|
|
302
|
+
let response = await fetch('/api/data', { signal: t.signal })
|
|
303
|
+
assert.equal(response.status, 200)
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
beforeEach(
|
|
307
|
+
async () => {
|
|
308
|
+
await resetDatabase()
|
|
309
|
+
},
|
|
310
|
+
{ timeout: 1_000 },
|
|
311
|
+
)
|
|
312
|
+
```
|
|
313
|
+
|
|
287
314
|
#### Fake Timers
|
|
288
315
|
|
|
289
316
|
`t.useFakeTimers()` replaces the global timer functions (`setTimeout`, `setInterval`, etc.) with controllable fakes that are automatically restored after the test. It works in any test environment — server unit tests, browser tests, or E2E setup code.
|