mockaton 8.2.9 → 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 +44 -49
- 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,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,30 +74,21 @@ 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
93
|
filename. For example, `(demo-part1)`, `(demo-part2)`. See the
|
|
104
94
|
"Comments" section under the "Filename Convention" for details.
|
|
@@ -108,6 +98,10 @@ filename. For example, `(demo-part1)`, `(demo-part2)`. See the
|
|
|
108
98
|
- Avoids spinning up and maintaining hefty backends when developing UIs.
|
|
109
99
|
- For a deterministic and comprehensive backend state. For example, having all the possible
|
|
110
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.
|
|
111
105
|
|
|
112
106
|
## Alternatives
|
|
113
107
|
- Chrome DevTools allows for [overriding responses](https://developer.chrome.com/docs/devtools/overrides)
|
|
@@ -116,9 +110,9 @@ filename. For example, `(demo-part1)`, `(demo-part2)`. See the
|
|
|
116
110
|
|
|
117
111
|
---
|
|
118
112
|
|
|
119
|
-
## Mock Variants
|
|
113
|
+
## Multiple Mock Variants
|
|
120
114
|
Each route can have many mocks, which could either be:
|
|
121
|
-
- Different response __status code__. For example, for
|
|
115
|
+
- Different response __status code__. For example, for triggering errors.
|
|
122
116
|
- __Comment__ on the filename, which is anything within parentheses.
|
|
123
117
|
- e.g. `api/login(locked out user).POST.423.json`
|
|
124
118
|
|
|
@@ -199,7 +193,7 @@ Put it in your `Config.staticDir` without the mock filename convention.
|
|
|
199
193
|
|
|
200
194
|
---
|
|
201
195
|
|
|
202
|
-
## Mock
|
|
196
|
+
## Mock Filename Convention
|
|
203
197
|
|
|
204
198
|
### Extension
|
|
205
199
|
|
|
@@ -231,11 +225,9 @@ api/foo.GET.200.json
|
|
|
231
225
|
api/video<b>?limit=[limit]</b>.GET.200.json
|
|
232
226
|
</pre>
|
|
233
227
|
|
|
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),
|
|
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),
|
|
239
231
|
but since that’s part of the query string, it’s ignored anyway.
|
|
240
232
|
|
|
241
233
|
|
|
@@ -259,9 +251,11 @@ api/foo/.GET.200.json
|
|
|
259
251
|
### `mocksDir: string`
|
|
260
252
|
This is the only required field
|
|
261
253
|
|
|
254
|
+
|
|
262
255
|
### `host?: string`
|
|
263
256
|
Defaults to `'localhost'`
|
|
264
257
|
|
|
258
|
+
|
|
265
259
|
### `port?: number`
|
|
266
260
|
Defaults to `0`, which means auto-assigned
|
|
267
261
|
|
|
@@ -271,9 +265,8 @@ Defaults to `/(\.DS_Store|~)$/`
|
|
|
271
265
|
|
|
272
266
|
|
|
273
267
|
### `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).
|
|
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).
|
|
277
270
|
|
|
278
271
|
|
|
279
272
|
### `proxyFallback?: string`
|
|
@@ -284,25 +277,22 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
|
|
|
284
277
|
### `staticDir?: string`
|
|
285
278
|
Files under `Config.staticDir` don’t use the filename convention.
|
|
286
279
|
Also, they take precedence over the `GET` mocks in `Config.mocksDir`.
|
|
287
|
-
|
|
288
280
|
For example, if you have two files for `GET /foo/bar.jpg`
|
|
289
281
|
```
|
|
290
282
|
my-static-dir/foo/bar.jpg
|
|
291
283
|
my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
|
|
292
284
|
```
|
|
293
285
|
|
|
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,
|
|
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,
|
|
297
288
|
build your frontend bundle, and serve it from Mockaton.
|
|
298
289
|
|
|
299
290
|
|
|
300
291
|
### `cookies?: { [label: string]: string }`
|
|
301
292
|
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
302
|
-
|
|
303
293
|
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
|
-
|
|
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`.
|
|
306
296
|
|
|
307
297
|
By the way, there’s a `jwtCookie` helper, which has a hardcoded header and
|
|
308
298
|
signature. In other words, it’s useful if you only care about its payload.
|
|
@@ -320,6 +310,7 @@ Config.cookies = {
|
|
|
320
310
|
}
|
|
321
311
|
```
|
|
322
312
|
|
|
313
|
+
|
|
323
314
|
### `extraHeaders?: string[]`
|
|
324
315
|
Note it’s a unidimensional array. The header name goes at even indices.
|
|
325
316
|
|
|
@@ -331,6 +322,7 @@ Config.extraHeaders = [
|
|
|
331
322
|
]
|
|
332
323
|
```
|
|
333
324
|
|
|
325
|
+
|
|
334
326
|
### `extraMimes?: { [fileExt: string]: string }`
|
|
335
327
|
```js
|
|
336
328
|
Config.extraMimes = {
|
|
@@ -338,6 +330,7 @@ Config.extraMimes = {
|
|
|
338
330
|
}
|
|
339
331
|
```
|
|
340
332
|
|
|
333
|
+
|
|
341
334
|
### `plugins?: [filenameTester: RegExp, plugin: Plugin][]`
|
|
342
335
|
```ts
|
|
343
336
|
type Plugin = (
|
|
@@ -353,8 +346,9 @@ Plugins are for processing mocks before sending them.
|
|
|
353
346
|
|
|
354
347
|
Note: don’t call `response.end()`
|
|
355
348
|
|
|
349
|
+
<details>
|
|
350
|
+
<summary><b> Plugin Examples </b></summary>
|
|
356
351
|
|
|
357
|
-
#### Plugin Examples
|
|
358
352
|
```shell
|
|
359
353
|
npm install yaml
|
|
360
354
|
```
|
|
@@ -383,6 +377,7 @@ function capitalizePlugin(filePath) {
|
|
|
383
377
|
}
|
|
384
378
|
}
|
|
385
379
|
```
|
|
380
|
+
</details>
|
|
386
381
|
|
|
387
382
|
|
|
388
383
|
### `corsAllowed?: boolean`
|
|
@@ -398,11 +393,11 @@ Config.corsMaxAge = 0 // seconds to cache the preflight req
|
|
|
398
393
|
Config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
399
394
|
```
|
|
400
395
|
|
|
396
|
+
|
|
401
397
|
### `onReady?: (dashboardUrl: string) => void`
|
|
402
|
-
This defaults to trying to open the dashboard
|
|
403
|
-
|
|
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
|
|
404
400
|
|
|
405
|
-
If you don’t want to open a browser, pass a noop, such as
|
|
406
401
|
```js
|
|
407
402
|
Config.onReady = () => {}
|
|
408
403
|
```
|
package/package.json
CHANGED