node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

@springworks/static-api-server

This package uses semantic release! Use correct semantic commit messages.

Static API Server

Greenkeeper badge

Useful to mimic behavior of an actual API server, but with static fixture responses instead.

Usage

Install dependency:

npm install -D @springworks/static-api-server

Send requests to API and get matching fixtures back:

 
import static_api_server from '@springworks/static-api-server';
import { createFixtureLoader } from 'fixture-loader';
 
const fixture_loader = createFixtureLoader('./path-to-fixtures');
static_api_server.createServer({
    swagger_spec: swagger_spec,
    fixture_loader: fixture_loader,
    host: 'localhost',
    port: 3001,
    output_validation: false,
  })
  .then(created_server => {
    created_server.start().then(() => {
      // Send requests to server, which will respond with successful status code and fixture
      // created_server.stop() when done
    });
  });
 

API

createServer({ swagger_spec, fixture_loader, host, port, output_validation })

output_validation is optional and defaults to off. When enabled, all responses are validated against the API specification using hapi-sov.

Creates and configures a Hapi server. Routes defined in swagger_spec will be matched with fixtures from fixture_loader.

Returns a Hapi server instance.

server.replaceFixtures(verb, route, fixtures)

Gives you the ability to adjust the mocks for certain calls. fixtures is an array of fixtures, where each element in the array corresponds to the call count of the invocation. Meaning, request number 1 will receive fixture at index 0, and so on. This is an instance method on the created server object. NOTE! Make sure to enable output_validation in the server options if using shadow fixtures! This ensures that your mocked fixtures validate against the swagger spec.

server.shadowFixtures(verb, route, fixtures)

Gives you the ability to adjust the mocks for certain calls. fixtures is an array of fixtures, where each element in the array corresponds to the call count of the invocation. This method uses lodash.merge for each fixture call. If you want to replace this fixture, please use replaceFixtures. Meaning, request number 1 will receive fixture at index 0, and so on. This is an instance method on the created server object. NOTE! Make sure to enable output_validation in the server options if using shadow fixtures! This ensures that your mocked fixtures validate against the swagger spec.

server.purgeShadowFixtures()

Purge any existing shadow fixtures. Useful as an afterEach() during testing.

Notes

This module uses the semantic-release automated package publishing. Which means that the commit messages are required to look in a certain way.

To ensure this pattern is enforced, we use pre-git which installs a commit-msg hook. Please read and understand what the types mean and use them properly when committing and contributing.

The commit-msg hook needs to know about your installed node which means that if you use third party application when committing, like Tower, that application needs to know your PATH environment. There are probably a lot of ways to solve this, but one that works is to open the application from the terminal: open /Applications/Tower.app.

Contributing

  1. Write a new test with new fixtures or extend the existing ones
  2. Make a PR
  3. 🙏 👏