Skip to content

hone-labs/mock-api

Repository files navigation

mock-rest-api

Creates a mock REST API from JSON files.

Used to mock dependencies of frontends and backends for testing.

The dataset that is returned by the mock API is called an API fixture and is the same sort of concept as a database fixture.

Usage

You need Node.js installed to use mock-api.

Global usage

Install mock-api globally:

npm install --global @optio-labs/mock-api

Change directory to the project for your mock API.

Run the mock-api server:

mock-api

Local usage

Change directory to the Node.js project for your mock API.

Install mock-api in your project:

npm install --save @optio-labs/mock-api

Run the mock-api server:

mock-api

Creating API fixtures

Create API fixtures under the fixtures directory in your project.

Each subdirectory under fixtures is a named fixture, for example:

  • fixtures
    • first-fixture
      • ...
    • second-fixture
      • ...
    • etc...

Under each fixture are nested subdirectories that create routes in the mock API, for example:

  • first-fixture
    • nested-route
      • deeper-route

Under each route directory create a file response.json to create a mock JSON response for that route. for example:

first-fixture/nested-route/deeper-route/response.json:

{
    "msg": "Hello world!"
}

Loading fixtures

A named fixture is loaded by invoking the mock-api route load-fixture.

You can invoke it your browser, for example to load first-fixture open a browser tab and open http://localhost:3000/load-fixture?name=first-fixture.

Alternatively you can use Postman or VS Code REST Client to invoke that route.

Or for the purposes of automated testing (say, using Jest) you could use something like Axios to invoke the route. That means you can make a helper function like the following to load fixtures before running tests:

import axios from "axios";

//
// Loads a named API fixture.
//
export function loadFixture(fixtureName: string): Promise<void> {
    await axios.get(`http://localhost:3000/load-fixture?name=${fixtureName}`);
}

Command line arguments

Usage:

npx mock-api [options]

Options:

  • --port=<port-number> - Sets the port number used by the mock API's HTTP server.
  • --fixture=<fixture-name> - Loads a default fixture on start up.

Development

Clone this repo.

Install dependencies:

cd mock-api
pnpm install

Run in development mode:

npm run start:dev

Run tests:

npm test

Run in prod mode:

npm run build
npm start

About

Creates a mock REST API from JSON files.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published