pgte/nock — Issue #30: > 1 nock scopes, global intercept problems
Author: iangreenleaf
Created: 2012-02-03 21:22:15+00:00
State: open

I noticed that in my tests using nock, if one test failed to fulfill an expectation, subsequent tests that set up the same expectation would fail, because the requests were being hijacked by a lingering intercept from the previous test. I wrote a test that demonstrates:

```javascript
tap.test("scopes are independent", function(t) {
var scope1 = nock('http://www.google.com')
.get('/')
.reply(200, "Hello World!");
var scope2 = nock('http://www.google.com')
.get('/')
.reply(200, "Hello World!");

var req = http.request({
host: "www.google.com"
, path: '/'
, port: 80
}, function(res) {
res.on('end', function() {
t.ok(scope1.isDone());
t.ok(scope2.isDone()); // fails
t.end();
});
});

req.end();
});
```

What's your preferred way to fix this?

* One intercepted request could fulfill expectations from multiple scopes (in other words, the above test would pass without modification).
* `nock.restore()` could clear out whatever is hanging around to cause this. Test would change to something like this:

```javascript
tap.test("scopes are independent", function(t) {
var scope1 = nock(/*...*/);
nock.restore();
var scope2 = nock(/*...*/);

var req = http.request({
//...
}, function(res) {
res.on('end', function() {
t.notOk(scope1.isDone());
t.ok(scope2.isDone());
t.end();
});
});
req.end();
});
```
* Similar to the previous option, but give the duty to a new method rather than `restore`.