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
Jest --detectLeaks reports memory leaks on nock.cleanAll() and nock.restore() #2358
Comments
I ran into this as well. It's not directly related to either of those functions. As long as you have a "hard" import to nock, it will cause this. For example this modified version of your sample test also triggers it for me (note the side-effect import, which is preserved in the transpiled code):
I think it has something to do with how nock monkey-patches Node's http modules which doesn't play nice with Jest's architecture. I was able to get all memory leak warnings to go away by using this workaround:
Now, assuming you call |
Adding to @eric-parsons 's solution. I noticed
would not be enough to prevent a memory leak. Sometimes it looks like there would be pending requests as well that would lead to the memory leak warning with jest. This can be avoided by adding
|
@mkurapov Does this happen to you in 13.5.1? https://github.com/nock/nock/releases/tag/v13.5.1 |
Hi @mikicho, yes, same with 13.5.1. |
If I remove the nock code from the |
@Uzlopak that's odd. On my end: |
hey @mkurapov try to change the order of your imports from:
to:
|
@VladimirChuprazov changing the order of imports works for me in the test repo, but not in the repo where I am trying to address the leaks. In any case, I think this can be added to the README maybe? |
Maybe related to: jestjs/jest#6814 |
Please avoid duplicates
Reproducible test case
https://github.com/jaylonez/nock-mem-leak
Nock Version
13.2.4
Node Version
16.13.0
TypeScript Version
4.6.4
What happened?
Running a simple test with an
afterAll
that runsnock.cleanAll()
ornock.restore()
gets reported as a memory leak when using the--detectLeaks
flag in jest.Having either of these function calls present will result in the memory leak, but commenting both of them out will result in the test passing properly.
Would you be interested in contributing a fix?
The text was updated successfully, but these errors were encountered: