Skip to content

Instantly share code, notes, and snippets.

@dylants
Created October 10, 2017 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylants/655a1ed88225d3aa1143f9becd687e8f to your computer and use it in GitHub Desktop.
Save dylants/655a1ed88225d3aa1143f9becd687e8f to your computer and use it in GitHub Desktop.
Nock / mock API requests
// config/mock/nock.js
import nock from 'nock';
import config from '../../config';
// disable all connections so that we use only the mocks
nock.disableNetConnect();
nock(config.blah.api.rootUrl)
// these interceptors should persist for all requests
// https://github.com/node-nock/nock#persist
.persist()
// SOME ENDPOINT
.get(new RegExp(config.blah.api.someApi))
.reply(200, require('./responses/some-data.json'));
// ...
"scripts": {
// ...
"mock": "MOCK_API=true npm start",
// ...
},
// ...
// ...
// use the mock API responses if required
if (process.env.MOCK_API) {
require('../config/mock/nock');
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment