Manual Reference Source Test
import {Server} from 'fetch-mock-fixtures/src/Server.js'
public class | version 1.0.0 | since 1.0.0 | source

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

on: Fixture: *

This getter is used when configuring a fixture in-the-fly.

version 1.0.0 since 2.0.0
public get

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

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

stub: Object: *

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(fixtures: Fixture | Object | Array): Server

Import a fixture into the server pool.

version 1.0.0 since 2.0.0
public

preset(name: String, preset: Object): Preset

Returns the selected preset or a new one based on name resolution.

version 1.0.0 since 2.0.0
public

reset(resetStub: Boolean): Server

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 window.fetch

version 2.0.0 since 1.0.0
public

stop(resetServer: Boolean): Server

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

verbose(verbose: Boolean): Server

Set the verbose behavior of the server

version 1.0.0 since 2.1.0
public

warn(error: String | Error)

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

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 constructor() version 2.0.0 since 1.0.0 source

Import the default presets into server

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

Return:

Number

Number of requests received

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

Return:

Fixture

Fallback fixture

See:

public history: ServerHistory since 2.0.0 source

Store the server history

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

Return:

Fixture

New Fixture

public get request: FMFRequest: * version 1.0.0 since 2.0.0 source

Returns the last request received by the server

Return:

FMFRequest

See:

public get respond: Fixture: * version 1.0.0 since 2.0.0 source

Getter used when configuring fixture on-the-fly

Return:

Fixture

Return either the default fixture or set the current to respond mode

See:

public get response: FMFRequest: * version 1.0.0 since 2.0.0 source

Returns the last response received by the server

Return:

FMFRequest

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

Return:

Boolean

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

Return:

Object

Sinon stub

public get when: Fixture: * version 1.0.0 since 2.0.0 source

Alias for Server#on

Return:

Fixture

New fixture

Private Members

private _fixtures: Array since 2.0.0 source

Store the fixtures loaded into the server or created on-the-fly

See:

private _presets: Object since 2.0.0 source

Store the loaded presets and those created on-the-fly

private _throwOnError: Boolean since 2.0.0 source

Store wether FMF shoud throw or send a 500 HTTP response when an error is raised

See:

private _verbose: Boolean source

Store if server should output events to console

private _warnOnError: Boolean since 2.0.0 source

Store wether FMF shoud display a warning message in console when an error is raised

See:

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

Params:

NameTypeAttributeDescription
fixtures Fixture | Object | Array

Fixture(s) to import

Return:

Server

Server instance

Throw:

FMFException

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

Params:

NameTypeAttributeDescription
name String

Preset name

preset Object
  • optional
  • default: {}

Preset content

Return:

Preset

See:

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:

NameTypeAttributeDescription
resetStub Boolean
  • optional
  • default: true

If true, the stub history will also be resetted

Return:

Server

Server instance

public start(): Server version 2.0.0 since 1.0.0 source

Start the server by stubbing window.fetch

Return:

Server

Server instance

public stop(resetServer: Boolean): Server version 2.0.0 since 1.0.0 source

Stop the server and, optionnally reset it

Params:

NameTypeAttributeDescription
resetServer Boolean
  • optional
  • default: false

If true, stop will also reset server (see Server#reset)

Return:

Server

Server instance

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:

NameTypeAttributeDescription
throwOnError Boolean

If true server will throw

Return:

Server

Server instance

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:

NameTypeAttributeDescription
verbose Boolean

If true turn the verbose mode on

Return:

Server

Server instance

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

Params:

NameTypeAttributeDescription
error String | Error

Error description

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:

NameTypeAttributeDescription
warnOnError Boolean

true will display warnings

Return:

Server

Server instance

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:

NameTypeAttributeDescription
request FMFRequest

Request

Return:

Promise

Resolved in fixture instance

Throw:

FMFException

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

Return:

Fixture

Fallback fixture

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

Params:

NameTypeAttributeDescription
request String | Request

Incoming request

init Object
  • optional

request options

Return:

Promise

Response

Throw:

FMFException

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:

NameTypeAttributeDescription
fixture Object
  • optional
  • default: {}

Calling fixture or void object if not called from a fixture

Return:

Fixture

Return either the default fixture or set the current to respond mode