Skip to content

Commit

Permalink
feat: Add log to history and verbose mode
Browse files Browse the repository at this point in the history
See manual for more informations
  • Loading branch information
liqueurdetoile committed May 2, 2019
1 parent 3a0512c commit fb12396
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
16 changes: 16 additions & 0 deletions manual/pages/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ Method / Property | Description
## Adding fixtures to server
Adding fixtures to the server is pretty simple. See [fixtures](fixtures.html) 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.

```javascript
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.

Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const server = new Server();

// Define on-the-fly fixtures to handle login tests
server
.verbose(true) // Enable console login for each request/response/error
.when
.pathname.equal('/login')
.method.equal('POST')
Expand Down Expand Up @@ -104,7 +105,7 @@ We're not only sending back data to the app but also checking outgoing requests

Last not least, you can easily deploy url-based routing to use your "real" data inside each tests instead of providing fake data and get rid of on-the-fly fixtures (see [dynamic fixtures examples](https://liqueurdetoile.github.io/fetch-mock-fixtures/manual/dynamic-fixtures-examples)).

** When to use FMF ? **
**When to use FMF ?**

At any time :smile:

Expand Down
27 changes: 27 additions & 0 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class Server {
*/
_fixtures = [];

/**
* Store if server should output events to console
* @type {Boolean}
*/
_verbose = false;

/**
* Store wether FMF shoud throw or send a 500 HTTP response when an error is raised
* @type {Boolean}
Expand Down Expand Up @@ -117,6 +123,20 @@ export class Server {
return this;
}

/**
* Set the verbose behavior of the server
* @version 1.0.0
* @since 2.1.0
* @param {Boolean} verbose If `true` turn the verbose mode on
* @return {Server} Server instance
*/
verbose(verbose) {
this._verbose = !!verbose;
this.history._verbose = !!verbose;

return this;
}

/**
* Tells the server to display a warning in console when an error is raised or when
* something seems to went wrong in configuration.
Expand Down Expand Up @@ -388,6 +408,9 @@ export class Server {
// Build FMFRequest object
request = new FMFRequest(request, init);

// Log incoming request
this.history.log(`Request : ${request.method} ${request.url}`)

// Locate matching fixture
let fixture = await this._findFixture(request.clone());

Expand All @@ -397,8 +420,12 @@ export class Server {
// Store request in history
this.history.push(request.clone(), response.clone());

this.history.log(`Response sent (${response.status} ${response.statusText})`);

return response;
} catch (err) {
this.history.log(err.toString());

if (this._warnOnError) this.warn(err);
if (this._throwOnError) /* istanbul ignore next */ throw (err instanceof FMFException ? err : new FMFException('Request process failure', err));

Expand Down
9 changes: 9 additions & 0 deletions src/helpers/ServerHistory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export default class ServerHistory {
logs = [];
_history = [];
_call = null;
_target = null;
_verbose = false;

push(request, response) {
this._history.push({
Expand All @@ -10,6 +12,12 @@ export default class ServerHistory {
})
}

log(message) {
this.logs.push(message);

if (this._verbose) console.log(message); // eslint-disable-line
}

get request() {
if (this._call) {
const entry = this._history[this._call - 1].request;
Expand Down Expand Up @@ -73,6 +81,7 @@ export default class ServerHistory {

reset() {
this._history = [];
this._log = [];
this._call = null;
this._target = null;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/units/log.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Server} from '@';

const server = new Server();

describe('Log and verbose tests', function() {
before(() => server.start());
afterEach(() => server.reset());
after(() => server.stop());

it('should log request/response/error', async function() {
server.warnOnError(false);
await fetch('/api/log');
server.respond.with.preset('default');
await fetch('/api/log');

server.history.logs.should.deep.equal([
'Request : GET http://localhost:9876/api/log',
'FMF error: No fixtures defined',
'Request : GET http://localhost:9876/api/log',
'Response sent (200 OK)'
])
})

it('should log and verbose request/response/error', async function() {
sinon.spy(console, 'log');

server.warnOnError(false).verbose(true);
await fetch('/api/log');
server.respond.with.preset('default');
await fetch('/api/log');

console.log.args.should.deep.equal([ // eslint-disable-line
['Request : GET http://localhost:9876/api/log'],
['FMF error: No fixtures defined'],
['Request : GET http://localhost:9876/api/log'],
['Response sent (200 OK)']
])

console.log.restore(); // eslint-disable-line
})
})

0 comments on commit fb12396

Please sign in to comment.