No configuration needed: Just start it, and test your code!
Mock REST Server comes with these features 🚀 :
- Support GET, POST, PUT, PATCH, DELETE and OPTIONS HTTP methods
- Accept and respond with JSON
- Support real HTTP status code response from REST API
- Return HTTP error status codes via api version
- Fake latency
- Filtering, sorting and pagination
- Auto populate tool to fill database, don't waste your time for a REST server!
All details of changes to this project will be documented in this file.
npm i -D mock-rest-server
node_modules/.bin/mock-rest-server
Param name | Description | Type | Default |
---|---|---|---|
--port=3000 |
(Optional) Change server port | Number |
3000 |
--silent |
(Optional) Hide server output | Boolean |
false |
--latency |
(Optional) wait before response | Number |
0 |
For more details, look at the full example from test file.
Assuming you're using a module-compatible system (like webpack), start MockRestServer on top of your unit test file:
import MockRestServer from 'mock-rest-server'
describe('MockRestServer', function () {
it('Start server', async () => {
const server = await MockRestServer.start(3000, true, 0)
server.populate('articles', 30, {
title: String,
body: String,
userId: Number,
created: Date,
private: Boolean
})
// ...
// ...
// ...
// don't forget to stop!
// If you work with `--watch` param that reload your unit test, don't forget to stop server at the end of your tests.
// It'll try to launch server again when refresh, and port will already in use.
await server.stop()
})
})
MockRestServer come with (optional) awesome feature that fill database with random typed data: populate(collection, length, schema)
Param name | Description | Type |
---|---|---|
collection |
Collection name to create/populate | String |
length |
Number of resource to create in collection | Number |
schema |
Object send to create a resource. Values are replaced by random value based on JS type String , Number , Date or Boolean . |
Object |
Start MockRestServer, open a new shell and run some curl on api /v1/
:
curl -X POST -d '{"title":"Awesome news!","body":"Some content."}' http://localhost:3000/v1/articles
# {"title":"Awesome news!","body":"Some content.","id":1}
Now, get your articles with:
curl http://localhost:3000/v1/articles
# [{"title":"Awesome news!","body":"Some content.","id":1},{"title":"Awesome news!","body":"Some content.","id":2}]
curl http://localhost:3000/v1/articles/1
# {"title":"Awesome news!","body":"Some content.","id":1}
Use api /v[xxx]/
to mock HTTP status codes (403, 404, 500...) from server response:
curl http://localhost:3000/v403/articles
Any help or feedback are really welcome, no matter how great or small!
Please make sure to read the Contributing Guide before making a pull request.