1 | # API
|
2 | A mocked version of your API, to control what data is returned for certain URL's and usecases. Based on [mocked-api](https://www.npmjs.com/package/mocked-api).
|
3 |
|
4 | ## Configuration
|
5 | To setup an API for GreenLight, add a `runAPI()` to your runner. Pass it an object as configuration, like so:
|
6 |
|
7 | ```js
|
8 | var GreenLight = require('green-light');
|
9 |
|
10 | GreenLight
|
11 | .init()
|
12 | .then(() => {
|
13 | return GreenLight.runAPI({
|
14 | port: 4000, // or whatever port is available to use
|
15 | dir: './test/mocks' // or wherever your mock-files are
|
16 | });
|
17 | });
|
18 | ```
|
19 |
|
20 | Option | Type | Description
|
21 | ------ | ---- | -----------
|
22 | `verbose` | `Boolean` | Enables extensive logging
|
23 | `port` | `Integer` | The port that the API will run on
|
24 | `dir` | `String` | The directory where your mocked data lives
|
25 |
|
26 | ## Usage
|
27 | In the example above, the API will be running on `http://localhost:4000` and will be looking for content in the `./test/mocks` directory. Add a json-file in the configured directory and open `http://localhost:4000/example.json` to validate that this is working as expected.
|
28 |
|
29 | You can use nested directories to simulate a path-hierarchy. For example, the file at `./test/mocks/content/article/42.json` will be served at `http://localhost:4000/content/article/42.json` for the configuration above.
|
30 |
|
31 | The configured API will be exposed by GreenLight, to mutate responses from within your tests:
|
32 |
|
33 | ```js
|
34 | import { api } from 'green-light';
|
35 | ```
|
36 |
|
37 | For more information about using the API, please refer to the documentation of [mocked-api](https://www.npmjs.com/package/mocked-api).
|
38 |
|
39 | ## Multiple API's
|
40 | [mocked-api](https://www.npmjs.com/package/mocked-api) also supports the use of "named API's". Configure them one by one, with an extra `name` property and a different port:
|
41 |
|
42 | ```js
|
43 | var GreenLight = require('green-light');
|
44 |
|
45 | GreenLight
|
46 | .init()
|
47 | .then(() => {
|
48 | return GreenLight.runAPI({
|
49 | name: 'content',
|
50 | port: 4000,
|
51 | dir: './test/mocks/content'
|
52 | });
|
53 | .then(() => {
|
54 | return GreenLight.runAPI({
|
55 | name: 'user',
|
56 | port: 4001,
|
57 | dir: './test/mocks/user'
|
58 | });
|
59 | });
|
60 | ```
|
61 |
|
62 | GreenLight will expose `getByName()` from [mocked-api](https://www.npmjs.com/package/mocked-api) to reference each API from within your tests:
|
63 |
|
64 | ```js
|
65 | import { API } from 'green-light';
|
66 | // ^^^ Watch the capitals here. We need the class, not the (default and unnamed) instance.
|
67 | const content = API.getByName('content');
|
68 | const userApi = API.getByName('user');
|
69 | ```
|
70 |
|
71 | ## Command line overrides
|
72 | You can override configuration through the CLI:
|
73 |
|
74 | Command | Description
|
75 | ------- | -------
|
76 | `--api-verbose` | Enables extensive logging
|
77 | `--api-port` | The port that the API will run on
|
78 | `--api-dir` | The directory where your mocked data lives
|
79 |
|
80 |
|
81 | [Read this](./command-line-overrides.md) for more information about command line overrides.
|