Skip to content

Instantly share code, notes, and snippets.

@LearningNerd
Created September 30, 2017 15:52
Show Gist options
  • Save LearningNerd/7acfa738564d45ebd1fda4d960184ed7 to your computer and use it in GitHub Desktop.
Save LearningNerd/7acfa738564d45ebd1fda4d960184ed7 to your computer and use it in GitHub Desktop.
Code by @gr2m -- 5 min example he showed me of how @node-nock can mock an API for testing
const assert = require('assert')
const nock = require('nock')
const axios = require('axios')
// nock('https://api.github.com')
// .get('/')
// .reply(200, {
// current_user_url: 'https://api.github.com/user'
// })
nock.recorder.rec()
myLib()
.then(result => {
assert.equal(result.current_user_url, 'https://api.github.com/user')
console.log('ok')
}, assert.ifError)
function myLib () {
return axios.get('https://api.github.com')
.then(result => result.data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment