mockaton 8.2.8 → 8.2.10
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 +70 -50
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
<img src="src/mockaton-logo.svg" alt="Mockaton Logo" width="210" style="margin-top: 30px"/>
|
|
2
2
|
|
|
3
|
-
_Mockaton_ is
|
|
3
|
+
_Mockaton_ is an HTTP mock server for improving the frontend
|
|
4
|
+
development and testing experience.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
Mockaton scans a given directory for filenames following a convention similar to
|
|
8
|
+
the URL paths. For example, the following mock will be served on `/api/user/1234`
|
|
7
9
|
```
|
|
8
10
|
my-mocks-dir/api/user/[user-id].GET.200.json
|
|
9
11
|
```
|
|
12
|
+
You don’t need to mock everything. Indicate your backend address in
|
|
13
|
+
`Config.proxyFallback`, and Mockaton will request from it routes you don’t have mocks for.
|
|
10
14
|
|
|
11
|
-
By the way, [this browser extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
|
|
12
|
-
can create a TAR of your requests following that convention.
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
By the way, [this browser
|
|
17
|
+
extension](https://github.com/ericfortis/devtools-ext-tar-http-requests) can
|
|
18
|
+
create a TAR of your requests following that convention.
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
## Dashboard UI
|
|
19
21
|
|
|
20
22
|
<picture>
|
|
21
23
|
<source media="(prefers-color-scheme: light)" srcset="./README-dashboard-light.png">
|
|
@@ -48,19 +50,18 @@ node --import=tsx my-mockaton.js
|
|
|
48
50
|
```
|
|
49
51
|
|
|
50
52
|
|
|
51
|
-
## Running the Example
|
|
53
|
+
## Running the Demo Example
|
|
52
54
|
This demo uses the [sample-mocks/](./sample-mocks) directory of this repository.
|
|
53
55
|
|
|
54
|
-
-
|
|
55
|
-
|
|
56
|
-
- `cd mockaton`
|
|
56
|
+
- `git clone https://github.com/ericfortis/mockaton.git`
|
|
57
|
+
- `cd mockaton`
|
|
57
58
|
- `npm install tsx` (optional)
|
|
58
59
|
- `npm run demo:ts` it will open a dashboard
|
|
59
60
|
|
|
60
61
|
Experiment with the Dashboard:
|
|
61
62
|
|
|
62
63
|
- Pick a mock variant from the Mock dropdown (we’ll discuss them later)
|
|
63
|
-
- Toggle the 🕓
|
|
64
|
+
- Toggle the 🕓 _Delay Responses_ button, (e.g. for testing spinners)
|
|
64
65
|
- Toggle the _500_ button, which sends and _Internal Server Error_ on that endpoint
|
|
65
66
|
|
|
66
67
|
Finally, edit a mock file in your IDE. You don’t need to restart Mockaton for that.
|
|
@@ -68,21 +69,39 @@ The _Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
|
68
69
|
|
|
69
70
|
|
|
70
71
|
## Use Cases
|
|
71
|
-
|
|
72
|
-
-
|
|
73
|
-
-
|
|
74
|
-
-
|
|
75
|
-
-
|
|
76
|
-
-
|
|
77
|
-
-
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
### Testing
|
|
73
|
+
- Empty responses
|
|
74
|
+
- Spinners by delaying responses
|
|
75
|
+
- Errors such as _Bad Request_ and _Internal Server Error_
|
|
76
|
+
- Setting up UI tests
|
|
77
|
+
- Polled resources (for triggering their different states)
|
|
78
|
+
- alerts
|
|
79
|
+
- notifications
|
|
80
|
+
- slow to build assets
|
|
81
|
+
|
|
82
|
+
### Time Travel
|
|
83
|
+
If you commit the mocks in the repo, when bisecting a bug, you don’t
|
|
84
|
+
have to sync the frontend with many backend repos. Similarly, it
|
|
85
|
+
allows for checking out long-lived branches with old API contracts.
|
|
86
|
+
|
|
87
|
+
### Deterministic Standalone Demo Server
|
|
88
|
+
Perhaps you need to demo your app, but the ideal flow is too complex to
|
|
89
|
+
simulate from the actual backend. In this case, compile your frontend app and
|
|
90
|
+
put its built assets in `Config.staticDir`. Then, from the Mockaton dashboard
|
|
91
|
+
you can "Bulk Select" mocks to simulate the complete states you want to demo.
|
|
92
|
+
For bulk-selecting, you just need to add a comment to the mock
|
|
93
|
+
filename. For example, `(demo-part1)`, `(demo-part2)`. See the
|
|
94
|
+
"Comments" section under the "Filename Convention" for details.
|
|
95
|
+
|
|
81
96
|
|
|
82
97
|
## Motivation
|
|
83
98
|
- Avoids spinning up and maintaining hefty backends when developing UIs.
|
|
84
99
|
- For a deterministic and comprehensive backend state. For example, having all the possible
|
|
85
100
|
state variants of a collection helps for spotting inadvertent bugs.
|
|
101
|
+
- Sometimes, frontend progress is blocked waiting for some backend API. Similarly,
|
|
102
|
+
it’s often delayed due to missing data or inconvenient contracts. Therefore,
|
|
103
|
+
many meetings can be saved by prototyping frontend features with mocks, and
|
|
104
|
+
then showing those contracts to the backend team.
|
|
86
105
|
|
|
87
106
|
## Alternatives
|
|
88
107
|
- Chrome DevTools allows for [overriding responses](https://developer.chrome.com/docs/devtools/overrides)
|
|
@@ -91,9 +110,9 @@ The _Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
|
91
110
|
|
|
92
111
|
---
|
|
93
112
|
|
|
94
|
-
## Mock Variants
|
|
113
|
+
## Multiple Mock Variants
|
|
95
114
|
Each route can have many mocks, which could either be:
|
|
96
|
-
- Different response __status code__. For example, for
|
|
115
|
+
- Different response __status code__. For example, for triggering errors.
|
|
97
116
|
- __Comment__ on the filename, which is anything within parentheses.
|
|
98
117
|
- e.g. `api/login(locked out user).POST.423.json`
|
|
99
118
|
|
|
@@ -134,19 +153,19 @@ database, or pull data from a backend.
|
|
|
134
153
|
<details>
|
|
135
154
|
<summary><b>See More Examples</b></summary>
|
|
136
155
|
|
|
137
|
-
|
|
138
|
-
|
|
156
|
+
Imagine you have an initial list of colors, and
|
|
157
|
+
you want to concatenate newly added colors.
|
|
139
158
|
|
|
140
159
|
`api/colors.POST.201.js`
|
|
141
160
|
```js
|
|
142
|
-
import { parseJSON } from 'mockaton'
|
|
161
|
+
import { parseJSON } from 'mockaton'
|
|
143
162
|
|
|
144
163
|
export default async function insertColor(request, response) {
|
|
145
164
|
const color = await parseJSON(request)
|
|
146
165
|
globalThis.newColorsDatabase ??= []
|
|
147
166
|
globalThis.newColorsDatabase.push(color)
|
|
148
167
|
|
|
149
|
-
// These two lines are not needed but you can change
|
|
168
|
+
// These two lines are not needed but you can change their values
|
|
150
169
|
// response.statusCode = 201 // default derived from filename
|
|
151
170
|
// response.setHeader('Content-Type', 'application/json') // unconditional default
|
|
152
171
|
|
|
@@ -174,7 +193,7 @@ Put it in your `Config.staticDir` without the mock filename convention.
|
|
|
174
193
|
|
|
175
194
|
---
|
|
176
195
|
|
|
177
|
-
## Mock
|
|
196
|
+
## Mock Filename Convention
|
|
178
197
|
|
|
179
198
|
### Extension
|
|
180
199
|
|
|
@@ -206,11 +225,9 @@ api/foo.GET.200.json
|
|
|
206
225
|
api/video<b>?limit=[limit]</b>.GET.200.json
|
|
207
226
|
</pre>
|
|
208
227
|
|
|
209
|
-
The query string is ignored when routing to it. In other words, it’s
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
Speaking of which, in Windows, filenames containing "?" are [not
|
|
213
|
-
permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
228
|
+
The query string is ignored when routing to it. In other words, it’s only used for
|
|
229
|
+
documenting the URL contract. Speaking of which, in Windows, filenames containing "?" are
|
|
230
|
+
[not permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
214
231
|
but since that’s part of the query string, it’s ignored anyway.
|
|
215
232
|
|
|
216
233
|
|
|
@@ -234,9 +251,11 @@ api/foo/.GET.200.json
|
|
|
234
251
|
### `mocksDir: string`
|
|
235
252
|
This is the only required field
|
|
236
253
|
|
|
254
|
+
|
|
237
255
|
### `host?: string`
|
|
238
256
|
Defaults to `'localhost'`
|
|
239
257
|
|
|
258
|
+
|
|
240
259
|
### `port?: number`
|
|
241
260
|
Defaults to `0`, which means auto-assigned
|
|
242
261
|
|
|
@@ -246,9 +265,8 @@ Defaults to `/(\.DS_Store|~)$/`
|
|
|
246
265
|
|
|
247
266
|
|
|
248
267
|
### `delay?: number` 🕓
|
|
249
|
-
The clock icon next to the mock selector is a checkbox for delaying a particular
|
|
250
|
-
|
|
251
|
-
The delay is globally configurable via `Config.delay = 1200` (milliseconds).
|
|
268
|
+
The clock icon next to the mock selector is a checkbox for delaying a particular
|
|
269
|
+
response. The delay is globally configurable via `Config.delay = 1200` (milliseconds).
|
|
252
270
|
|
|
253
271
|
|
|
254
272
|
### `proxyFallback?: string`
|
|
@@ -259,25 +277,22 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
|
|
|
259
277
|
### `staticDir?: string`
|
|
260
278
|
Files under `Config.staticDir` don’t use the filename convention.
|
|
261
279
|
Also, they take precedence over the `GET` mocks in `Config.mocksDir`.
|
|
262
|
-
|
|
263
280
|
For example, if you have two files for `GET /foo/bar.jpg`
|
|
264
281
|
```
|
|
265
282
|
my-static-dir/foo/bar.jpg
|
|
266
283
|
my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
|
|
267
284
|
```
|
|
268
285
|
|
|
269
|
-
Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
270
|
-
|
|
271
|
-
Use Case 2: For a standalone demo server. For example,
|
|
286
|
+
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
287
|
+
- Use Case 2: For a standalone demo server. For example,
|
|
272
288
|
build your frontend bundle, and serve it from Mockaton.
|
|
273
289
|
|
|
274
290
|
|
|
275
291
|
### `cookies?: { [label: string]: string }`
|
|
276
292
|
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
277
|
-
|
|
278
293
|
The key is just a label used for selecting a particular cookie in the
|
|
279
|
-
dashboard. In the dashboard, only one cookie can be selected. If you need
|
|
280
|
-
|
|
294
|
+
dashboard. In the dashboard, only one cookie can be selected. If you need more
|
|
295
|
+
cookies you can inject additional cookies globally in `Config.extraHeaders`.
|
|
281
296
|
|
|
282
297
|
By the way, there’s a `jwtCookie` helper, which has a hardcoded header and
|
|
283
298
|
signature. In other words, it’s useful if you only care about its payload.
|
|
@@ -295,6 +310,7 @@ Config.cookies = {
|
|
|
295
310
|
}
|
|
296
311
|
```
|
|
297
312
|
|
|
313
|
+
|
|
298
314
|
### `extraHeaders?: string[]`
|
|
299
315
|
Note it’s a unidimensional array. The header name goes at even indices.
|
|
300
316
|
|
|
@@ -306,6 +322,7 @@ Config.extraHeaders = [
|
|
|
306
322
|
]
|
|
307
323
|
```
|
|
308
324
|
|
|
325
|
+
|
|
309
326
|
### `extraMimes?: { [fileExt: string]: string }`
|
|
310
327
|
```js
|
|
311
328
|
Config.extraMimes = {
|
|
@@ -313,6 +330,7 @@ Config.extraMimes = {
|
|
|
313
330
|
}
|
|
314
331
|
```
|
|
315
332
|
|
|
333
|
+
|
|
316
334
|
### `plugins?: [filenameTester: RegExp, plugin: Plugin][]`
|
|
317
335
|
```ts
|
|
318
336
|
type Plugin = (
|
|
@@ -328,8 +346,9 @@ Plugins are for processing mocks before sending them.
|
|
|
328
346
|
|
|
329
347
|
Note: don’t call `response.end()`
|
|
330
348
|
|
|
349
|
+
<details>
|
|
350
|
+
<summary><b> Plugin Examples </b></summary>
|
|
331
351
|
|
|
332
|
-
#### Plugin Examples
|
|
333
352
|
```shell
|
|
334
353
|
npm install yaml
|
|
335
354
|
```
|
|
@@ -358,6 +377,7 @@ function capitalizePlugin(filePath) {
|
|
|
358
377
|
}
|
|
359
378
|
}
|
|
360
379
|
```
|
|
380
|
+
</details>
|
|
361
381
|
|
|
362
382
|
|
|
363
383
|
### `corsAllowed?: boolean`
|
|
@@ -373,11 +393,11 @@ Config.corsMaxAge = 0 // seconds to cache the preflight req
|
|
|
373
393
|
Config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
374
394
|
```
|
|
375
395
|
|
|
396
|
+
|
|
376
397
|
### `onReady?: (dashboardUrl: string) => void`
|
|
377
|
-
This defaults to trying to open the dashboard
|
|
378
|
-
|
|
398
|
+
This defaults to trying to open the dashboard in your default browser in
|
|
399
|
+
macOS and Windows. If you don’t want to open a browser, pass a noop, such as
|
|
379
400
|
|
|
380
|
-
If you don’t want to open a browser, pass a noop, such as
|
|
381
401
|
```js
|
|
382
402
|
Config.onReady = () => {}
|
|
383
403
|
```
|
package/package.json
CHANGED