New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@optio-labs/mock-api

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@optio-labs/mock-api

Creates a mock REST API from JSON files.

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

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

FAQs

Package last updated on 15 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc