Skip to content

Commit 3e78d62

Browse files
committed
Initial commit
1 parent d8e244c commit 3e78d62

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

index.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var nock = require('nock');
2+
var finalizeEach = require('mocha-finalize-each');
3+
4+
var nockSandbox = module.exports = function(constructorFunction, options) {
5+
var nocks = [];
6+
var nockFactory = function () {
7+
var args = Array.prototype.slice.call(arguments);
8+
var nockScope = nock.apply(nock, args);
9+
nocks.push(nockScope);
10+
return nockScope;
11+
};
12+
13+
nockFactory.begin = function () {
14+
nock.disableNetConnect();
15+
nock.enableNetConnect(/127\.0\.0\.1|localhost/);
16+
};
17+
18+
if (!(options && options.noAutoBegin)) {
19+
nockFactory.begin();
20+
}
21+
22+
nockFactory.cleanAll = function() {
23+
nocks.forEach(function(nockScope) {
24+
nockScope.interceptors.forEach(function(interceptor) {
25+
nock.removeInterceptor(interceptor);
26+
});
27+
});
28+
};
29+
nockFactory.done = function() {
30+
try {
31+
nocks.forEach(function (nockScope) {
32+
// will throw if nock is not resolved
33+
nockScope.done();
34+
});
35+
} finally {
36+
nockFactory.cleanAll();
37+
nock.enableNetConnect();
38+
}
39+
};
40+
41+
if(typeof constructorFunction === 'function') {
42+
constructorFunction.call(null, nockFactory);
43+
}
44+
45+
return nockFactory;
46+
};
47+
48+
nockSandbox.createConstructor = function(constructorFunction) {
49+
return function() {
50+
return module.exports(constructorFunction);
51+
};
52+
};
53+
54+
nockSandbox.setupForMocha = function(mochaContext) {
55+
var nocks = nockSandbox(null, { noAutoBegin: true });
56+
57+
mochaContext.beforeEach(function() {
58+
nocks.begin();
59+
});
60+
61+
finalizeEach(mochaContext, function(promise) {
62+
return promise.then(function() { return nocks.done(); });
63+
});
64+
65+
mochaContext.afterEach(function() {
66+
nocks.cleanAll();
67+
nock.enableNetConnect();
68+
});
69+
70+
return nocks;
71+
};

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "nock-sandbox",
3+
"version": "1.0.0",
4+
"description": "Clean up nock on demand, for example after each test. Helper function for mocha is provided.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"keywords": [
10+
"nock",
11+
"sandbox",
12+
"cleanup",
13+
"restore",
14+
"afterEach",
15+
"remove",
16+
"mocks",
17+
"http"
18+
],
19+
"author": "Damian Kaczmarek <rush@tenfold.com>",
20+
"license": "MIT",
21+
"dependencies": {
22+
"mocha-finalize-each": "^1.0.0",
23+
"nock": "^9.0.14"
24+
},
25+
"devDependencies": {
26+
"chai": "^4.1.1",
27+
"mocha": "^3.5.0",
28+
"sinon": "^3.2.1"
29+
}
30+
}

test.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)