Skip to content

Instantly share code, notes, and snippets.

@ksheedlo
Last active October 24, 2015 06:26
Show Gist options
  • Save ksheedlo/de392f55f788ee430759 to your computer and use it in GitHub Desktop.
Save ksheedlo/de392f55f788ee430759 to your computer and use it in GitHub Desktop.
'use strict';
global.Promise = global.Promise || require('bluebird');
var request = require('request'),
fetch = require('node-fetch');
exports.getRequest = function getRequest() {
return new Promise(function (resolve, reject) {
request({
url: 'https://api.whatever.co/items',
headers: {
'X-Foo': 'lolwhatever'
},
json: true
}, function (err, resp, body) {
if (err) {
return reject(err);
}
return resolve(body);
});
});
};
exports.getFetch = function getFetch() {
return fetch('https://api.whatever.co/items', {
headers: {
'X-Foo': 'lolwhatever'
}
})
.then(function (response) {
return response.json();
});
};
'use strict';
var nock = require('nock'),
getters = require('../');
describe('fetching', function () {
it('getRequest gets the stuff', function (done) {
var scope = nock('https://api.whatever.co', {
reqheaders: {
'X-Foo': 'lolwhatever'
}
})
.get('/items')
.reply(200, [{ sku: '123' }, { sku: '456' }])
getters.getRequest().then(function (resp) {
scope.done();
done();
});
});
it('getFetch works without headers', function (done) {
var scope = nock('https://api.whatever.co')
.get('/items')
.reply(200, [{ sku: '123' }, { sku: '456' }])
getters.getFetch().then(function (resp) {
scope.done();
done();
})
.catch(function (err) {
console.error(err.stack);
throw err;
});
});
it('getFetch gets the stuff', function (done) {
var scope = nock('https://api.whatever.co', {
reqheaders: {
'X-Foo': 'lolwhatever'
}
})
.get('/items')
.reply(200, [{ sku: '123' }, { sku: '456' }])
getters.getFetch().then(function (resp) {
scope.done();
done();
})
.catch(function (err) {
console.error(err.stack);
throw err;
});
});
});
fetching
✓ getRequest gets the stuff
✓ getFetch works without headers
Error: request to https://api.whatever.co/items failed, reason: Nock: No match for request GET https://api.whatever.co/items
at OverriddenClientRequest.<anonymous> (/Users/ken/ksheedlo/basic-nock/node_modules/node-fetch/index.js:117:11)
at OverriddenClientRequest.emit (events.js:107:17)
at /Users/ken/ksheedlo/basic-nock/node_modules/nock/lib/request_overrider.js:202:11
at process._tickCallback (node.js:355:11)
1) getFetch gets the stuff
2 passing (2s)
1 failing
1) fetching getFetch gets the stuff:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment