@znight/testing 0.6.0 → 0.6.2
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/README.md +32 -31
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -185,16 +185,16 @@ await using app = await TestApp.create({
|
|
|
185
185
|
when the start fails because a port is taken (`EADDRINUSE` anywhere in the `cause` chain), the error
|
|
186
186
|
carries a hint naming these options.
|
|
187
187
|
|
|
188
|
-
| Option | Type | Description
|
|
189
|
-
| ----------- | --------------------------------- |
|
|
190
|
-
| `metadata` | `ZnightMetadata` | **Required.** The generated metadata.
|
|
191
|
-
| `plugins` | `Plugin[]` | Plugins, as in `Application.create`.
|
|
192
|
-
| `providers` | `Map` / `[key, value][]` / record | Values for externals (`Logger`, `ConfigSource`...), as in `Application.create`.
|
|
193
|
-
| `overrides` | `Map` / `[key, value][]` / record | Values or provider definitions replacing providers, applied last. Overridden providers are instantiated at boot.
|
|
194
|
-
| `only` | `'all'` \| `(Class \| Token)[]` | Providers instantiated at boot (with their dependencies), plus the overrides and the plugin roots (below). Default: the entry points of the metadata (HTTP controllers with their guards and interceptors, message consumers, hosts of scheduled jobs, GraphQL resolvers with their guards and loaders), or every provider when there is none. |
|
|
195
|
-
| `start` | `boolean` | Run `app.start()` (plugins listen/subscribe). Default `true`; `false` stops after `onBootstrap`.
|
|
196
|
-
| `logger` | `CoreLogger` | Default: a console logger writing warnings and errors only.
|
|
197
|
-
| `shutdown` | `{ timeoutMs?, hookTimeoutMs? }` | Shutdown timeouts.
|
|
188
|
+
| Option | Type | Description |
|
|
189
|
+
| ----------- | --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
190
|
+
| `metadata` | `ZnightMetadata` | **Required.** The generated metadata. |
|
|
191
|
+
| `plugins` | `Plugin[]` | Plugins, as in `Application.create`. |
|
|
192
|
+
| `providers` | `Map` / `[key, value][]` / record | Values for externals (`Logger`, `ConfigSource`...), as in `Application.create`. |
|
|
193
|
+
| `overrides` | `Map` / `[key, value][]` / record | Values or provider definitions replacing providers, applied last. Overridden providers are instantiated at boot. |
|
|
194
|
+
| `only` | `'all'` \| `(Class \| Token)[]` | Providers instantiated at boot (with their dependencies), plus the overrides and the plugin roots (below). Default: the entry points of the metadata (HTTP controllers with their guards and interceptors, message consumers, hosts of scheduled jobs, GraphQL resolvers with their guards and loaders, WebSocket gateways with their handshake and message guards), or every provider when there is none. |
|
|
195
|
+
| `start` | `boolean` | Run `app.start()` (plugins listen/subscribe). Default `true`; `false` stops after `onBootstrap`. |
|
|
196
|
+
| `logger` | `CoreLogger` | Default: a console logger writing warnings and errors only. |
|
|
197
|
+
| `shutdown` | `{ timeoutMs?, hookTimeoutMs? }` | Shutdown timeouts. |
|
|
198
198
|
|
|
199
199
|
Keys of `providers` and `overrides` may be classes, tokens, aliases or unique display names
|
|
200
200
|
(`'UserRepository'`), like in `TestBed`; other strings are used as tokens (values for tokens
|
|
@@ -305,6 +305,27 @@ test('uploads and downloads', async () => {
|
|
|
305
305
|
})
|
|
306
306
|
```
|
|
307
307
|
|
|
308
|
+
### WebSocket connections
|
|
309
|
+
|
|
310
|
+
`app.connect(path, { query?, headers?, protocols?, origin?, cookies? })` runs the real WebSocket
|
|
311
|
+
handshake of `WebSocketPlugin` (`@znight/websocket`) through the HTTP pipeline, in memory, and
|
|
312
|
+
resolves with a `TestWsClient`: `request(event, data?)` (the reply's data; rejects with a
|
|
313
|
+
`WsRequestError` `{ code, message, details }`), `next(event?)` (the oldest server event not taken
|
|
314
|
+
yet: frames are buffered from the start), `send`, `sendRaw`, `nextRaw`, `frames`, `close()` and
|
|
315
|
+
`closed`. A refused handshake rejects with a `WsHandshakeError` (`status`, `headers`, `body`,
|
|
316
|
+
`json()`). `connectWebSocket(url, { headers?, protocols? })` gives the same client over the network
|
|
317
|
+
(the global `WebSocket` of Node.js and Bun).
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
import { connectWebSocket, WsHandshakeError } from '@znight/testing'
|
|
321
|
+
|
|
322
|
+
await using alice = await app.connect('/chat', { protocols: ['chat.v1'] })
|
|
323
|
+
expect(await alice.request('room.join', { room: 'general' })).toEqual({ members: 1 })
|
|
324
|
+
await expect(app.connect('/chat', { origin: 'https://evil.example' })).rejects.toBeInstanceOf(WsHandshakeError)
|
|
325
|
+
|
|
326
|
+
await using remote = await connectWebSocket(`ws://127.0.0.1:${port}/chat`, { protocols: ['chat.v1'] })
|
|
327
|
+
```
|
|
328
|
+
|
|
308
329
|
## Mocks
|
|
309
330
|
|
|
310
331
|
```ts
|
|
@@ -425,27 +446,7 @@ without importing any feature package:
|
|
|
425
446
|
| `extensions.messaging.consumers[].token` | `TestApp` boot roots |
|
|
426
447
|
| `extensions.schedule.jobs[].token` | `TestApp` boot roots |
|
|
427
448
|
| `extensions.graphql.types[kind: 'object'].fields[].resolver.{ token, guards, loaders }` | `TestApp` boot roots |
|
|
428
|
-
|
|
429
|
-
### WebSocket connections
|
|
430
|
-
|
|
431
|
-
`app.connect(path, { query?, headers?, protocols?, origin?, cookies? })` runs the real WebSocket
|
|
432
|
-
handshake of `WebSocketPlugin` (`@znight/websocket`) through the HTTP pipeline, in memory, and
|
|
433
|
-
resolves with a `TestWsClient`: `request(event, data?)` (the reply's data; rejects with a
|
|
434
|
-
`WsRequestError` `{ code, message, details }`), `next(event?)` (the oldest server event not taken
|
|
435
|
-
yet: frames are buffered from the start), `send`, `sendRaw`, `nextRaw`, `frames`, `close()` and
|
|
436
|
-
`closed`. A refused handshake rejects with a `WsHandshakeError` (`status`, `headers`, `body`,
|
|
437
|
-
`json()`). `connectWebSocket(url, { headers?, protocols? })` gives the same client over the network
|
|
438
|
-
(the global `WebSocket` of Node.js and Bun).
|
|
439
|
-
|
|
440
|
-
```ts
|
|
441
|
-
import { connectWebSocket, WsHandshakeError } from '@znight/testing'
|
|
442
|
-
|
|
443
|
-
await using alice = await app.connect('/chat', { protocols: ['chat.v1'] })
|
|
444
|
-
expect(await alice.request('room.join', { room: 'general' })).toEqual({ members: 1 })
|
|
445
|
-
await expect(app.connect('/chat', { origin: 'https://evil.example' })).rejects.toBeInstanceOf(WsHandshakeError)
|
|
446
|
-
|
|
447
|
-
await using remote = await connectWebSocket(`ws://127.0.0.1:${port}/chat`, { protocols: ['chat.v1'] })
|
|
448
|
-
```
|
|
449
|
+
| `extensions.websocket.gateways[].{ token, handshakeGuards, messageGuards, handlers[].guards }` | `TestApp` boot roots |
|
|
449
450
|
|
|
450
451
|
Runtime contract with `@znight/http`: the plugin named `'http'` exposes
|
|
451
452
|
`inject(request: { method: string; url: string; headers?; body?; query?; cookies?; signedCookies? }): Promise<{ status: number; headers; body: string; bytes?: Uint8Array; cookies?: Record<string, SetCookieInfo>; json<T>(): T }>`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@znight/testing",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Znight testing utilities: TestBed with auto-mocking and TestApp for end-to-end tests",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ugho STEPHAN",
|
|
@@ -46,6 +46,6 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@znight/core": "0.6.
|
|
49
|
+
"@znight/core": "0.6.2"
|
|
50
50
|
}
|
|
51
51
|
}
|