Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Mockttp

A mockttp instance allow you to start and stop mock servers and control their behaviour.

Call .start() to set up a server on a random port, use methods like .get(url), .post(url) and .anyRequest() to get a MockRuleBuilder and start defining mock rules. Call .stop() when your test is complete.

Hierarchy

  • Mockttp

Implemented by

Index

Properties

port

port: number

The port the server is running on.

This will throw an error if read before the server is started.

proxyEnv

proxyEnv: ProxyConfig

The environment variables typically needed to use this server as a proxy, in a format you can add to your environment straight away.

This will throw an error if read before the server is started.

process.env = Object.assign(process.env, mockServer.proxyEnv)

url

url: string

The root URL of the server.

This will throw an error if read before the server is started.

Methods

anyRequest

delete

enableDebug

  • enableDebug(): void
  • Enable extra debug output so you can understand exactly what the server is doing.

    Returns void

get

on

  • on(event: "request", callback: function): Promise<void>
  • Subscribe to hear about request details as they're received.

    This is only useful in some niche use cases, such as logging all requests seen by the server independently of the rules defined.

    The callback will be called asynchronously from request handling. This function returns a promise, and the callback is not guaranteed to be registered until the promise is resolved.

    Parameters

    Returns Promise<void>

options

  • Get a builder for a mock rule that will match OPTIONS requests for the given path.

    This can only be used if the cors option has been set to false.

    If cors is true (the default when using a remote client, e.g. in the browser), then the mock server automatically handles OPTIONS requests to ensure requests to the server are allowed by clients observing CORS rules.

    You can pass {cors: false} to getLocal/getRemote to disable this behaviour, but if you're testing in a browser you will need to ensure you mock all OPTIONS requests appropriately so that the browser allows your other requests to be sent.

    Parameters

    Returns MockRuleBuilder

patch

post

put

reset

  • reset(): void
  • Reset the stored rules. Most of the time it's better to start & stop the server instead, but this can be useful in some special cases.

    Returns void

start

  • start(port?: undefined | number): Promise<void>
  • Start a mock server.

    Specify a fixed port if you need one. If you don't, a random port will be chosen, which you can get later with .port, or by using .url and .urlFor(path) to generate your URLs automatically.

    Parameters

    • Optional port: undefined | number

    Returns Promise<void>

stop

  • stop(): Promise<void>
  • Stop the mock server and reset the rules.

    Returns Promise<void>

urlFor

  • urlFor(path: string): string
  • The URL for a given path on the server.

    This will throw an error if read before the server is started.

    Parameters

    • path: string

    Returns string

Generated using TypeDoc