New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Scope#clone()
method
#2564
Conversation
What is the advantage over just using |
The advantage of using baseScope.clone() is the ability to create a new scope with the same parameters without duplicating the parameters in each test. For example: describe('Example', () => {
const baseScope = nock('http://www.example.com', {
reqheaders: {
authorization: 'Basic Auth',
},
})
it('GET scope is done', async () => {
const get = baseScope.clone().get('/').reply(200)
await got('http://example.test/')
expect(get.isDone()).to.be.true()
})
it('POST scope is done', async () => {
const post = baseScope.clone().post('/').reply(200)
await got.post('http://example.test/')
expect(post.isDone()).to.be.true()
})
}) You can generally do without baseScope.clone() and make a function that will create a scope with the necessary parameters. But it seems to me that this is not very convenient and I would like to have an interface that helps to clone baseScope with the necessary parameters. For example: describe('Example', () => {
const baseScope = () => nock('http://www.example.com', {
reqheaders: {
authorization: 'Basic Auth',
},
})
it('GET scope is done', async () => {
const get = baseScope().get('/').reply(200)
await got('http://example.test/')
expect(get.isDone()).to.be.true()
})
it('POST scope is done', async () => {
const post = baseScope().post('/').reply(200)
await got.post('http://example.test/')
expect(post.isDone()).to.be.true()
})
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for clarifying, this is a great additional, and a great PR. Thank you!
@all-contributors please add @rsaryev for code and test |
I've put up a pull request to add @rsaryev! 🎉 |
🎉 This PR is included in version 13.5.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This ship already sailed, but: export function createScope(url) {
return nock('http://www.example.com', {
reqheaders: {
authorization: 'Basic Auth',
},
})
} |
Hi, #2564 (comment) I described it here, or did you have something else in mind? |
Yeah, I just disagree with the conclusion :) |
New Feature:
Scope#clone()
MethodThis pull request introduces a new method
Scope#clone()
to theScope
. This method allows creating a newScope
with the samebasePath
andscope options
.Motivation
This is particularly useful when we cannot wait for all requests to complete, and we want to check if the requests have been fulfilled using the
isDone()
method.Usage
Here is a simple usage example: