Title: vi.fn "memory leak" with `isolate: false` · Issue #9492 · vitest-dev/vitest · GitHub
Open Graph Title: vi.fn "memory leak" with `isolate: false` · Issue #9492 · vitest-dev/vitest
X Title: vi.fn "memory leak" with `isolate: false` · Issue #9492 · vitest-dev/vitest
Description: Clear and concise description of the problem In our project, we have a need to use isolate: false and fileParallelism: false in vitest. We have seen that under this configuration, the number of vitest mocks being kept in memory grows as ...
Open Graph Description: Clear and concise description of the problem In our project, we have a need to use isolate: false and fileParallelism: false in vitest. We have seen that under this configuration, the number of vit...
X Description: Clear and concise description of the problem In our project, we have a need to use isolate: false and fileParallelism: false in vitest. We have seen that under this configuration, the number of vit...
Opengraph URL: https://github.com/vitest-dev/vitest/issues/9492
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"vi.fn \"memory leak\" with `isolate: false`","articleBody":"### Clear and concise description of the problem\n\nIn our project, we have a need to use `isolate: false` and `fileParallelism: false` in vitest.\n\nWe have seen that under this configuration, the number of vitest mocks being kept in memory grows as your test suite runs. This is not surprising given the shared context. \n\nWhat is surprising, is there is no clear mechanism to control this. \n\nThe result is that calling functions like `clearAllMocks` or `restoreAllMocks` get progressively slower over time ([as we simply loop over all active mocks](https://github.com/vitest-dev/vitest/blame/main/packages/spy/src/index.ts#L634)) - without ever having a way to reduce the size of the set.\n\nHere is a reproduction 👉 https://stackblitz.com/edit/vitest-dev-vitest-y6nkb7na?file=vite.config.ts,test%2Ftest01.test.ts,vitest.setup.ts,test%2Ftest05.test.ts,test%2Ftest04.test.ts&initialPath=__vitest__/\n\nI have 5 test files, that each create a large number of mocks and you can see the performance of `clearAllMocks` degrades over time - relative to the number of mocks being created.\nThis is an extreme case (with 1000's of mocks created) - but in our real-world case, we aren't mocking many functions - but having a lot of tests (5000+ tests) & even mocking a reasonable number of functions per test, get's out of control quickly.\n\nUsing `isolate: true`, does solve this problem - because the context is reset between tests. But this introduces other problems we then have to overcome.\n\n### Suggested solution\n\nI find it surprising there is no way to \"clean/kill mocks\" that are in memory.\nWe have the power to clean up other parts of shared context `unstubAllGlobals` or `unstubAllEnvs` are maybe good examples?\nI'd kind of expect some `killAllMocks` function.\n\n### Alternative\n\nI guess there is an argument that we should mock less things. Or not run with `isolate: false`.\nBut if we imagine for a second, that there is a use case for mocking a large number of functions in a shared context (we have one in our project) - there is not equal alternative.\n\nAlternatively I guess we could also say that `resetAllMocks` or `restoreAllMocks` should be keeping this list under control itself. I don't see any reason that we would want to just let this set grow forever as my tests run.\n\n### Additional context\n\nI'd be open to a suggestion on how to solve this problem with existing methods - without resorting to:\n- Changing `isolate:false` or `fileParallelism: false` \n- Mocking less things\n\n### Validations\n\n- [x] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)\n- [x] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).\n- [x] Read the [docs](https://vitest.dev/guide/).\n- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.","author":{"url":"https://github.com/andrewbrennanfr","@type":"Person","name":"andrewbrennanfr"},"datePublished":"2026-01-20T14:41:25Z","url":"https://github.com/vitest-dev/vitest/issues/9492"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2d8572f7-bbc2-86dc-ccb8-5e44465b1f11 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B558:2DAA79:4C9683:694DB2:6AED8444 |
| html-safe-nonce | 99ec5b58c21e814632df7db8d73298a212f80c023e9a641b63a0bb6893d35c3b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTU4OjJEQUE3OTo0Qzk2ODM6Njk0REIyOjZBRUQ4NDQ0IiwidmlzaXRvcl9pZCI6IjMwMTQzNzUwNzI4MTQ5NTc2MzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e491d725ec4e70dc08af6f95255456e77231c2a03257a7b9ebfad95f03d0b68f |
| hovercard-subject-tag | issue:3834031589 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/vitest-dev/vitest/9492/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e47a33d090612667b0457af758c731f8794f9b8b184d6dcb157f6fbb6ea476c7/vitest-dev/vitest/issues/9492 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e47a33d090612667b0457af758c731f8794f9b8b184d6dcb157f6fbb6ea476c7/vitest-dev/vitest/issues/9492 |
| og:image:alt | Clear and concise description of the problem In our project, we have a need to use isolate: false and fileParallelism: false in vitest. We have seen that under this configuration, the number of vit... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andrewbrennanfr |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| turbo-cache-control | no-preview |
| go-import | github.com/vitest-dev/vitest git https://github.com/vitest-dev/vitest.git |
| octolytics-dimension-user_id | 95747107 |
| octolytics-dimension-user_login | vitest-dev |
| octolytics-dimension-repository_id | 434708679 |
| octolytics-dimension-repository_nwo | vitest-dev/vitest |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 434708679 |
| octolytics-dimension-repository_network_root_nwo | vitest-dev/vitest |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width