Skip to content

Commit

Permalink
Add isDone() to global scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Sousa committed Oct 28, 2015
1 parent f480a84 commit d530377
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,21 @@ setTimeout(function() {

## .isDone()

You can also call `isDone()`, which will return a boolean saying if all the expectations are met or not (instead of throwing an exception);
You can call `isDone()` on a single expectation to determine if the expectation was met:

```js
var scope = nock('http://google.com')
.get('/')
.reply(200);

scope.isDone(); // will return false
```

It is also available in the global scope, which will determine if all expectations have been met:

```js
nock.isDone();
```

## .cleanAll()

Expand Down
9 changes: 9 additions & 0 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ function isActive() {

}

function isDone() {
return _.every(allInterceptors, function(interceptors) {
return _.every(interceptors, function(interceptor) {
return interceptor.__nock_scope.isDone();
});
});
}

function activate() {

if(originalClientRequest) {
Expand Down Expand Up @@ -414,6 +422,7 @@ module.exports.removeInterceptor = removeInterceptor;
module.exports.isOn = isOn;
module.exports.activate = activate;
module.exports.isActive = isActive;
module.exports.isDone = isDone;
module.exports.enableNetConnect = enableNetConnect;
module.exports.disableNetConnect = disableNetConnect;
module.exports.overrideClientRequest = overrideClientRequest;
Expand Down
1 change: 1 addition & 0 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ module.exports = startScope;
module.exports.cleanAll = cleanAll;
module.exports.activate = globalIntercept.activate;
module.exports.isActive = globalIntercept.isActive;
module.exports.isDone = globalIntercept.isDone;
module.exports.removeInterceptor = globalIntercept.removeInterceptor;
module.exports.disableNetConnect = globalIntercept.disableNetConnect;
module.exports.enableNetConnect = globalIntercept.enableNetConnect;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,20 @@ test('clean all works', function(t) {

});

test('is done works', function(t) {
var scope = nock('http://amazon.com')
.get('/nonexistent')
.reply(200);

t.ok(!scope.isDone());

var req = http.get({host: 'amazon.com', path: '/nonexistent'}, function(res) {
t.assert(res.statusCode === 200, "should mock before cleanup");
t.ok(nock.isDone());
t.end();
});
});

test('username and password works', function(t) {
var scope = nock('http://passwordyy.com')
.get('/')
Expand Down

0 comments on commit d530377

Please sign in to comment.