Server version of the popular data-mocks library
npm install data-mocks-serverThis 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.
- Data Mocks Server
- Table of contents
- Installation
- Example usage
- API
- run
- default
- scenarios
- options
- Types
- Mock
- HttpMock
- Response
- HttpResponseFunction
- GraphQlMock
- Operation
- GraphQlResponse
- GraphQlResponseFunction
- Override
```
npm install data-mocks-server
`javascript
const { run } = require('data-mocks-server');
run({
default: [
{
url: '/api/test-me',
method: 'GET',
response: { data: { blue: 'yoyo' } },
},
],
scenarios: {
cheese: [
{
url: '/api/test-me',
method: 'GET',
response: { data: { 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' }.
Returns the internal express instance
> function({ default, scenarios, options })
Returns an http server, with an additional kill method
> function({ default, scenarios, options })
#### default
> Array | _required_
| Property | Type | Default | Description |
| -------- | ------------- | ----------- | ------------------------------------- |
| context | object | undefined | Used to set up data across API calls. |Array
| mocks | | _required_ | See Mock for more details. |
#### scenarios
> { [scenarioName]: Array
| Property | Type | Default | Description |
| ------------ | ------------- | ----------- | -------------------------------------------------------------------------------------- |
| scenarioName | string | _required_ | Name of scenario. |Mock
| Mock | | _required_ | See Mock for more details. |string
| group | | undefined | Used to group scenarios together so that only one scenario in a group can be selected. |object
| context | | undefined | Used to set up data across API calls. |Array
| mocks | | _required_ | See Mock for more details. |
#### options
> { port, uiPath, modifyScenariosPath, resetScenariosPath, scenariosPath } | defaults to {}
| Property | Type | Default | Description |
| ------------------- | --------- | ------------------- | ------------------------------------------------------------------------------------------ |
| port | number | 3000 | Port that the http server runs on. |string
| uiPath | | / | Path that the UI will load on. http://localhost:{port}{uiPath} |string
| modifyScenariosPath | | /modify-scenarios | API path for modifying scenarios. http://localhost:{port}{modifyScenariosPath} |string
| resetScenariosPath | | /reset-scenarios | API path for resetting scenarios. http://localhost:{port}{resetScenariosPath} |string
| scenariosPath | | /scenarios | API path for getting scenarios. http://localhost:{port}{scenariosPath} |boolean
| cookieMode | | false | Whether or not to store scenario selections in a cookie rather than directly in the server |
> HttpMock | GraphQlMock
See HttpMock and GraphQlMock for more details.
> { url, method, response }
| Property | Type | Default | Description |
| -------- | ----------------------------------------------------- | ----------- | --------------------------------------------------------------------- |
| url | string / RegExp | _required_ | Path of endpoint. Must start with /. |'GET'
| method | / 'POST' / 'PUT' / 'DELETE' / 'PATCH' | _required_ | HTTP method of endpoint. |undefined
| response | / Response / HttpResponseFunction | undefined | Response, HttpResponseFunction. |
> { status, headers, data, delay }
| Property | Type | Default | Description |
| -------- | ---------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| status | number | 200 | HTTP status code for response. |object
| headers | / 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. |null
| data | / string / object | undefined | Response data |number
| delay | | 0 | Number of milliseconds before the response is returned. |
> function({ query, body, params, context, updateContext }): response | Promise
| Property | Type | Default | Description |
| ------------- | ------------------------ | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| query | object | {} | query object as defined by express. |object
| body | | {} | body object as defined by express. |object
| params | | {} | params object as defined by express. |object
| context | | {} | Data stored across API calls. |Function
| updateContext | | partialContext => updatedContext | Used to update context. partialContext can either be an object or a function (context => partialContext). |undefined
| response | / Response | _required_ | Response. |
> { url, method, operations }
| Property | Type | Default | Description |
| ---------- | ------------------ | ---------- | -------------------------------------------------------------------------------------- |
| url | string | _required_ | Path of endpoint. |'GRAPHQL'
| method | | _required_ | Indentifies this mock as a GraphQlMock. |Array
| operations | | _required_ | List of operations for GraphQL endpoint. See Operation for more details. |
#### Operation
> { type, name, response }
| Property | Type | Default | Description |
| -------- | ----------------------------------------------------------- | ----------- | ----------------------------------------------------------------------------------------- |
| type | 'query' / 'mutation' | _required_ | Type of operation. |string
| name | | _required_ | Name of operation. |undefined
| response | / GraphQlResponse / GraphQlResponseFunction | undefined | GraphQlResponse, GraphQlResponseFunction. |
> { status, headers, data, delay }
| Property | Type | Default | Description |
| -------- | ------------------------------------------ | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| status | number | 200 | HTTP status code for response. |object
| headers | / 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. |{ data?: null / object, errors?: array }
| data | | undefined | Response data |number
| delay | | 0 | Number of milliseconds before the response is returned. |
> function({ variables, context, updateContext }): response | Promise
| Property | Type | Default | Description |
| ------------- | ------------------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| variables | object | {} | variables sent by client. |object
| context | | {} | Data stored across API calls. |Function
| updateContext | | partialContext => updatedContext | Used to update context. partialContext can either be an object or a function (context => partialContext). |undefined
| response | / GraphQlResponse | _required_ | GraphQlResponse. |
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, given response can be a function, it is possible to respond with a different value for the status, headers, data and delay properties:
`javascript
const mock = {
url: '/some-url',
method: 'GET',
response: ({ body }) => {
if (body.name === 'error1') {
return {
status: 400,
data: { message: 'something went wrong' },
delay: 1000,
};
}
if (body.name === 'error2') {
return {
status: 500,
data: { message: 'something else went wrong' },
delay: 2000,
};
}
if (body.name === 'notFound') {
return {
status: 404,
data: { message: 'no data here' },
};
}
// Default status is 200
return { data: { message: 'success' } };
},
};
``