Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

wdio-wiremock-service

erwinheitzman4.7kMIT7.0.2TypeScript support: included

A WebdriverIO service to start & stop WireMock Standalone

webdriver, webdriverio, wdio, wdio-service, wiremock, standalone, server, mock, proxy, stub, tests

readme

WebdriverIO WireMock Service

npm version downloads

Join the chat at https://gitter.im/erwinheitzman/wdio-wiremock-service

This service helps you to run WireMock seamlessly when running tests with WebdriverIO. It uses the well known Maven repository to download the WireMock jar for you which is then automatically installed, started and stopped. Stay up to date by joining the community over at Gitter for help and support.

Installation

npm i -D wdio-wiremock-service

Instructions on how to install WebdriverIO can be found here.

Usage

In the root directory (default ./mock) you find two subdirectories, __files and mappings which are used for your fixtures and mocks.

For more information, checkout WireMock's official documentation.

Configuration

In order to use the service with the wdio testrunner you need to add it to your service array:

// wdio.conf.js
export.config = {
  // ...
  services: ['wiremock'],
  // ...
};

When using webdriverio standalone you need to add the service and trigger the onPrepare and onComplete hooks manually. An example can be found here (the example makes use of Jest):

Options

The following options can be added to the service.

port

Port where WireMock should run on.

Type: Number

Default: 8080

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { port: 8181 }]],
    // ...
};

rootDir

Path where WireMock will look for files.

Type: String

Default: ./mock

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { rootDir: './mock' }]],
    // ...
};

version

Version of WireMock to be downloaded and used.

Type: String

Default: 3.3.1

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { version: '2.25.1' }]],
    // ...
};

skipWiremockInstall

Tell the service to skip downloading WireMock.

Type: Boolean

Default: false

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { skipWiremockInstall: true }]],
    // ...
};

binPath

Custom path to a local Wiremock binary (often used in combination with skipWiremockInstall).

Type: String

Default: './wiremock-standalone-3.0.0.jar' (relative from service)

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { binPath: './my-custom/example-path/wiremock-standalone-3.0.0.jar' }]],
    // ...
};

silent

Silent mode for logging WireMock's output (including additional logging from the service itself).

Type: Boolean

Default: false

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { silent: true }]],
    // ...
};

mavenBaseUrl

Base download url for Maven.

Type: String

Default: https://repo1.maven.org/maven2

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [['wiremock', { mavenBaseUrl: 'https://repo1.maven.org/maven2' }]],
    // ...
};

args

List where you can pass all the supported arguments for configuring WireMock

Note: you cannot pass the options (port, rootDir, stdio, mavenBaseUrl) here as they will be ignored.

Type: Array

Example:

// wdio.conf.js
export const config = {
    // ...
    services: [
        [
            'wiremock',
            {
                args: ['--verbose', '--match-headers'],
            },
        ],
    ],
    // ...
};

Writing tests

Writing your first test is really straight forward:

Using the WDIO testrunner

import fetch from 'node-fetch'; // you can use any HTTP client you like
import { equal } from 'node:assert'; // you can use any assertion library you like

describe('example', () => {
    it(`should assert the mock data`, async () => {
        const body = await fetch('http://localhost:8080/api/mytest');
        equal(body.text(), 'Hello world!');
    });
});

Using WebdriverIO Standalone

import fetch from 'node-fetch'; // you can use any HTTP client you like
import { equal } from 'node:assert'; // you can use any assertion library you like
import { remote } from 'webdriverio';
import { launcher } from 'wdio-wiremock-service';

const WDIO_OPTIONS = {
    capabilities: {
        browserName: 'chrome',
    },
};

describe('example', () => {
    let wiremockLauncher;
    let client;

    beforeAll(async () => {
        wiremockLauncher = new launcher(); // create instance of the service
        await wiremockLauncher.onPrepare(WDIO_OPTIONS); // run the onPrepare hook
        client = await remote(WDIO_OPTIONS);
    });

    afterAll(async () => {
        await client.deleteSession();
        await wiremockLauncher.onComplete(); // run the onComplete hook
    });

    test('should showoff a mocked api response', async () => {
        const body = await fetch('http://localhost:8080/api/mytest');
        equal(body.text(), 'Hello world!');
    });
});

For more information on WebdriverIO see the homepage.

changelog

7.0.2

  • Fix types

7.0.1

  • Fix download url and download error message

7.0.0

  • Updated dependencies
  • Updated documentation
  • Added a downloadUrl option
  • changed the default Wiremock version to 3.3.1
  • changed the repository to download Wiremock from to GitHub instead of maven

6.0.7

  • Updated dependencies
  • Removed obsolete dependencies
  • Updated configurations
  • Changed Husky setup
  • Cleaned up project

6.0.6

  • fixed killing of process

6.0.5

  • Removed stdio option
  • Added silent mode
  • Updated dependencies

6.0.4

  • Fixed a minor issue with some parameter types
  • Updated dependencies

6.0.3

  • Split logic
  • Added more tests
  • Updated dependencies

6.0.2

  • Fixed Unrecognized option error when assigning arguments

6.0.0 - 6.0.1

  • Updated the service to be optimised for WebdriverIO v6
  • Fixed non-graceful shutdown when in watch mode

5.0.0 - 5.0.2

Had to re-release because of the missing lib directory on publish. Will investigate in order to get the release process setup automatically.

Breaking changes

  • Changed the default rootDir value from mock to wiremock.\ Fix: simply rename the directory to wiremock or add the rootDir property to the options object and pass the value mock to keep the directory as it is.

Notable changes

  • Rewrote the service in TypeScript
  • Added tests
  • Added linting
  • Added styling
  • Added type declarations to the output
  • Fixed arguments assignment issue
  • Added WebdriverIO 5.X.X as a peer dependency
  • Changed version to 5.0.0 as this service will add support for both WebdriverIO 5 and soon WebdriverIO 6

2.26.4

  • Fixed options assignments

2.26.3

  • Fixed port assignment issue
  • Fixed issue where the arguments weren't passed correctly
  • Updated default version to 2.26.3

2.26.2

  • Added support for passing a different version

2.26.1

  • Added examples using the HTTP API to the docs

2.26.0

  • Updated to WireMock 2.26.0

2.25.1-8

  • Added support for relative path for rootDir
  • Added auto creation of the rootDir when it does not exist
  • Fixed typo in README.md

2.25.1-7

  • Added a default object to the options parameter for configuring the service
  • Updated readme with examples, fixed typo

2.25.1-6

  • Fixed logging on exit using wdio as standalone

2.25.1-5

  • Fix port not being set correctly

2.25.1-4

  • Fixed bug where a reference was incorrect

2.25.1-3

  • Added args support
  • Bug fixes
  • Correctly added dependency tcp-port-used

2.25.1-2

  • Added options support
  • Updated readme
  • Cleaned up code

2.25.1-1

  • First working version of the service