Skip to main content
The JavaScript client works in both Node.js and the browser. It communicates with a running MockServer instance over HTTP and returns Promises for every operation.

Installation

To also start MockServer from Node.js, install mockserver-node:

Creating a client

Node.js

Browser

Include the browser bundle directly in your HTML, then call mockServerClient as a global:

Connecting over TLS

Pass true as the fourth argument to enable HTTPS:

Creating expectations

Use .mockAnyResponse(expectation) to register an expectation. The method returns a Promise.

Simple shorthand

Use .mockSimpleResponse(path, responseBody, statusCode) for a quick GET-only mock:

Limit how many times an expectation fires

Include a times object to cap the number of matches:

Set expectation priority

Use a priority field to control which expectations match first. Higher values match first; negative values act as catch-all defaults:

Verifying requests

Verify by count

.verify(requestMatcher, atLeast, atMost) checks how many times a request was received.

Verify a sequence

Use .verifySequence() to assert that a set of requests was received in a specific order:

Retrieving recorded requests

Retrieve all recorded requests that match a filter:
Retrieve recorded request-response pairs:
Retrieve log messages:

Clearing and resetting state

Clear matching state

Remove expectations and recorded requests that match a pattern:
Clear only the log, leaving expectations active:

Reset all state

Remove all expectations and the request log:

Starting MockServer from Node.js

Use the mockserver-node package to start and stop MockServer programmatically, without a separate process.

Basic start and stop

With JVM options and version pin

MockServer requires Java to be installed and available on PATH. The mockserver-node package downloads the MockServer JAR automatically.

Grunt plugin

If your project uses Grunt, mockserver-node doubles as a Grunt plugin. Add start_mockserver and stop_mockserver tasks to your Gruntfile.js:
Set trace: true (or pass --verbose on the CLI) to enable INFO-level logging. MockServer only records requests in its log at INFO level or above, so this setting is required if you plan to use the /retrieve endpoint or call .retrieveRecordedRequests().