|
| 1 | +var nockSandbox = require('./'); |
| 2 | +var nativeNock = require('nock'); |
| 3 | +var expect = require('chai').expect; |
| 4 | +var sinon = require('sinon'); |
| 5 | +var finalizeEach = require('mocha-finalize-each'); |
| 6 | + |
| 7 | +describe('nock-sandbox', function() { |
| 8 | + var sinonSandbox; |
| 9 | + beforeEach(function() { |
| 10 | + sinonSandbox = sinon.sandbox.create(); |
| 11 | + }); |
| 12 | + afterEach(function() { |
| 13 | + sinonSandbox.reset(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should create a nock factory', function() { |
| 17 | + var nock = nockSandbox(); |
| 18 | + expect(nock).to.be.instanceOf(Function); |
| 19 | + expect(nock).to.have.property('cleanAll'); |
| 20 | + expect(nock).to.have.property('done'); |
| 21 | + var handle1 = nock('foo'); |
| 22 | + var handle2 = nativeNock('foo'); |
| 23 | + expect(handle1).to.be.instanceOf(handle2.constructor); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should clean up scope after .done', function() { |
| 27 | + var nock = nockSandbox(); |
| 28 | + var nockScope = nock('foo'); |
| 29 | + sinon.stub(nockScope, 'done').callsFake(function() {}); |
| 30 | + sinon.stub(nock, 'cleanAll').callsFake(function() {}); |
| 31 | + |
| 32 | + nock.done(); |
| 33 | + |
| 34 | + sinon.assert.calledOnce(nockScope.done); |
| 35 | + sinon.assert.calledOnce(nock.cleanAll); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should throw on .done if nock was not used', function() { |
| 39 | + var nock = nockSandbox(); |
| 40 | + var nockScope = nock('http://foo/bar').get('.').reply(200); |
| 41 | + expect(function() { |
| 42 | + nock.done(); |
| 43 | + }).to.throw('Mocks not yet satisfied:'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should disable netConnect and re-enable it', function() { |
| 47 | + sinon.stub(nativeNock, 'disableNetConnect').callsFake(function() {}) |
| 48 | + var nock = nockSandbox(); |
| 49 | + sinon.assert.calledOnce(nativeNock.disableNetConnect); |
| 50 | + sinon.stub(nativeNock, 'enableNetConnect').callsFake(function() {}); |
| 51 | + nock.done(); |
| 52 | + sinon.assert.calledOnce(nativeNock.enableNetConnect); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should clean up all interceptors after cleanAll', function() { |
| 56 | + var nock = nockSandbox(); |
| 57 | + var scope = nock('http://localhost:5555'); |
| 58 | + scope.get('/').reply('foo'); |
| 59 | + scope.get('/').reply('bar'); |
| 60 | + expect(scope.interceptors).to.have.length(2); |
| 61 | + expect(scope.isDone()).to.be.false; |
| 62 | + nock.cleanAll(); |
| 63 | + expect(scope.isDone()).to.be.true; |
| 64 | + }); |
| 65 | + |
| 66 | + describe('setupForMocha', function() { |
| 67 | + var nock = nockSandbox.setupForMocha(this); |
| 68 | + var beginSpy = sinon.spy(nock, 'begin'); |
| 69 | + var endSpy = sinon.spy(nock, 'done'); |
| 70 | + |
| 71 | + // NOTE: these two tests are dependent on each other |
| 72 | + it('calls nock.begin before test', function() { |
| 73 | + sinon.assert.calledOnce(beginSpy); |
| 74 | + }); |
| 75 | + it('calls nock.begin every test and nock.done', function() { |
| 76 | + sinon.assert.calledTwice(beginSpy); |
| 77 | + sinon.assert.calledOnce(endSpy); |
| 78 | + }); |
| 79 | + }) |
| 80 | + |
| 81 | + describe('setupForMocha lets each test fail independently', function() { |
| 82 | + var nock = nockSandbox.setupForMocha(this); |
| 83 | + var finalizeError; |
| 84 | + finalizeEach(this, promise => { |
| 85 | + return promise.catch(err => { |
| 86 | + finalizeError = err; |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('#1 is failing and error recorded', function() { |
| 91 | + nock('http://foo.com').get('/').reply('foo'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('#2 is failing and error recorded', function() { |
| 95 | + nock('http://foo.com').get('/').reply('bar'); |
| 96 | + }); |
| 97 | + |
| 98 | + afterEach(function() { |
| 99 | + expect(finalizeError.message).to.contain('Mocks not yet satisfied:'); |
| 100 | + }); |
| 101 | + }) |
| 102 | + |
| 103 | +}); |
0 commit comments