Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@neverendingqs
Last active August 18, 2018 01:32
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 neverendingqs/ee784b5d8726755402fe0bdb870c60ed to your computer and use it in GitHub Desktop.
Save neverendingqs/ee784b5d8726755402fe0bdb870c60ed to your computer and use it in GitHub Desktop.
2017/03/13/ensure-all-nock-interceptors-are-used.html - final.js
'use strict';
const assert = require('chai').assert;
const nock = require('nock');
const request = require('supertest');
const uniqueValue = 'da64daaf-182b-4af6-a4af-09727bf8d5aa';
const app = require('../../src/server');
describe('server', function() {
afterEach(function() {
if(!nock.isDone()) {
this.test.error(new Error('Not all nock interceptors were used!'));
nock.cleanAll();
}
});
it('GET / once', function() {
nock('https://www.example.com')
.get('/')
.reply(200, uniqueValue);
return request(app)
.get('/')
.then(res => assert.equal(res.text, uniqueValue));
});
it('GET / five times', function() {
nock('https://www.example.com')
.get('/')
.times(5) // sets up 5 interceptors
.reply(200, uniqueValue);
return request(app)
.get('/')
.then(res => assert.equal(res.text, uniqueValue));
});
it('GET / fails gracefully if example.com is down', function() {
nock('https://www.example.com')
.get('/')
.reply(500);
return request(app)
.get('/')
.then(res => assert.equal(res.status, 503));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment