Server control, configuration and history
Before being used, you must create a server instance :
import Server from 'fetch-mock-fixtures'
const server = new Server()
The constructor takes no arguments. Global presets are loaded when server instance is created. From server instance, you can start, stop and reset the server, configure presets and fixtures and access server history.
Server control
Method / Property | Description |
---|---|
start() |
Start the server by mocking native window.fetch with a Sinon stub |
stop(reset=false) |
Stop the server by restoring window.fetch . You can optionally pass true as argument to also reset the server |
reset(resetStub=true) |
Reset the server (clear all fixtures and history) and, optionnally reset the stub history |
running |
If true server is running |
stub |
Direct access to the sinon stub |
Adding fixtures to server
Adding fixtures to the server is pretty simple. See fixtures documentation.
Logging and verbose mode
Since v2.1.0
To help go through requests history analysis or to debug fixtures, FMF logs all events in history. You can access logs through server.history.logs
.
Logs can also be displayed at runtime to console with the verbose mode of the server. Simply call the chainable verbose
method on server instance.
import Server from 'fetch-mock-fixtures'
const server = new Server()
server.verbose(true) // enable verbose mode
server.verbose(false) // disable verbose mode
Error management
When encountering an error during configuration, the server will throw an error.
During request processing, the server will display a warning in console and send back a 500 response with error description. This behavior can be changed with :
warnOnError(true|false)
: Activate/deactivate warnings in consolethrowOnError(true|false)
: Iftrue
, tells the server to throw an error instead of sending back a 500 error.
Server's history
The server keeps track of all incoming requests and responses. As convenience, you can access the last request and response by calling server.request
or server.response
. For more advanced selection tools, the history is available under server.history
. See history tests for available tools.
The request is stored as a FMFRequest that also exposes parsed informations about the url using url-parse).