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