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

Push persisted interceptors to the end instead of ignore on remove #2350

Merged
merged 2 commits into from Jun 25, 2023

Conversation

mikicho
Copy link
Contributor

@mikicho mikicho commented May 12, 2022

Currently, if you do:

nock(..).get('/').reply(200).persist();
nock(..).get('/').reply(400); // this will never get called

The second interceptor won't get called ever.

But sometimes in tests (queue for examples), I want to do:

beforeAll(() => {
  // some default behaviour
  nock(..).get('/').reply(200).persist();
});
test('should ...', () => {
  // test specific scenario for this same endpoint above
  nock(..).get('/').reply(400)
})

I find a workaround using the body matcher function:

const createInterceptor = () => {
  nock(..).get('/', () => { 
    createInterceptor(); 
    return true; 
  })
  .reply(200)
}

Which pushes the interceptor to the end of the queue (interceptors array) and mimics the wanted behavior but is slower and way less elegant.

P.S: I'll add tests and documentation if we agree on the wanted behavior.

@mikicho
Copy link
Contributor Author

mikicho commented Mar 15, 2023

Trying my luck.
@gr2m can you please take a look on this?

@gr2m
Copy link
Member

gr2m commented Jun 21, 2023

Currently, if you do:

nock(..).get('/').reply(200).persist();
nock(..).get('/').reply(400); // this will never get called

The second interceptor won't get called ever.

Just to check, swapping the lines around works, but you can't do it because you call the .perst() line in a beforeEach block?

As long as no other tests break and you update docs, I'd be happy to help get it landed.

Sorry for the late response

@mikicho
Copy link
Contributor Author

mikicho commented Jun 21, 2023

Just to check, swapping the lines around works, but you can't do it because you call the .perst() line in a beforeEach block?

beforeAll, yes.

As long as no other tests break and you update docs, I'd be happy to help get it landed.

Cool! I'll add relevant tests.

Currently, if you do:
```
nock(..).get('/').reply(200).persist();
nock(..).get('/').reply(400); // this will never get called
```
The second interceptor won't get called ever.

But sometimes in tests (queue for examples), I want to do:
```
beforeAll(() => {
  // some default behaviour
  nock(..).get('/').reply(200).persist();
});
test('should ...', () => {
  // test specific scenario for this same endpoint above
  nock(..).get('/').reply(400)
})
```

I find a workaround using the body matcher function:
```
const createInterceptor = () => {
  nock(..).get('/', () => {
    createInterceptor();
    return true;
  })
  .reply(200)
}
```
Which mimics the wanted behavior but is slower and way less elegant.

fix
@mikicho
Copy link
Contributor Author

mikicho commented Jun 23, 2023

@gr2m PTAL

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 so much 💐

@gr2m gr2m merged commit 8aab603 into nock:main Jun 25, 2023
19 checks passed
@mikicho mikicho deleted the patch-1 branch June 26, 2023 04:59
@github-actions
Copy link

🎉 This PR is included in version 13.3.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

gr2m added a commit that referenced this pull request Aug 16, 2023
gr2m added a commit that referenced this pull request Aug 16, 2023
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

2 participants