Skip to main content

Installation

Ng-apimock is essentially a set of npm packages that can be installed over npm.

Requirements

  • Node.js version >= 10.15.1 or above (which can be checked by running node -v). You can use nvm for managing multiple Node versions on a single machine installed
  • Yarn version >= 1.5 (which can be checked by running yarn version). Yarn is a performant package manager for JavaScript and replaces the npm client. It is not strictly necessary but highly encouraged. To add the standard WireMock JAR as a project dependency, put the following in the dependencies section of your build file:

Installing using npm / yarn

npm install @ng-apimock/core --save-dev

or

yarn add @ng-apimock/core --dev

Usage

Once the plugin has been installed, you can require it with this line of JavaScript:

const apimock = require('@ng-apimock/core');

Processor

The next step is to tell @ng-apimock/core where it can find the mocks and / or presets. You can do that by calling the processor.

apimock.processor.process({
src: 'mocks', // required
patterns: { // optional
mocks: '**/*Mock.json', // optional: default is '**/*.mock.json'
presets: '**/*Preset.json', // optional: default is '**/*.preset.json'
},
watches: { // optional
mocks: '**/*.json', // optional: no default, set if watch files regex is different from mocks pattern
presets: '**/*.json' // optional: no default, set if watch files regex is different from presets pattern
},
watch: true // optional: default is 'false'
});

There are 4 parameters here:

  • src: this is the directory that will be used to search for mocks and presets.
  • patterns: there are 2 regex patterns that can be overridden, mocks and presets.
  • watches: set these if the patterns differ from the files to watch. Typically needed when using js instead of json.
  • watch: set to true will ensure that ng-apimock will watch for file changes.
caution

As a side-effect, when a mock or preset change has been detected, the saved state will be reset.

Middleware

The final step to take is to register @ng-apimock/core as middleware. It is compatible with both Connect and Express

const connect = require('connect');
const app = connect();

app.use(apimock.middleware);

or

const express = require('express');
const app = express();

app.use(apimock.middleware);

Middleware body limit

The default bodyParser library that is used has a body limit is 100kb. In order to increase the limit you can set the limit like this:

app.use(bodyParser.json({limit: '10mb'}));

Middleware configuration

You can configure apimock with a configuration object.

apimock.configure({
middleware:{
useHeader: true, // optional: indicator to use a header instead of a cookie to provide the identifier. (defaults to false)
identifier: 'my-identifier' // optional: name of the header or cookie that is used as the identifier. (defaults to 'apimockid')
}
});
important

The cookie or header is used to make sure that parallel executed tests do not interfere with each other. Each session is fully isolated.

Minimal setup example

This is a minimal setup example of how you can manually use @ng-apimock/core

const apimock = require('@ng-apimock/core');
const express = require('express');
const app = express();
app.set('port', 9999);

apimock.processor.process({
src: 'mocks'
});

app.use(apimock.middleware);

app.listen(app.get('port'), () => {
console.log('@ng-apimock/core running on port', app.get('port'));
});

To start up the script just type:

node serve.js

Endpoints

There are a few endpoints available when you startup @ng-apimock/core:

  • /info - responsible for providing information of the running instance
  • /health - responsible for providing status information
  • /health/readiness - readiness probe
  • /health/liveness - liveness probe