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

Add interceptor at the beginning of list #2555

Open
1 of 2 tasks
ten-kis opened this issue Nov 9, 2023 · 0 comments
Open
1 of 2 tasks

Add interceptor at the beginning of list #2555

ten-kis opened this issue Nov 9, 2023 · 0 comments
Labels

Comments

@ten-kis
Copy link

ten-kis commented Nov 9, 2023

Please avoid duplicates

Context

When interceptor is added - it's added at the end of the list. I'd like to have opportunity to add interceptor at the beginning of the list so it'll be checked before existing.

Context:
When I write integration tests I have global mock for all third party calls that can potentially happen - it helps me forget about any potential call that can happen when I write my test scenraio. For example I'm covering some GET /something request with tests: this endpoint sends another call to third party that I already have mocked globally. In this particular test scenario I don't care about this call so I just want third party response to me with something that not an error to proceed with scenario I care about. And this is how I write tests for 95% of test scenarios.

Here's how this global mock looks like

const UUID_REGEXP = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/;
        Nock(BASE_URL)
            .persist()
            .get(new RegExp(`/v1/something/${UUID_REGEXP.source}`))
            .reply(200, () => {
                const id = 'uuid_v4';
                return { id };
            });

As you can see interceptor above catches any potential request to /v1/something/{id} so if for particular scenario I want to add another interceptor for a specific ID - I can't cause the global interceptor has regexp that catches everything. So what I'm doing is

  1. Removing existing interceptor
  2. Adding interceptor to catch request with my specific ID I need
  3. Setting global interceptor back at the end of test

If there would be opportunity to add interceptor at the beginning of list for a specific ID - I would not need to remove my global interceptor! The interceptor for a specific ID would be check request at first, and if ID doesn't match - the request would checked by second interceptor (which is with RegExp to catch any ID) and mock it.

From the interface how to do it: I don't care being honest, for example it can be

Nock(BASE_URL)
            .persist()
            .get(`/v1/something/1`), {unshift: true}) // specific flag to add to the beginnig of list
            .reply(200, {id: 1});

Alternatives

No response

If the feature request is accepted, would you be willing to submit a PR?

  • yes
@ten-kis ten-kis added the feature label Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant