mockaton 8.2.7 → 8.2.9
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 +78 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,22 +62,47 @@ Experiment with the Dashboard:
|
|
|
62
62
|
- Pick a mock variant from the Mock dropdown (we’ll discuss them later)
|
|
63
63
|
- Toggle the 🕓 Clock button, which _Delays_ responses (e.g. for testing spinners)
|
|
64
64
|
- Toggle the _500_ button, which sends and _Internal Server Error_ on that endpoint
|
|
65
|
+
- Click `index.html` in the "Static" section
|
|
66
|
+
- see what for in the "Deterministic Standalone Demo Server" use case below
|
|
65
67
|
|
|
66
|
-
Finally, edit a mock file. You don’t need to restart Mockaton for that.
|
|
67
|
-
_Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
68
|
+
Finally, edit a mock file in your IDE. You don’t need to restart Mockaton for that.
|
|
69
|
+
The _Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
68
70
|
|
|
69
71
|
|
|
70
72
|
## Use Cases
|
|
71
|
-
|
|
72
|
-
-
|
|
73
|
-
-
|
|
74
|
-
-
|
|
75
|
-
-
|
|
76
|
-
-
|
|
77
|
-
-
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
### Testing
|
|
74
|
+
- Empty responses
|
|
75
|
+
- Spinners by delaying responses
|
|
76
|
+
- Errors such as _Bad Request_ and _Internal Server Error_
|
|
77
|
+
- Setting up UI tests
|
|
78
|
+
- Polled resources (trigger different states)
|
|
79
|
+
- alerts
|
|
80
|
+
- notifications
|
|
81
|
+
- slow to build assets
|
|
82
|
+
|
|
83
|
+
### Prototype Ahead of Backend
|
|
84
|
+
Sometimes, frontend progress is needlessly blocked waiting for some
|
|
85
|
+
backend API. Similarly, it’s often delayed due to missing data or inconvenient
|
|
86
|
+
contracts. Therefore, many meetings can be saved by prototyping frontend
|
|
87
|
+
features with mocks, and then showing those contracts to the backend team.
|
|
88
|
+
|
|
89
|
+
They won’t like it at first.
|
|
90
|
+
|
|
91
|
+
### Time Travel
|
|
92
|
+
If you commit the mocks in the repo, when bisecting a bug, you don’t
|
|
93
|
+
have to sync the frontend with many backend repos. Similarly, it
|
|
94
|
+
allows for checking out long-lived branches that have old API contracts.
|
|
95
|
+
|
|
96
|
+
### Deterministic Standalone Demo Server
|
|
97
|
+
Perhaps you need to demo your app, but the ideal flow is too complex to
|
|
98
|
+
simulate from the actual backend. In this case, compile your frontend app and
|
|
99
|
+
put its built assets in `Config.staticDir`. Then, from the Mockaton dashboard
|
|
100
|
+
you can "Bulk Select" mocks to simulate the complete states you want to demo.
|
|
101
|
+
|
|
102
|
+
For bulk-selecting, you just need to add a comment to the mock
|
|
103
|
+
filename. For example, `(demo-part1)`, `(demo-part2)`. See the
|
|
104
|
+
"Comments" section under the "Filename Convention" for details.
|
|
105
|
+
|
|
81
106
|
|
|
82
107
|
## Motivation
|
|
83
108
|
- Avoids spinning up and maintaining hefty backends when developing UIs.
|
|
@@ -111,49 +136,66 @@ api/user(default).GET.200.json
|
|
|
111
136
|
---
|
|
112
137
|
|
|
113
138
|
## You can write JSON mocks in JavaScript or TypeScript
|
|
139
|
+
For example, `api/foo.GET.200.js`
|
|
140
|
+
|
|
114
141
|
|
|
115
142
|
**Option A:** An Object, Array, or String is sent as JSON.
|
|
116
143
|
|
|
117
|
-
`api/foo.GET.200.js`
|
|
118
144
|
```js
|
|
119
|
-
export default [
|
|
120
|
-
{ id: 0 }
|
|
121
|
-
]
|
|
145
|
+
export default [{ foo: 'bar' }]
|
|
122
146
|
```
|
|
123
147
|
|
|
124
148
|
**Option B:** Function
|
|
125
149
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
150
|
+
Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
export default (request, response) => JSON.stringify({ foo: 'bar' })
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Think of these functions as HTTP handlers. You can read or write to a
|
|
157
|
+
database, or pull data from a backend.
|
|
129
158
|
|
|
130
|
-
|
|
159
|
+
<details>
|
|
160
|
+
<summary><b>See More Examples</b></summary>
|
|
131
161
|
|
|
132
|
-
|
|
133
|
-
|
|
162
|
+
Imagine you have an initial list of colors, and
|
|
163
|
+
you want to concatenate newly added colors.
|
|
134
164
|
|
|
165
|
+
`api/colors.POST.201.js`
|
|
135
166
|
```js
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
167
|
+
import { parseJSON } from 'mockaton'
|
|
168
|
+
|
|
169
|
+
export default async function insertColor(request, response) {
|
|
170
|
+
const color = await parseJSON(request)
|
|
171
|
+
globalThis.newColorsDatabase ??= []
|
|
172
|
+
globalThis.newColorsDatabase.push(color)
|
|
173
|
+
|
|
174
|
+
// These two lines are not needed but you can change their values
|
|
175
|
+
// response.statusCode = 201 // default derived from filename
|
|
176
|
+
// response.setHeader('Content-Type', 'application/json') // unconditional default
|
|
177
|
+
|
|
178
|
+
return JSON.stringify({ msg: 'CREATED' })
|
|
142
179
|
}
|
|
143
180
|
```
|
|
144
181
|
|
|
145
|
-
|
|
182
|
+
`api/colors.GET.200.js`
|
|
146
183
|
```js
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
import colors from './colors.json' with { type: 'json' }
|
|
150
|
-
import { parseJSON } from 'mockaton' // body-parser alike
|
|
184
|
+
import colorsFixture from './colors.json' with { type: 'json' }
|
|
151
185
|
|
|
152
|
-
export default
|
|
153
|
-
|
|
154
|
-
|
|
186
|
+
export default function listColors() {
|
|
187
|
+
return JSON.stringify([
|
|
188
|
+
...colorsFixture,
|
|
189
|
+
...(globalThis.newColorsDatabase || [])
|
|
190
|
+
])
|
|
155
191
|
}
|
|
156
192
|
```
|
|
193
|
+
</details>
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
If you are wondering, what if I need to serve a static `.js`?
|
|
198
|
+
Put it in your `Config.staticDir` without the mock filename convention.
|
|
157
199
|
|
|
158
200
|
---
|
|
159
201
|
|
|
@@ -268,7 +310,6 @@ signature. In other words, it’s useful if you only care about its payload.
|
|
|
268
310
|
```js
|
|
269
311
|
import { jwtCookie } from 'mockaton'
|
|
270
312
|
|
|
271
|
-
|
|
272
313
|
Config.cookies = {
|
|
273
314
|
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
274
315
|
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
@@ -322,7 +363,6 @@ import { parse } from 'yaml'
|
|
|
322
363
|
import { readFileSync } from 'node:js'
|
|
323
364
|
import { jsToJsonPlugin } from 'mockaton'
|
|
324
365
|
|
|
325
|
-
|
|
326
366
|
Config.plugins = [
|
|
327
367
|
[/\.(js|ts)$/, jsToJsonPlugin], // Default
|
|
328
368
|
[/\.yml$/, yamlToJsonPlugin],
|
|
@@ -371,7 +411,6 @@ For a more cross-platform utility, you could `npm install open` and pass it.
|
|
|
371
411
|
```js
|
|
372
412
|
import open from 'open'
|
|
373
413
|
|
|
374
|
-
|
|
375
414
|
Config.onReady = open
|
|
376
415
|
```
|
|
377
416
|
|
|
@@ -383,7 +422,6 @@ All of its methods return their `fetch` response promise.
|
|
|
383
422
|
```js
|
|
384
423
|
import { Commander } from 'mockaton'
|
|
385
424
|
|
|
386
|
-
|
|
387
425
|
const myMockatonAddr = 'http://localhost:2345'
|
|
388
426
|
const mockaton = new Commander(myMockatonAddr)
|
|
389
427
|
```
|
package/package.json
CHANGED