Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

JackuB/apish

Repository files navigation

Build Status Coveralls Status npm downloads Greenkeeper badge

apish - Mock APIs and Services

Describe any API or Service you need mocked. Supports API Blueprint and Swagger API Description formats.

Use cases

3rd party APIs

  • Your app is calling GitHub API, weather API, Trello API, …
  • Describe endpoints in API Description format like API Blueprint (or ask API provider for API Description)
  • Add it as a test fixture and let apish create mock for you:
before(() => {
  return apish(fs.readFileSync('github-api.apib', 'utf8'));
});

// Run your tests with mocked requests against GitHub API

(Micro)services

  • Have all your services publish API Description onto (private) npm. Use semver to version it and tools like Dredd to test its implementation
  • When you depend on another service, just require its package with API Description and run tests against its mocks, that are always in sync with implementation
  • You can always compare version you've tested against to what is currently running in your environment
import myOtherService from 'myOtherService';

before(() => apish(myOtherService));

// Run your tests…

Installation

$ npm i apish -D

Usage

import apish from 'apish';

// In your test runner
let mockResult = {};
before(() => {
  const apib = fs.readFileSync('github-api.apib', 'utf8');
  return mockResult = apish(apib); // apish returns a Promise
});

// Cleanup
after(() => {
  // .value() is Promise-related helper in this case
  mockResult.value().restore();
});

Arguments

const mockedApi = apish(apiDescription, options);
  • apiDescription (string) - API Blueprint or Swagger API Description
  • options (OPTIONAL, object)
    • host (string) - overwrite specified host (base URL) that should be used

returns Promise

Resolved promise returns object with methods:

  • restore() - clears all mocks for this host

License

MIT