This server can mock the responses of an API with files on disk. Most likely to be used in automatic testing or in early development of an API-depending project.
$ npm install mocked-api --save-dev
To get things running (to use it in your test-suite for example), initialize it like this:
;const api = MockedApi;apistartconsole);
You can do this once (like in a script that's running your test-suite). The server will keep running until that process is killed, so you don't have to setup the server for every single test.
Place JSON-files in the configured directory to accomodate the responses. You can use nested directories to simulate a path-hierarchy. For example, the file at
./mocks/content/article/42.json will be served at
http://localhost:3000/content/article/42.json for the configuration above. If your API prohibits you from adding an extension, either create a file with or without an extension and it will be resolved.
Once initialized, you can mutate responses with the following methods:
;api// Defines the path you're about to change// Replace a property in your JSON-file. This is based on JSON-pointers, described in ().// Custom status-code (`200`, `404`, `500`, etc)api// Callback fired for every request, handy for debugging// Removes _all_ custom mutations
You can use these methods to make small changes in a response and test your UI for every little variation that you make. This way your tests can be small and specific, and still cover a lot of edge-cases.
The following example is based on mocha/chai/jsdom, but you can use it similarly in other environments:
You can run multiple instances of MockedApi simultaneously. For that, pass it a name when you setup each:
;const userApi = MockedApi;const blogApi = MockedApi;
Anytime you need one of those API's to override, make sure you use that same name:
;const userApi = MockedApi;;
You can also run the server standalone, from your CLI:
$ node_modules/.bin/mocked-api --port 6000 --dir ./mocks
This will serve JSON-files in
localhost:6000, but that's it. Custom mutation of responses is not possible through the CLI.