/sinon-har-server

Auto mocks your server from a recorded .har file. Don't let your mocks lie to you!

Primary LanguageJavaScriptMIT LicenseMIT

sinon-har-server

Auto mocks your server from a recorded .har file. Don't let your mocks lie to you!

Build Status Coverage Status Dependency Status devDependency Status js-semistandard-style

This module works in Node.js as well as in the browser with either AMD or with a <script> tag which defines sinonHarServer as a global.

Usage:

var server;

beforeEach(function () {
  return fetch('api-traffic.har').then(function (response) {
    return response.json();
  }).then(function (harFile) {
    server = sinon.fakeServer.create();
    server.autoRespond = true;
    server.autoRespondAfter = 1;
    return sinonHarServer.load(server, harFile);
  });
});

afterEach(function () {
  server.restore();
});

This is especially useful for capturing all test traffic from your back-end test suite, for doing auto mocking in your front-end test suite. See this project for an example: https://github.com/larsthorup/http-auto-mock-demo

Blog post about this technique: http://www.zealake.com/2015/01/05/unit-test-your-service-integration-layer/

The .har file can be conveniently produced by https://github.com/larsthorup/node-request-har-capture