@motiadev/test 0.8.2-beta.140-957262 → 0.8.2-beta.140-628177
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 +42 -41
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/src/types.d.ts +2 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -14,26 +14,24 @@ pnpm add @motiadev/test --save-dev
|
|
|
14
14
|
|
|
15
15
|
## Features
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
* Create mock flow contexts for testing steps and workflows
|
|
18
|
+
* Simulate event emission and capture events
|
|
19
|
+
* Mock loggers for testing
|
|
20
|
+
* Test event-driven workflows in isolation
|
|
21
|
+
* Utilities for testing state management
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
25
|
### Creating a Tester
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
|
-
import {
|
|
28
|
+
import { createMotiaTester } from '@motiadev/test';
|
|
29
29
|
|
|
30
30
|
// Create a tester instance
|
|
31
|
-
const tester =
|
|
31
|
+
const tester = createMotiaTester();
|
|
32
32
|
|
|
33
33
|
// Use the tester to test your workflows
|
|
34
|
-
const response = await tester.
|
|
35
|
-
.post('/api/endpoint')
|
|
36
|
-
.send({ data: 'test' });
|
|
34
|
+
const response = await tester.post('/api/endpoint', { body: { data: 'test' } });
|
|
37
35
|
|
|
38
36
|
// Assert on the response
|
|
39
37
|
expect(response.status).toBe(200);
|
|
@@ -42,20 +40,18 @@ expect(response.status).toBe(200);
|
|
|
42
40
|
### Capturing Events
|
|
43
41
|
|
|
44
42
|
```typescript
|
|
45
|
-
import {
|
|
43
|
+
import { createMotiaTester } from '@motiadev/test';
|
|
46
44
|
|
|
47
|
-
const tester =
|
|
45
|
+
const tester = createMotiaTester();
|
|
48
46
|
|
|
49
47
|
// Set up event capturing
|
|
50
|
-
const watcher = tester.
|
|
48
|
+
const watcher = tester.watch('event.topic');
|
|
51
49
|
|
|
52
50
|
// Trigger an action that emits events
|
|
53
|
-
await tester.
|
|
54
|
-
.post('/api/trigger')
|
|
55
|
-
.send({ action: 'test' });
|
|
51
|
+
await tester.post('/api/trigger', { body: { action: 'test' } });
|
|
56
52
|
|
|
57
53
|
// Get captured events
|
|
58
|
-
const events = watcher
|
|
54
|
+
const events = await watcher;
|
|
59
55
|
expect(events).toHaveLength(1);
|
|
60
56
|
expect(events[0].data).toEqual({ result: 'success' });
|
|
61
57
|
```
|
|
@@ -90,79 +86,84 @@ const logger = createMockLogger();
|
|
|
90
86
|
logger.info('Test message');
|
|
91
87
|
|
|
92
88
|
// Assert on logged messages
|
|
93
|
-
expect(logger.
|
|
89
|
+
expect(logger.info).toHaveBeenCalledWith('Test message');
|
|
94
90
|
```
|
|
95
91
|
|
|
96
92
|
## API Reference
|
|
97
93
|
|
|
98
|
-
### `
|
|
94
|
+
### `createMotiaTester()`
|
|
99
95
|
|
|
100
96
|
Creates a tester instance for testing Motia workflows.
|
|
101
97
|
|
|
102
98
|
**Returns:**
|
|
103
|
-
|
|
99
|
+
|
|
100
|
+
* `MotiaTester`: Tester instance with methods for posting requests, watching events, and closing the tester.
|
|
104
101
|
|
|
105
102
|
### `createMockFlowContext()`
|
|
106
103
|
|
|
107
104
|
Creates a mock flow context for testing step handlers.
|
|
108
105
|
|
|
109
106
|
**Returns:**
|
|
110
|
-
|
|
107
|
+
|
|
108
|
+
* `MockFlowContext`: Mocked context with spied methods like `emit` and `logger`.
|
|
111
109
|
|
|
112
110
|
### `createMockLogger()`
|
|
113
111
|
|
|
114
112
|
Creates a mock logger for testing logging functionality.
|
|
115
113
|
|
|
116
114
|
**Returns:**
|
|
117
|
-
|
|
115
|
+
|
|
116
|
+
* `MockLogger`: Mock logger with all standard logging methods mocked.
|
|
118
117
|
|
|
119
118
|
### `MotiaTester` Methods
|
|
120
119
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
120
|
+
* `post(path, options)` → Send POST requests to your endpoint.
|
|
121
|
+
* `get(path, options)` → Send GET requests.
|
|
122
|
+
* `watch(event)` → Capture events emitted on a topic.
|
|
123
|
+
* `waitEvents()` → Wait for all events to be processed.
|
|
124
|
+
* `emit(event)` → Emit an event.
|
|
125
|
+
* `close()` → Clean up resources after testing.
|
|
124
126
|
|
|
125
127
|
### `Watcher` Methods
|
|
126
128
|
|
|
127
|
-
|
|
129
|
+
* Returns a Promise of captured events.
|
|
130
|
+
* `getCapturedEvents()` → Get an array of captured events.
|
|
128
131
|
|
|
129
132
|
## Example: Testing a Complete Flow
|
|
130
133
|
|
|
131
134
|
```typescript
|
|
132
|
-
import {
|
|
135
|
+
import { createMotiaTester } from '@motiadev/test';
|
|
133
136
|
import { expect, test } from 'vitest';
|
|
134
137
|
|
|
135
138
|
test('complete order flow works correctly', async () => {
|
|
136
|
-
const tester =
|
|
139
|
+
const tester = createMotiaTester();
|
|
137
140
|
|
|
138
141
|
// Watch for order completion events
|
|
139
|
-
|
|
142
|
+
const orderCompletedWatcher = tester.watch('order.completed');
|
|
143
|
+
|
|
140
144
|
|
|
141
145
|
// Trigger the order creation
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
.send({
|
|
146
|
+
const response = await tester.post('/api/orders', {
|
|
147
|
+
body: {
|
|
145
148
|
items: [{ id: 'item1', quantity: 2 }],
|
|
146
149
|
customer: { id: 'cust1', email: 'test@example.com' }
|
|
147
|
-
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
148
152
|
|
|
149
153
|
// Verify the API response
|
|
150
154
|
expect(response.status).toBe(200);
|
|
151
155
|
expect(response.body).toHaveProperty('orderId');
|
|
152
156
|
|
|
153
157
|
// Wait for all events to be processed
|
|
154
|
-
await tester.
|
|
158
|
+
await tester.waitEvents();
|
|
155
159
|
|
|
156
160
|
// Verify the order completed event was emitted
|
|
157
|
-
const completedEvents = orderCompletedWatcher
|
|
161
|
+
const completedEvents = await orderCompletedWatcher;
|
|
158
162
|
expect(completedEvents).toHaveLength(1);
|
|
159
|
-
expect(completedEvents[0].data).toMatchObject({
|
|
160
|
-
|
|
161
|
-
status: 'completed'
|
|
162
|
-
});
|
|
163
|
-
|
|
163
|
+
expect(completedEvents[0].data).toMatchObject({ orderId: expect.any(String), status: 'completed' });
|
|
164
|
+
|
|
164
165
|
// Clean up
|
|
165
|
-
|
|
166
|
+
await tester.close();
|
|
166
167
|
});
|
|
167
168
|
```
|
|
168
169
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./src/tester"), exports);
|
|
18
18
|
__exportStar(require("./src/types"), exports);
|
|
19
19
|
__exportStar(require("./src/helpers"), exports);
|
|
20
|
+
__exportStar(require("./src/event-manager"), exports);
|
package/dist/src/types.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ export interface MotiaTester {
|
|
|
17
17
|
}
|
|
18
18
|
export type RequestOptions = {
|
|
19
19
|
body?: Record<string, unknown>;
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
cookies?: Record<string, string>;
|
|
20
22
|
};
|
|
21
23
|
export type CapturedEvent<TData = unknown> = Omit<Event<TData>, 'logger' | 'tracer'>;
|
|
22
24
|
export type MockFlowContext = {
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/test",
|
|
3
3
|
"description": "A testing utility package for Motia workflows that provides tools for mocking, testing, and simulating Motia components.",
|
|
4
|
-
"version": "0.8.2-beta.140-
|
|
4
|
+
"version": "0.8.2-beta.140-628177",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@motiadev/core": "0.8.2-beta.140-
|
|
9
|
-
"motia": "0.8.2-beta.140-
|
|
8
|
+
"@motiadev/core": "0.8.2-beta.140-628177",
|
|
9
|
+
"motia": "0.8.2-beta.140-628177"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
12
|
+
"jest": "^29.7.0",
|
|
13
|
+
"supertest": "^7.1.4"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/jest": "^29.5.14",
|