Difficulty clearing one scope
See original GitHub issueContext
In a parallel test suite, it is not easy to clear a single nock scope if there are multiple initialized. Calling nock.cleanAll()
is not an option per test file, since that would break other in progress test files. scope.complete()
almost gets there, but I have some test suites that will not use all routes set up for a scope (I reuse and re-initialize the same scope for multiple test cases)
Simple example of the interface I am looking for:
// test1.js
describe('test1', () => {
let scope
before(async () => {
scope = nock('http://google.com')
})
after(() => scope.cleanAll())
it('test', () => {
// call some http requests
})
})
// test2.js
describe('test2', () => {
let scope
before(async () => {
scope = nock('http://news.ycombinator.com')
})
after(() => scope.cleanAll())
it('test', () => {
// call some http requests
})
})
Alternatives The alternative is to only run tests in serial.
Has the feature been requested before? concerns related to parallel testing have been brought up before: https://github.com/nock/nock/issues/541 https://github.com/nock/nock/issues/30
If the feature request is accepted, would you be willing to submit a PR? Yes, if help is provided
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (2 by maintainers)
We often just want to remove a scope. So we do: scope.interceptors.forEach(nock.removeInterceptor) It works. But it’s bad. I shouldn’t be poking my nose in nock’s internal variables. A future release could break that and I’d have no right to complain. BUT the use-case seems compelling. You build a set of mocks on a single scope and you keep the scope. When you’re done (but other stuff may be nock’ed elsewhere) you want to remove your mocks. That seems like the primary use-case for saving the scope (other than augmenting it during the initial build). It’d be nice if there was a scope.remove() or scope.delete() and maybe even scope.suspend() and scope.resume(). I might make a small PR to add something that does that one line but was wondering if I’d missed something. Is there a reason not to expose a “delete”/“cancel” on a scope? Seems like a gimme to me.
I vote for
scope.restore()
because we havenock.restore()
, andsinon
has asandbox
it also usesrestore()
.