mockaton 8.2.9 → 8.2.11
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 +45 -51
- 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. If you indicate your backend address in
|
|
13
|
+
`Config.proxyFallback` Mockaton will request from it the 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,22 +50,19 @@ 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
|
-
- Click `index.html` in the "Static" section
|
|
66
|
-
- see what for in the "Deterministic Standalone Demo Server" use case below
|
|
67
66
|
|
|
68
67
|
Finally, edit a mock file in your IDE. You don’t need to restart Mockaton for that.
|
|
69
68
|
The _Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
@@ -75,39 +74,33 @@ The _Reset_ button is for registering newly added, removed, or renamed mocks.
|
|
|
75
74
|
- Spinners by delaying responses
|
|
76
75
|
- Errors such as _Bad Request_ and _Internal Server Error_
|
|
77
76
|
- Setting up UI tests
|
|
78
|
-
- Polled resources (
|
|
77
|
+
- Polled resources (for triggering their different states)
|
|
79
78
|
- alerts
|
|
80
79
|
- notifications
|
|
81
80
|
- slow to build assets
|
|
82
81
|
|
|
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
82
|
### Time Travel
|
|
92
83
|
If you commit the mocks in the repo, when bisecting a bug, you don’t
|
|
93
84
|
have to sync the frontend with many backend repos. Similarly, it
|
|
94
|
-
allows for checking out long-lived branches
|
|
85
|
+
allows for checking out long-lived branches with old API contracts.
|
|
95
86
|
|
|
96
87
|
### Deterministic Standalone Demo Server
|
|
97
88
|
Perhaps you need to demo your app, but the ideal flow is too complex to
|
|
98
89
|
simulate from the actual backend. In this case, compile your frontend app and
|
|
99
90
|
put its built assets in `Config.staticDir`. Then, from the Mockaton dashboard
|
|
100
91
|
you can "Bulk Select" mocks to simulate the complete states you want to demo.
|
|
101
|
-
|
|
102
92
|
For bulk-selecting, you just need to add a comment to the mock
|
|
103
|
-
filename
|
|
104
|
-
"Comments" section under the "Filename Convention" for details.
|
|
93
|
+
filename, such as `(demo-part1)`, `(demo-part2)`.
|
|
105
94
|
|
|
106
95
|
|
|
107
96
|
## Motivation
|
|
108
97
|
- Avoids spinning up and maintaining hefty backends when developing UIs.
|
|
109
98
|
- For a deterministic and comprehensive backend state. For example, having all the possible
|
|
110
99
|
state variants of a collection helps for spotting inadvertent bugs.
|
|
100
|
+
- Sometimes, frontend progress is blocked waiting for some backend API. Similarly,
|
|
101
|
+
it’s often delayed due to missing data or inconvenient contracts. Therefore,
|
|
102
|
+
many meetings can be saved by prototyping frontend features with mocks, and
|
|
103
|
+
then showing those contracts to the backend team.
|
|
111
104
|
|
|
112
105
|
## Alternatives
|
|
113
106
|
- Chrome DevTools allows for [overriding responses](https://developer.chrome.com/docs/devtools/overrides)
|
|
@@ -116,9 +109,9 @@ filename. For example, `(demo-part1)`, `(demo-part2)`. See the
|
|
|
116
109
|
|
|
117
110
|
---
|
|
118
111
|
|
|
119
|
-
## Mock Variants
|
|
112
|
+
## Multiple Mock Variants
|
|
120
113
|
Each route can have many mocks, which could either be:
|
|
121
|
-
- Different response __status code__. For example, for
|
|
114
|
+
- Different response __status code__. For example, for triggering errors.
|
|
122
115
|
- __Comment__ on the filename, which is anything within parentheses.
|
|
123
116
|
- e.g. `api/login(locked out user).POST.423.json`
|
|
124
117
|
|
|
@@ -199,7 +192,7 @@ Put it in your `Config.staticDir` without the mock filename convention.
|
|
|
199
192
|
|
|
200
193
|
---
|
|
201
194
|
|
|
202
|
-
## Mock
|
|
195
|
+
## Mock Filename Convention
|
|
203
196
|
|
|
204
197
|
### Extension
|
|
205
198
|
|
|
@@ -231,11 +224,9 @@ api/foo.GET.200.json
|
|
|
231
224
|
api/video<b>?limit=[limit]</b>.GET.200.json
|
|
232
225
|
</pre>
|
|
233
226
|
|
|
234
|
-
The query string is ignored when routing to it. In other words, it’s
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
Speaking of which, in Windows, filenames containing "?" are [not
|
|
238
|
-
permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
227
|
+
The query string is ignored when routing to it. In other words, it’s only used for
|
|
228
|
+
documenting the URL contract. Speaking of which, in Windows, filenames containing "?" are
|
|
229
|
+
[not permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
239
230
|
but since that’s part of the query string, it’s ignored anyway.
|
|
240
231
|
|
|
241
232
|
|
|
@@ -259,9 +250,11 @@ api/foo/.GET.200.json
|
|
|
259
250
|
### `mocksDir: string`
|
|
260
251
|
This is the only required field
|
|
261
252
|
|
|
253
|
+
|
|
262
254
|
### `host?: string`
|
|
263
255
|
Defaults to `'localhost'`
|
|
264
256
|
|
|
257
|
+
|
|
265
258
|
### `port?: number`
|
|
266
259
|
Defaults to `0`, which means auto-assigned
|
|
267
260
|
|
|
@@ -271,9 +264,8 @@ Defaults to `/(\.DS_Store|~)$/`
|
|
|
271
264
|
|
|
272
265
|
|
|
273
266
|
### `delay?: number` 🕓
|
|
274
|
-
The clock icon next to the mock selector is a checkbox for delaying a particular
|
|
275
|
-
|
|
276
|
-
The delay is globally configurable via `Config.delay = 1200` (milliseconds).
|
|
267
|
+
The clock icon next to the mock selector is a checkbox for delaying a particular
|
|
268
|
+
response. The delay is globally configurable via `Config.delay = 1200` (milliseconds).
|
|
277
269
|
|
|
278
270
|
|
|
279
271
|
### `proxyFallback?: string`
|
|
@@ -284,25 +276,22 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
|
|
|
284
276
|
### `staticDir?: string`
|
|
285
277
|
Files under `Config.staticDir` don’t use the filename convention.
|
|
286
278
|
Also, they take precedence over the `GET` mocks in `Config.mocksDir`.
|
|
287
|
-
|
|
288
279
|
For example, if you have two files for `GET /foo/bar.jpg`
|
|
289
280
|
```
|
|
290
281
|
my-static-dir/foo/bar.jpg
|
|
291
282
|
my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
|
|
292
283
|
```
|
|
293
284
|
|
|
294
|
-
Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
295
|
-
|
|
296
|
-
Use Case 2: For a standalone demo server. For example,
|
|
285
|
+
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
286
|
+
- Use Case 2: For a standalone demo server. For example,
|
|
297
287
|
build your frontend bundle, and serve it from Mockaton.
|
|
298
288
|
|
|
299
289
|
|
|
300
290
|
### `cookies?: { [label: string]: string }`
|
|
301
291
|
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
302
|
-
|
|
303
292
|
The key is just a label used for selecting a particular cookie in the
|
|
304
|
-
dashboard. In the dashboard, only one cookie can be selected. If you need
|
|
305
|
-
|
|
293
|
+
dashboard. In the dashboard, only one cookie can be selected. If you need more
|
|
294
|
+
cookies you can inject additional cookies globally in `Config.extraHeaders`.
|
|
306
295
|
|
|
307
296
|
By the way, there’s a `jwtCookie` helper, which has a hardcoded header and
|
|
308
297
|
signature. In other words, it’s useful if you only care about its payload.
|
|
@@ -320,6 +309,7 @@ Config.cookies = {
|
|
|
320
309
|
}
|
|
321
310
|
```
|
|
322
311
|
|
|
312
|
+
|
|
323
313
|
### `extraHeaders?: string[]`
|
|
324
314
|
Note it’s a unidimensional array. The header name goes at even indices.
|
|
325
315
|
|
|
@@ -331,6 +321,7 @@ Config.extraHeaders = [
|
|
|
331
321
|
]
|
|
332
322
|
```
|
|
333
323
|
|
|
324
|
+
|
|
334
325
|
### `extraMimes?: { [fileExt: string]: string }`
|
|
335
326
|
```js
|
|
336
327
|
Config.extraMimes = {
|
|
@@ -338,6 +329,7 @@ Config.extraMimes = {
|
|
|
338
329
|
}
|
|
339
330
|
```
|
|
340
331
|
|
|
332
|
+
|
|
341
333
|
### `plugins?: [filenameTester: RegExp, plugin: Plugin][]`
|
|
342
334
|
```ts
|
|
343
335
|
type Plugin = (
|
|
@@ -353,8 +345,9 @@ Plugins are for processing mocks before sending them.
|
|
|
353
345
|
|
|
354
346
|
Note: don’t call `response.end()`
|
|
355
347
|
|
|
348
|
+
<details>
|
|
349
|
+
<summary><b> Plugin Examples </b></summary>
|
|
356
350
|
|
|
357
|
-
#### Plugin Examples
|
|
358
351
|
```shell
|
|
359
352
|
npm install yaml
|
|
360
353
|
```
|
|
@@ -383,6 +376,7 @@ function capitalizePlugin(filePath) {
|
|
|
383
376
|
}
|
|
384
377
|
}
|
|
385
378
|
```
|
|
379
|
+
</details>
|
|
386
380
|
|
|
387
381
|
|
|
388
382
|
### `corsAllowed?: boolean`
|
|
@@ -398,11 +392,11 @@ Config.corsMaxAge = 0 // seconds to cache the preflight req
|
|
|
398
392
|
Config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
399
393
|
```
|
|
400
394
|
|
|
395
|
+
|
|
401
396
|
### `onReady?: (dashboardUrl: string) => void`
|
|
402
|
-
This defaults to trying to open the dashboard
|
|
403
|
-
|
|
397
|
+
This defaults to trying to open the dashboard in your default browser in
|
|
398
|
+
macOS and Windows. If you don’t want to open a browser, pass a noop, such as
|
|
404
399
|
|
|
405
|
-
If you don’t want to open a browser, pass a noop, such as
|
|
406
400
|
```js
|
|
407
401
|
Config.onReady = () => {}
|
|
408
402
|
```
|
package/package.json
CHANGED