@wolfstar/http-framework-test-utils 3.0.0 → 3.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/README.md +112 -0
- package/package.json +4 -4
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# `@wolfstar/http-framework-test-utils`
|
|
2
|
+
|
|
3
|
+
Test utilities and fixtures for [`@wolfstar/http-framework`](https://github.com/wolfstar-project/stars-components/tree/main/packages/http-framework) — designed to work with [Vitest](https://vitest.dev/).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @wolfstar/http-framework-test-utils vitest
|
|
9
|
+
# or
|
|
10
|
+
pnpm add -D @wolfstar/http-framework-test-utils vitest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### `createTestHarness`
|
|
16
|
+
|
|
17
|
+
The entry point for most tests. Returns a `TestableClient` and an `InteractionTestRunner` wired together:
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { createTestHarness } from '@wolfstar/http-framework-test-utils';
|
|
21
|
+
|
|
22
|
+
const { client, runner } = createTestHarness();
|
|
23
|
+
|
|
24
|
+
// Load your commands before running tests
|
|
25
|
+
await client.load();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Running interactions
|
|
29
|
+
|
|
30
|
+
Use `InteractionTestRunner.run()` to dispatch a Discord interaction and get back a typed result:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { ChatInputApplicationCommandInteractionData } from '@wolfstar/http-framework-test-utils';
|
|
34
|
+
|
|
35
|
+
const result = await runner.run({
|
|
36
|
+
...ChatInputApplicationCommandInteractionData,
|
|
37
|
+
data: { id: '0', name: 'ping', type: 1, options: [] }
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(result.statusCode).toBe(200);
|
|
41
|
+
expect(result.json()).toMatchObject({ type: 4, data: { content: 'Pong!' } });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Vitest custom matchers
|
|
45
|
+
|
|
46
|
+
Import and register the matchers in your Vitest setup file:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// vitest.setup.ts
|
|
50
|
+
import { expect } from 'vitest';
|
|
51
|
+
import { httpFrameworkMatchers } from '@wolfstar/http-framework-test-utils/vitest';
|
|
52
|
+
|
|
53
|
+
expect.extend(httpFrameworkMatchers);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Then in tests:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
expect(result).toHaveStatus(200);
|
|
60
|
+
expect(result).toHaveBody('{"type":4}');
|
|
61
|
+
expect(result).toHaveJsonBody({ type: 4, data: { content: 'Pong!' } });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`toHaveJsonBody` performs a strict deep-equality check: the expected object must contain the exact same set of keys as the response body at every level. If you only care about a subset of fields, assert against `result.json()` with Vitest's built-in `toMatchObject` instead.
|
|
65
|
+
|
|
66
|
+
### Available fixtures
|
|
67
|
+
|
|
68
|
+
Pre-built interaction payloads for all interaction types:
|
|
69
|
+
|
|
70
|
+
| Export | Type |
|
|
71
|
+
| -------------------------------------------------- | ---------------------------- |
|
|
72
|
+
| `ChatInputApplicationCommandInteractionData` | Chat input command |
|
|
73
|
+
| `MessageApplicationCommandInteractionData` | Message context menu command |
|
|
74
|
+
| `UserApplicationCommandInteractionData` | User context menu command |
|
|
75
|
+
| `ApplicationCommandAutocompleteInteractionData` | Autocomplete |
|
|
76
|
+
| `MessageComponentButtonInteractionData` | Button |
|
|
77
|
+
| `MessageComponentStringSelectInteractionData` | String select menu |
|
|
78
|
+
| `MessageComponentChannelSelectInteractionData` | Channel select menu |
|
|
79
|
+
| `MessageComponentRoleSelectInteractionData` | Role select menu |
|
|
80
|
+
| `MessageComponentUserSelectInteractionData` | User select menu |
|
|
81
|
+
| `MessageComponentMentionableSelectInteractionData` | Mentionable select menu |
|
|
82
|
+
| `ModalSubmitInteractionData` | Modal submit |
|
|
83
|
+
|
|
84
|
+
Base data is also exported: `UserData`, `InteractionGuildMemberData`, `MessageData`, `BaseInteractionData`.
|
|
85
|
+
|
|
86
|
+
### Helper utilities
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { buildSubcommand, getAndDelete, makeCommand } from '@wolfstar/http-framework-test-utils';
|
|
90
|
+
|
|
91
|
+
// Build a SlashCommandSubcommandBuilder
|
|
92
|
+
const sub = buildSubcommand('add', 'Adds two numbers');
|
|
93
|
+
|
|
94
|
+
// Construct a command instance for unit testing
|
|
95
|
+
const cmd = makeCommand(MyCommand);
|
|
96
|
+
|
|
97
|
+
// Retrieve and clean up a command's registry entry
|
|
98
|
+
const entry = getAndDelete(MyCommand);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Exports
|
|
102
|
+
|
|
103
|
+
| Entry point | Contents |
|
|
104
|
+
| -------------------------------------------- | ------------------------------------------------ |
|
|
105
|
+
| `@wolfstar/http-framework-test-utils` | All utilities, fixtures, runners, and types |
|
|
106
|
+
| `@wolfstar/http-framework-test-utils/vitest` | Custom Vitest matchers (`httpFrameworkMatchers`) |
|
|
107
|
+
|
|
108
|
+
## Requirements
|
|
109
|
+
|
|
110
|
+
- Node.js `>=20`
|
|
111
|
+
- `@wolfstar/http-framework` (peer dependency)
|
|
112
|
+
- `vitest >=4.0.0` (optional peer dependency — only needed for the `/vitest` entry point)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wolfstar/http-framework-test-utils",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Test utilities for @wolfstar/http-framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"discord",
|
|
@@ -54,11 +54,11 @@
|
|
|
54
54
|
"tsdown": "^0.22.3",
|
|
55
55
|
"typescript": "~5.8.3",
|
|
56
56
|
"vitest": "^4.1.9",
|
|
57
|
-
"@wolfstar/http-framework": "3.
|
|
57
|
+
"@wolfstar/http-framework": "3.1.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"vitest": ">=4.
|
|
61
|
-
"@wolfstar/http-framework": "^3.
|
|
60
|
+
"vitest": ">=4.1.9",
|
|
61
|
+
"@wolfstar/http-framework": "^3.1.0"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|
|
64
64
|
"vitest": {
|