import {Server} from 'fetch-mock-fixtures/src/Server.js'
Server
Build a mock server to respond to any fetch calls. It replaces
window.fetch
with a Sinon stub. Therefore,
all functionnalities provided by stub are available
Note : All the server data is stored in the current instance. That may have unattended side effects when using the same instance through many test without resetting it each time
Constructor Summary
Public Constructor | ||
public |
Import the default presets into server |
version 2.0.0 since 1.0.0 |
Member Summary
Public Members | ||
public get |
Returs the number of calls made to server since start or last reset |
version 1.0.0 since 2.0.0 |
public get |
Returns the existing registered on the server or create and register a new fallback fixture to configure |
version 1.0.0 since 2.0.0 |
public |
Store the server history |
since 2.0.0 |
public get |
This getter is used when configuring a fixture in-the-fly. |
version 1.0.0 since 2.0.0 |
public get |
request: FMFRequest: * Returns the last request received by the server |
version 1.0.0 since 2.0.0 |
public get |
Getter used when configuring fixture on-the-fly |
version 1.0.0 since 2.0.0 |
public get |
response: FMFRequest: * Returns the last response received by the server |
version 1.0.0 since 2.0.0 |
public get |
Check if server is currently running by trying to access a stub property |
version 1.0.0 since 1.1.0 |
public get |
Exposes the underlying stub or throws error if server is not started |
version 1.0.0 since 1.1.0 |
public get |
Alias for Server#on |
version 1.0.0 since 2.0.0 |
Private Members | ||
private |
Store the fixtures loaded into the server or created on-the-fly |
since 2.0.0 |
private |
Store the loaded presets and those created on-the-fly |
since 2.0.0 |
private |
Store wether FMF shoud throw or send a 500 HTTP response when an error is raised |
since 2.0.0 |
private |
Store if server should output events to console |
|
private |
Store wether FMF shoud display a warning message in console when an error is raised |
since 2.0.0 |
Method Summary
Public Methods | ||
public |
Import a fixture into the server pool. |
version 1.0.0 since 2.0.0 |
public |
Returns the selected preset or a new one based on name resolution. |
version 1.0.0 since 2.0.0 |
public |
Reset the server configuration to default, clear server history and stub history |
version 2.0.0 since 1.0.0 |
public |
Start the server by stubbing |
version 2.0.0 since 1.0.0 |
public |
Stop the server and, optionnally reset it |
version 2.0.0 since 1.0.0 |
public |
throwOnError(throwOnError: Boolean): Server Set the behavior of the server when an Error is thrown. |
version 1.0.0 since 2.0.0 |
public |
Set the verbose behavior of the server |
version 1.0.0 since 2.1.0 |
public |
Displays a warning message in console. |
version 1.0.0 since 2.0.0 |
public |
warnOnError(warnOnError: Boolean): Server Tells the server to display a warning in console when an error is raised or when something seems to went wrong in configuration. |
version 1.0.0 since 2.0.0 |
Private Methods | ||
private |
async _findFixture(request: FMFRequest): Promise Seeks for matching fixtures when processing a request |
version 1.0.0 since 2.0.0 |
private |
Returns the existing registered on the server or create a new fallback fixture to configure |
version 1.0.0 since 2.0.0 |
private |
async _processRequest(request: String | Request, init: Object): Promise Process the incoming request and update history |
version 1.0.0 since 2.0.0 |
private |
_processRespond(fixture: Object): Fixture Process the respond call when called from a fixture to allow chainable fixtures on-the-fly configuration |
version 1.0.0 since 2.0.0 |
Public Constructors
Public Members
public get calls: Number: * version 1.0.0 since 2.0.0 source
Returs the number of calls made to server since start or last reset
public get fallback: Fixture: * version 1.0.0 since 2.0.0 source
Returns the existing registered on the server or create and register a new fallback fixture to configure
public get on: Fixture: * version 1.0.0 since 2.0.0 source
This getter is used when configuring a fixture in-the-fly. It will return
and register a new Fixture and set it to matching
mode
public get request: FMFRequest: * version 1.0.0 since 2.0.0 source
Returns the last request received by the server
See:
public get respond: Fixture: * version 1.0.0 since 2.0.0 source
Getter used when configuring fixture on-the-fly
public get response: FMFRequest: * version 1.0.0 since 2.0.0 source
Returns the last response received by the server
See:
public get running: Boolean: * version 1.0.0 since 1.1.0 source
Check if server is currently running by trying to access a stub property
public get stub: Object: * version 1.0.0 since 1.1.0 source
Exposes the underlying stub or throws error if server is not started
Private Members
private _fixtures: Array since 2.0.0 source
Store the fixtures loaded into the server or created on-the-fly
See:
private _throwOnError: Boolean since 2.0.0 source
Store wether FMF shoud throw or send a 500 HTTP response when an error is raised
Public Methods
public import(fixtures: Fixture | Object | Array): Server version 1.0.0 since 2.0.0 source
Import a fixture into the server pool. Fixture can be provided as a fixture instance or as a configuration object
Throw:
If fixture cannot be parsed |
public preset(name: String, preset: Object): Preset version 1.0.0 since 2.0.0 source
Returns the selected preset or a new one based on name resolution.
It allow a quick preset creation or edition that can be configured at once through the object provided within this call or with the classic ResponseConfigurator
public reset(resetStub: Boolean): Server version 2.0.0 since 1.0.0 source
Reset the server configuration to default, clear server history and stub history
Params:
Name | Type | Attribute | Description |
resetStub | Boolean |
|
If |
public stop(resetServer: Boolean): Server version 2.0.0 since 1.0.0 source
Stop the server and, optionnally reset it
Params:
Name | Type | Attribute | Description |
resetServer | Boolean |
|
If |
public throwOnError(throwOnError: Boolean): Server version 1.0.0 since 2.0.0 source
Set the behavior of the server when an Error is thrown. If set to true
, the server will
also throw the error at runtime. If set to false, it will respond with a 500 HTTP error
At default, the server is set to throw on error that will usually be the most suitable behavior when running tests to discard FMF failures.
note Only errors thrown during requests processing are affected by this parameter. Errors that occured on settings processing will always be raised
Params:
Name | Type | Attribute | Description |
throwOnError | Boolean | If |
See:
- Server#_onError
public verbose(verbose: Boolean): Server version 1.0.0 since 2.1.0 source
Set the verbose behavior of the server
Params:
Name | Type | Attribute | Description |
verbose | Boolean | If |
public warn(error: String | Error) version 1.0.0 since 2.0.0 source
Displays a warning message in console. It can be overridden to swap to another notification system
public warnOnError(warnOnError: Boolean): Server version 1.0.0 since 2.0.0 source
Tells the server to display a warning in console when an error is raised or when something seems to went wrong in configuration.
Default settings is true
Params:
Name | Type | Attribute | Description |
warnOnError | Boolean |
|
Private Methods
private async _findFixture(request: FMFRequest): Promise version 1.0.0 since 2.0.0 source
Seeks for matching fixtures when processing a request
An error will be raised if no fixtures have been set or if no matching fixtures have been found.
FMF will also send a warning to the console
Params:
Name | Type | Attribute | Description |
request | FMFRequest | Request |
Throw:
If no fixtures are defined or no matching fixtures found |
private _getDefaultFixture(): Fixture version 1.0.0 since 2.0.0 source
Returns the existing registered on the server or create a new fallback fixture to configure
private async _processRequest(request: String | Request, init: Object): Promise version 1.0.0 since 2.0.0 source
Process the incoming request and update history
Throw:
If request processing have failed |
private _processRespond(fixture: Object): Fixture version 1.0.0 since 2.0.0 source
Process the respond call when called from a fixture to allow chainable fixtures on-the-fly configuration
Params:
Name | Type | Attribute | Description |
fixture | Object |
|
Calling fixture or void object if not called from a fixture |