DEV Community

糊涂人
糊涂人

Posted on

An alternative API mocking library for frontend developers

What is it?

http-request-mock is an HTTP request mocking library that lets you develop, build and test as normal even when backend APIs are down or not ready yet. It supplies a new way to prototype your web application.

How does it work?

It mocks http requests issued by axios, jquery, superagent, node-fetch, got, (… you name it) by intercepting XMLHttpRequest, fetch, and nodejs native HTTP/HTTPS module requests at the low level.

How is it different?

It differs from the other mocking libraries in that it provides a webpack plugin and command-line tool to separate mock data from your business code. It’s a truly non-hacking mocking library. You never have to hack into your business code to mock something ever again after a one-time configuration.

Features

  • Business-code-unaware: Does not interfere with code writing. Keep your code unaware of whether something is mocked or not.
  • Interceptor: It can be used as an interceptor. You can decide how to handle requests.
  • Reusable: The same mock case can be reused in unit tests, CI integration, or even debugging.
  • Remote-mock: Support for using remote mock data, where you can dynamically modify the data returned from the remote.
  • Fake data: http-request-mock has integrated with @ngneat/falso, you can easily generate massive amounts of fake data.

Full feature list:

Usage example

To mock an http request, just call a mock method or http verb method(get,post,put,patch,delete).

Image description

In a bare-bones example, you just import http-request-mock into your application entry file(such as src/main.js) and configure your mock data there. Take a Vue project as an example:

Image description

Project Integration

It may have lots of APIs to be mocked in a large web application. You may need frequently change the entry file when adding/deleting/updating mock data. There will be a day that you’ll get a mess as the project grows. So, we provide a webpack plugin and command tool to integrate your project. In this way, the mock data file can be separated from the entry to reduce the burden of managing this entry file.

After a one-time configuration for integration, you can define your mock data(mock case) like below.

Image description

All the requests matched by the mock case above will be intercepted by http-request-mock.

Image description

Unit Test

http-request-mock comes with built-in unit test capability and can be used in jest and mocha environments.

Image description

Also, you can reuse a mock case like the one below, you can get the completed code by this link.

Image description

Fake Data

http-request-mock has integrated with @ngneat/falso. You can use it to generate massive amounts of fake data.

The data below is generated by the mock case above.

{
    "id": 11,
    "name": "Vijay Iglesias",
    "age": 49,
    "phone": "(557) 203-2412",
    "gender": "female",
    "avatar": "https://i.pravatar.cc/100"
}
Enter fullscreen mode Exit fullscreen mode

Remote mock data

http-request-mock supports using remote mock data, where you can dynamically modify the data returned from the remote.

Image description

When you request "https://www.api.com/remote2", the request of "https://jsonplaceholder.typicode.com/todos/1" will be sent out and returned to the remote object. Now, you can dynamically modify the data returned from the remote.

For more details, please refer to this link.

Data-change-cache

http-request-mock supports data-change-cache. You can use it to achieve a closed-loop of product development without backend dependencies:

Image description

Check out this link for a CURD demo without backend dependencies.

Examples

Top comments (0)