This package was originally a port of https://github.com/ovotech/data-mocks that prefers spinning up an express server instead of mocking out fetch
and XHR
operations. Thanks goes to grug for his idea and implementation.
npm install data-mocks-server
const { run } = require('data-mocks-server');
run({
default: [
{
url: '/api/test-me',
method: 'GET',
response: { blue: 'yoyo' },
},
],
scenarios: {
cheese: [
{
url: '/api/test-me',
method: 'GET',
response: { blue: 'cheese' },
},
],
},
});
Calls to http://localhost:3000/api/test-me
will start by returning { blue: 'yoyo' }
.
Visiting http://localhost:3000
will allow you to Modify scenarios
. The default response will always be included unless a scenario overrides it. In this case enabling cheese
will modify /api/test-me
so that it returns { blue: 'cheese' }
.
function({ default, scenarios, options })
Array<Mock> | { context, mocks }
| required
Property | Type | Default | Description |
---|---|---|---|
context | object |
undefined |
Used to set up data across API calls. |
mocks | Array<Mock> |
required | See Mock for more details. |
{ [scenarioName]: Array<Mock> | { group, mocks } }
Property | Type | Default | Description |
---|---|---|---|
scenarioName | string |
required | Name of scenario. |
Mock | Mock |
required | See Mock for more details. |
group | string |
undefined |
Used to group scenarios together so that only one scenario in a group can be selected. |
context | object |
undefined |
Used to set up data across API calls. |
mocks | Array<Mock> |
required | See Mock for more details. |
{ port, uiPath, modifyScenariosPath, resetScenariosPath }
| defaults to{}
Property | Type | Default | Description |
---|---|---|---|
port | number |
3000 |
Port that the http server runs on. |
uiPath | string |
/ |
Path that the UI will load on. http://localhost:{port}{uiPath} |
modifyScenariosPath | string |
/modify-scenarios |
API path for modifying scenarios. http://localhost:{port}{modifyScenariosPath} |
resetScenariosPath | string |
/reset-scenarios |
API path for resetting scenarios. http://localhost:{port}{resetScenariosPath} |
HttpMock | GraphQlMock
See HttpMock and GraphQlMock for more details.
{ url, method, response, responseCode, responseHeaders, responseDelay }
Property | Type | Default | Description |
---|---|---|---|
url | string / RegExp |
required | Path of endpoint. Must start with / . |
method | 'GET' / 'POST' / 'PUT' / 'DELETE' / 'PATCH' |
required | HTTP method of endpoint. |
response | undefined / Response / HttpResponseFunction |
undefined |
Response, HttpResponseFunction. |
responseCode | number |
200 |
HTTP status code for response. |
responseHeaders | object / undefined |
See description | Key/value pairs of HTTP headers for response. Defaults to undefined when response is undefined , adds 'Content-Type': 'application/json' when response is not undefined and Content-Type is not supplied. |
responseDelay | number |
0 |
Number of milliseconds before the response is returned. |
null
/string
/object
function({ query, body, params, context, updateContext }): response | Promise<response>
Property | Type | Default | Description |
---|---|---|---|
query | object |
{} |
query object as defined by express . |
body | object |
{} |
body object as defined by express . |
params | object |
{} |
params object as defined by express . |
context | object |
{} |
Data stored across API calls. |
updateContext | Function |
partialContext => updatedContext |
Used to update context. partialContext can either be an object or a function (context => partialContext ). |
response | undefined / Response / Override |
required | Response, Override. |
{ url, method, operations }
Property | Type | Default | Description |
---|---|---|---|
url | string |
required | Path of endpoint. |
method | 'GRAPHQL' |
required | Indentifies this mock as a GraphQlMock. |
operations | Array<Operation> |
required | List of operations for GraphQL endpoint. See Operation for more details. |
{ type, name, response, responseCode, responseHeaders, responseDelay }
Property | Type | Default | Description |
---|---|---|---|
type | 'query' / 'mutation' |
required | Type of operation. |
name | string |
required | Name of operation. |
response | undefined / Response / GraphQlResponse / GraphQlResponseFunction |
undefined |
Response, GraphQlResponse, GraphQlResponseFunction. |
responseCode | number |
200 |
HTTP status code for response. |
responseHeaders | object / undefined |
See description | Key/value pairs of HTTP headers for response. Defaults to undefined when response is undefined , adds 'Content-Type': 'application/json' when response is not undefined and Content-Type is not supplied. |
responseDelay | number |
0 |
Number of milliseconds before the response is returned. |
{ data?: null / object, errors?: array }
function({ variables, context, updateContext }): response | Promise<response>
Property | Type | Default | Description |
---|---|---|---|
variables | object |
{} |
variables sent by client. |
context | object |
{} |
Data stored across API calls. |
updateContext | Function |
partialContext => updatedContext |
Used to update context. partialContext can either be an object or a function (context => partialContext ). |
response | undefined / Response / GraphQlResponse / Override |
required | Response, GraphQlResponse, Override. |
{ __override: { response, responseCode, responseHeaders, responseDelay } }
Sometimes you may want an endpoint to respond with different status codes depending on what is sent. It is the recommendation of this package that this can be achieved by using scenarios. However, as an escape hatch you can override responseCode
, responseHeaders
and responseDelay
by using the __override
property:
const mock = {
url: '/some-url',
method: 'GET',
response: ({ body }) => {
if (body.name === 'error1') {
return {
__override: {
response: { message: 'something went wrong' },
responseCode: 400,
responseDelay: 1000,
},
};
}
if (body.name === 'error2') {
return {
__override: {
response: { message: 'something else went wrong' },
responseCode: 500,
responseDelay: 2000,
},
};
}
if (body.name === 'notFound') {
return {
__override: {
response: { message: 'no data here' },
responseCode: 404,
},
};
}
// No __override necessary, this is the response on success
return { message: 'success' };
},
}