Installation
- npm
- yarn
mockserver-node:
Creating a client
Node.js
Browser
Include the browser bundle directly in your HTML, then callmockServerClient as a global:
Connecting over TLS
Passtrue 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 atimes object to cap the number of matches:
Set expectation priority
Use apriority 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:Clearing and resetting state
Clear matching state
Remove expectations and recorded requests that match a pattern:Reset all state
Remove all expectations and the request log:Starting MockServer from Node.js
Use themockserver-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: