Skip to content
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

Merged
merged 2 commits into from Jan 14, 2024
Merged

feat: add Scope#clone() method #2564

merged 2 commits into from Jan 14, 2024

Conversation

rsaryev
Copy link
Contributor

@rsaryev rsaryev commented Dec 9, 2023

New Feature: Scope#clone() Method

This pull request introduces a new method Scope#clone() to the Scope. This method allows creating a new Scope with the same basePath and scope 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:

describe('Example', () => {
  const baseScope = nock('http://example.test')

  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()
  })
})

@gr2m
Copy link
Member

gr2m commented Dec 12, 2023

What is the advantage over just using nock('http://example.test') instead of baseScope.clone()?

@rsaryev
Copy link
Contributor Author

rsaryev commented Dec 14, 2023

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()
    })
})

Copy link
Member

@gr2m gr2m left a 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!

@gr2m gr2m merged commit 65385f7 into nock:main Jan 14, 2024
19 checks passed
@gr2m
Copy link
Member

gr2m commented Jan 14, 2024

@all-contributors please add @rsaryev for code and test

Copy link
Contributor

@gr2m

I've put up a pull request to add @rsaryev! 🎉

Copy link

🎉 This PR is included in version 13.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@rsaryev rsaryev deleted the add-scope-clone-method branch January 18, 2024 20:56
@mikicho
Copy link
Contributor

mikicho commented Jan 29, 2024

This ship already sailed, but:
@rsaryev What's not very convenient about a factory function?

export function createScope(url) {
  return nock('http://www.example.com', {
        reqheaders: {
            authorization: 'Basic Auth',
        },
    })
}

@rsaryev
Copy link
Contributor Author

rsaryev commented Jan 29, 2024

This ship already sailed, but:

@rsaryev What's not very convenient about a factory function?

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?

@mikicho
Copy link
Contributor

mikicho commented Jan 29, 2024

Yeah, I just disagree with the conclusion :)
I think adding API to a library should justify the future maintenance effort. In some extreme cases, when you have dozens of functions, it can be cumbersome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants