mockaton 8.12.8 → 8.12.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 +52 -44
- package/package.json +2 -3
- package/src/Commander.js +1 -1
- package/src/Dashboard.css +1 -0
package/README.md
CHANGED
|
@@ -2,16 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
+
[](https://github.com/ericfortis/mockaton/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/ericfortis/mockaton/actions/workflows/github-code-scanning/codeql)
|
|
5
7
|
|
|
6
8
|
An HTTP mock server for simulating APIs with minimal setup
|
|
7
9
|
— ideal for triggering difficult to reproduce backend states.
|
|
8
10
|
|
|
9
11
|
|
|
10
12
|
## Overview
|
|
11
|
-
With Mockaton, you don’t need to write code for wiring up your
|
|
12
|
-
given directory is scanned for filenames
|
|
13
|
+
With Mockaton, you don’t need to write code for wiring up your
|
|
14
|
+
mocks. Instead, a given directory is scanned for filenames
|
|
15
|
+
following a convention similar to the URLs.
|
|
16
|
+
|
|
17
|
+
For example, for [/api/user/123](#), the mock filename could be:
|
|
13
18
|
|
|
14
|
-
For example, for <code>/<b>api/user</b>/1234</code> the filename would be:
|
|
15
19
|
<pre>
|
|
16
20
|
<code>my-mocks-dir/<b>api/user</b>/[user-id].GET.200.json</code>
|
|
17
21
|
</pre>
|
|
@@ -20,15 +24,14 @@ For example, for <code>/<b>api/user</b>/1234</code> the filename would be:
|
|
|
20
24
|
## Dashboard
|
|
21
25
|
|
|
22
26
|
On the dashboard you can select a mock variant for a particular route, delaying responses,
|
|
23
|
-
or triggering an autogenerated `500`
|
|
24
|
-
|
|
27
|
+
or triggering an autogenerated `500` error, among other features.
|
|
25
28
|
Nonetheless, there’s a programmatic API, which is handy
|
|
26
29
|
for setting up tests (see **Commander API** section).
|
|
27
30
|
|
|
28
31
|
<picture>
|
|
29
|
-
<source media="(prefers-color-scheme: light)" srcset="
|
|
30
|
-
<source media="(prefers-color-scheme: dark)" srcset="
|
|
31
|
-
<img alt="Mockaton Dashboard" src="
|
|
32
|
+
<source media="(prefers-color-scheme: light)" srcset="pixaton-tests/macos/pic-for-readme.vp840x800.light.gold.png">
|
|
33
|
+
<source media="(prefers-color-scheme: dark)" srcset="pixaton-tests/macos/pic-for-readme.vp840x800.dark.gold.png">
|
|
34
|
+
<img alt="Mockaton Dashboard" src="pixaton-tests/macos/pic-for-readme.vp840x800.light.gold.png">
|
|
32
35
|
</picture>
|
|
33
36
|
|
|
34
37
|
|
|
@@ -60,12 +63,12 @@ api/videos.GET.<b>500</b>.txt # Internal Server Error
|
|
|
60
63
|
<br/>
|
|
61
64
|
|
|
62
65
|
## Fallback to Your Backend
|
|
63
|
-
No need to mock everything.
|
|
66
|
+
No need to mock everything. You can forward requests to your backend for routes
|
|
64
67
|
you don’t have mocks for, or routes that have the ☁️ **Cloud Checkbox** checked.
|
|
65
68
|
|
|
66
69
|
|
|
67
70
|
### Scraping mocks from your backend
|
|
68
|
-
|
|
71
|
+
By checking ✅ **Save Mocks**, you can collect the responses that hit your backend.
|
|
69
72
|
They will be saved in your `config.mocksDir` following the filename convention.
|
|
70
73
|
|
|
71
74
|
|
|
@@ -73,12 +76,10 @@ They will be saved in your `config.mocksDir` following the filename convention.
|
|
|
73
76
|
|
|
74
77
|
|
|
75
78
|
## Basic Usage
|
|
76
|
-
Mockaton is a Node.js program
|
|
77
|
-
|
|
78
|
-
`tsx` is only needed if you want to write mocks in TypeScript.
|
|
79
|
+
Mockaton is a Node.js program.
|
|
79
80
|
|
|
80
81
|
```sh
|
|
81
|
-
npm install mockaton
|
|
82
|
+
npm install mockaton --save-dev
|
|
82
83
|
```
|
|
83
84
|
|
|
84
85
|
Create a `my-mockaton.js` file
|
|
@@ -94,7 +95,7 @@ Mockaton({
|
|
|
94
95
|
```
|
|
95
96
|
|
|
96
97
|
```sh
|
|
97
|
-
node
|
|
98
|
+
node my-mockaton.js
|
|
98
99
|
```
|
|
99
100
|
|
|
100
101
|
<br/>
|
|
@@ -111,15 +112,16 @@ cd mockaton/demo-app-vite
|
|
|
111
112
|
npm install
|
|
112
113
|
|
|
113
114
|
npm run mockaton
|
|
114
|
-
npm run start
|
|
115
|
-
|
|
116
|
-
#
|
|
115
|
+
npm run start # in another terminal
|
|
116
|
+
|
|
117
|
+
# BTW, that directory has scripts for running both
|
|
118
|
+
# servers with one command in two terminals.
|
|
117
119
|
```
|
|
118
120
|
|
|
119
121
|
|
|
120
122
|
The app looks like this:
|
|
121
123
|
|
|
122
|
-
<img src="./demo-app-vite/pixaton-tests/pic-for-readme.
|
|
124
|
+
<img src="./demo-app-vite/pixaton-tests/pic-for-readme.vp740x880.light.gold.png" alt="Mockaton Demo App Screenshot" width="740" />
|
|
123
125
|
|
|
124
126
|
<br/>
|
|
125
127
|
|
|
@@ -167,7 +169,7 @@ For example, `api/foo.GET.200.js`
|
|
|
167
169
|
**Option A:** An Object, Array, or String is sent as JSON.
|
|
168
170
|
|
|
169
171
|
```js
|
|
170
|
-
export default
|
|
172
|
+
export default { foo: 'bar' }
|
|
171
173
|
```
|
|
172
174
|
|
|
173
175
|
**Option B:** Function
|
|
@@ -205,7 +207,7 @@ export default async function insertColor(request, response) {
|
|
|
205
207
|
}
|
|
206
208
|
```
|
|
207
209
|
|
|
208
|
-
`api/colors
|
|
210
|
+
`api/colors.GET.200.js`
|
|
209
211
|
```js
|
|
210
212
|
import colorsFixture from './colors.json' with { type: 'json' }
|
|
211
213
|
|
|
@@ -364,17 +366,16 @@ or that you manually picked with the ☁️ **Cloud Checkbox**.
|
|
|
364
366
|
|
|
365
367
|
### `collectProxied?: boolean`
|
|
366
368
|
Defaults to `false`. With this flag you can save mocks that hit
|
|
367
|
-
your proxy fallback to `config.mocksDir`. If
|
|
368
|
-
|
|
369
|
+
your proxy fallback to `config.mocksDir`. If the URL has v4 UUIDs,
|
|
370
|
+
the filename will have `[id]` in their place. For example:
|
|
369
371
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
my-mocks-dir
|
|
373
|
-
|
|
372
|
+
<pre>
|
|
373
|
+
<b>/api/user/</b>d14e09c8-d970-4b07-be42-b2f4ee22f0a6<b>/likes</b> =>
|
|
374
|
+
my-mocks-dir<b>/api/user/</b>[id]<b>/likes</b>.GET.200.json
|
|
375
|
+
</pre>
|
|
374
376
|
|
|
375
|
-
Your existing mocks won’t be overwritten.
|
|
376
|
-
|
|
377
|
-
a unique filename comment.
|
|
377
|
+
Your existing mocks won’t be overwritten. In other words, responses of routes with
|
|
378
|
+
the ☁️ **Cloud Checkbox** selected will be saved with unique filename-comments.
|
|
378
379
|
|
|
379
380
|
|
|
380
381
|
<details>
|
|
@@ -386,14 +387,14 @@ header was not sent by your backend.
|
|
|
386
387
|
|
|
387
388
|
<p>
|
|
388
389
|
An <code>.unknown</code> extension means the <code>Content-Type</code> is not in
|
|
389
|
-
|
|
390
|
+
the predefined list. For that, you can add it to <code>config.extraMimes</code>
|
|
390
391
|
</p>
|
|
391
392
|
</details>
|
|
392
393
|
|
|
393
394
|
|
|
394
395
|
### `formatCollectedJSON?: boolean`
|
|
395
|
-
Defaults to `true`. Saves the mock with
|
|
396
|
-
of `JSON.stringify(data, null, ' ')`
|
|
396
|
+
Defaults to `true`. Saves the mock with two spaces indentation —
|
|
397
|
+
the formatting output of `JSON.stringify(data, null, ' ')`
|
|
397
398
|
|
|
398
399
|
|
|
399
400
|
<br/>
|
|
@@ -410,14 +411,16 @@ config.cookies = {
|
|
|
410
411
|
'My JWT': jwtCookie('my-cookie', {
|
|
411
412
|
name: 'John Doe',
|
|
412
413
|
picture: 'https://cdn.auth0.com/avatars/jd.png'
|
|
413
|
-
})
|
|
414
|
+
}),
|
|
415
|
+
'None': ''
|
|
414
416
|
}
|
|
415
417
|
```
|
|
416
|
-
The selected cookie, which is the first one by default, is sent in every
|
|
417
|
-
|
|
418
|
+
The selected cookie, which is the first one by default, is sent in every response in a
|
|
419
|
+
`Set-Cookie` header (as long as its value is not an empty string). The object key is just
|
|
420
|
+
a label for UI display purposes, and also for selecting a cookie via the Commander API.
|
|
418
421
|
|
|
419
|
-
If you need to send more
|
|
420
|
-
in `config.extraHeaders`, or in function `.js` or `.ts` mock.
|
|
422
|
+
If you need to send more than one cookie, you can inject them globally
|
|
423
|
+
in `config.extraHeaders`, or individually in a function `.js` or `.ts` mock.
|
|
421
424
|
|
|
422
425
|
By the way, the `jwtCookie` helper has a hardcoded header and signature.
|
|
423
426
|
In other words, it’s useful only if you care about its payload.
|
|
@@ -478,8 +481,8 @@ import { jsToJsonPlugin } from 'mockaton'
|
|
|
478
481
|
|
|
479
482
|
config.plugins = [
|
|
480
483
|
|
|
481
|
-
// Although `jsToJsonPlugin` is set by default, you need to
|
|
482
|
-
//
|
|
484
|
+
// Although `jsToJsonPlugin` is set by default, you need to include it if you need it.
|
|
485
|
+
// IOW, your plugins array overwrites the default list. This way you can remove it.
|
|
483
486
|
[/\.(js|ts)$/, jsToJsonPlugin],
|
|
484
487
|
|
|
485
488
|
[/\.yml$/, yamlToJsonPlugin],
|
|
@@ -506,6 +509,10 @@ function capitalizePlugin(filePath) {
|
|
|
506
509
|
|
|
507
510
|
### `corsAllowed?: boolean`
|
|
508
511
|
Defaults to `true`. When `true`, these are the default options:
|
|
512
|
+
|
|
513
|
+
<details>
|
|
514
|
+
<summary>CORS Options</summary>
|
|
515
|
+
|
|
509
516
|
```js
|
|
510
517
|
config.corsOrigins = ['*']
|
|
511
518
|
config.corsMethods = require('node:http').METHODS
|
|
@@ -514,13 +521,14 @@ config.corsCredentials = true
|
|
|
514
521
|
config.corsMaxAge = 0 // seconds to cache the preflight req
|
|
515
522
|
config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
516
523
|
```
|
|
524
|
+
</details>
|
|
517
525
|
|
|
518
526
|
<br/>
|
|
519
527
|
|
|
520
528
|
### `onReady?: (dashboardUrl: string) => void`
|
|
521
529
|
By default, it will open the dashboard in your default browser on macOS and
|
|
522
530
|
Windows. But for a more cross-platform utility you could `npm install open` and
|
|
523
|
-
|
|
531
|
+
that implementation will be automatically used instead.
|
|
524
532
|
|
|
525
533
|
If you don’t want to open a browser, pass a noop:
|
|
526
534
|
```js
|
|
@@ -552,9 +560,9 @@ await mockaton.select('api/foo.200.GET.json')
|
|
|
552
560
|
```js
|
|
553
561
|
await mockaton.bulkSelectByComment('(demo-a)')
|
|
554
562
|
```
|
|
555
|
-
Parentheses are optional, so you can pass a partial match.
|
|
556
|
-
|
|
557
|
-
first mock in alphabetical order
|
|
563
|
+
Parentheses are optional, so you can pass a partial match. For example,
|
|
564
|
+
passing `'demo-'` (without the final `a`) works too. On routes
|
|
565
|
+
with many partial matches, their first mock in alphabetical order wins.
|
|
558
566
|
|
|
559
567
|
<br/>
|
|
560
568
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "mockaton",
|
|
3
3
|
"description": "HTTP Mock Server",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.12.
|
|
5
|
+
"version": "8.12.10",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "index.d.ts",
|
|
8
8
|
"license": "MIT",
|
|
@@ -19,13 +19,12 @@
|
|
|
19
19
|
"test": "node --test \"src/**/*.test.js\"",
|
|
20
20
|
"coverage": "node --test --test-reporter=lcov --test-reporter-destination=.coverage/lcov.info --experimental-test-coverage \"src/**/*.test.js\"",
|
|
21
21
|
"start": "node dev-mockaton.js",
|
|
22
|
-
"start:ts": "node --import=tsx dev-mockaton.js",
|
|
23
22
|
"pixaton": "node --test --import=./pixaton-tests/_setup.js --experimental-test-isolation=none \"pixaton-tests/**/*.test.js\"",
|
|
24
23
|
"outdated": "npm outdated --parseable | awk -F: '{ printf \"npm i %-30s ;# %s\\n\", $4, $2 }'"
|
|
25
24
|
},
|
|
26
25
|
"optionalDependencies": {
|
|
27
26
|
"open": "^10.0.0",
|
|
28
|
-
"pixaton": ">=1.
|
|
27
|
+
"pixaton": ">=1.1.1",
|
|
29
28
|
"puppeteer": ">=24.1.1"
|
|
30
29
|
}
|
|
31
30
|
}
|
package/src/Commander.js
CHANGED