Mocking a route
In this example, we’ll capture requests to our/todos
endpoint, and return
fake data instead of hitting the production API server.
First, we’ll create a new mock route step, and set the URL
matcher to a regex that will capture requests to GET /todos
.

Modifying the original response from the server
Sometimes, it’s easier to simply modify the original response from the server, for example, in order to set a specific feature flag. In order complete the initial request and pass the response to the mock generator, you must first enable the “fetch real response” option. This will allow us to access the response atmock.response
inside our JavaScript code.


Removing a mock
There are two ways to reference a mock in later steps:- By configuring a key in the mock route step.
- If no key is provided, a random one will be generated. You can still
reference it later by saving the output of the step to an environment
variable. The output of the mock route step will have a
key
field containing the random string.
Updating a mock
You can update a mock by registering a different handler with the same key using another mock route step. Registering a mock with the same URL matcher but a different key is not sufficient to replace the existing mock.Debugging mocked routes
Because network requests are handled asynchronously in a separate process, it can be hard to investigate when something goes wrong. If a response generator throws an error while handling a request, Momentic will still respond to the request with a 500 server error status code, and the error message in the body of the response. This makes it easy to figure out what’s going on either in your app’s UI, or using the network logs.