Created
October 10, 2017 23:42
-
-
Save dylants/655a1ed88225d3aa1143f9becd687e8f to your computer and use it in GitHub Desktop.
Nock / mock API requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
"scripts": { | |
// ... | |
"mock": "MOCK_API=true npm start", | |
// ... | |
}, | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
// 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