Mock requests made to URLs that match the provided regex. For more information about using this step, see our guide on request mocking.

Inputs

URL pattern
string
required
A regex used to determine which requests to intercept. The regex is evaluated against the full, original URL of the request including params.
Response
string
required
Javascript code used to generate a response for the intercepted requests. This code must return a Javascript Response object. This code behaves similarly to Momentic’s standard javascript step with a couple of exceptions:
  • Environment variables can be read at env.<VARIABLE_NAME>, however, they cannot be updated using setVariable. This is to prevent potential race conditions.
  • The request itself is accessible at mock.request. It contains the following fields:
    • url: string
    • method: GET | POST | PUT | PATCH | DELETE | OPTIONS
    • headers: Record<string, string>
    • json: unknown | undefined (set if the content-type is application/json)
    • text: string | undefined (set if the content-type is not application/json)
  • If the ‘fetch real response’ config is enabled, the original response is accessible at mock.response. It contains the following fields:
    • status: number
    • headers: Record<string, string>
    • json: unknown | undefined (set if the content-type is application/json)
    • text: string | undefined (set if the content-type is not application/json)

Configs

Fetch real response
boolean
Complete the original request, and pass its response into the generator as mock.response. Enabling this option is helpful if you want to modify the original response rather than fully replacing it. You should not enable this option if the request will fail (for example if the host is inaccessible), as it will cause the mock to fail.
Key
string
A key that can be used to reference the mock later. This should be set if you want to remove the mock later in the test. If not set, a random key will be generated.