npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-rest-server

v1.2.0

Published

MockRestServer is a lightweight (in memory) REST server for unit test.

Downloads

17

Readme

Mock REST Server

No configuration needed: Just start it, and test your code!

Mock REST Server comes with these features 🚀 :

  • [x] Support GET, POST, PUT, PATCH, DELETE and OPTIONS HTTP methods
  • [x] Accept and respond with JSON
  • [x] Support real HTTP status code response from REST API
  • [x] Return HTTP error status codes via api version
  • [x] Fake latency
  • [x] Filtering, sorting and pagination
  • [x] Auto populate tool to fill database, don't waste your time for a REST server!

Changes

All details of changes to this project will be documented in this file.

Installation & Usage

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 |

Unit test

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 |

Example

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

Contribution

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.

License

GPL-3.0