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 your test suite runs. This is not surprising given the shared context.
What is surprising, is there is no clear mechanism to control this.
The result is that calling functions like clearAllMocks or restoreAllMocks get progressively slower over time (as we simply loop over all active mocks) - without ever having a way to reduce the size of the set.
Here 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__/
I 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.
This 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.
Using isolate: true, does solve this problem - because the context is reset between tests. But this introduces other problems we then have to overcome.
Suggested solution
I find it surprising there is no way to "clean/kill mocks" that are in memory.
We have the power to clean up other parts of shared context unstubAllGlobals or unstubAllEnvs are maybe good examples?
I'd kind of expect some killAllMocks function.
Alternative
I guess there is an argument that we should mock less things. Or not run with isolate: false.
But 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.
Alternatively 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.
Additional context
I'd be open to a suggestion on how to solve this problem with existing methods - without resorting to:
- Changing
isolate:false or fileParallelism: false
- Mocking less things
Validations
Clear and concise description of the problem
In our project, we have a need to use
isolate: falseandfileParallelism: falsein vitest.We 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.
What is surprising, is there is no clear mechanism to control this.
The result is that calling functions like
clearAllMocksorrestoreAllMocksget progressively slower over time (as we simply loop over all active mocks) - without ever having a way to reduce the size of the set.Here 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__/
I have 5 test files, that each create a large number of mocks and you can see the performance of
clearAllMocksdegrades over time - relative to the number of mocks being created.This 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.
Using
isolate: true, does solve this problem - because the context is reset between tests. But this introduces other problems we then have to overcome.Suggested solution
I find it surprising there is no way to "clean/kill mocks" that are in memory.
We have the power to clean up other parts of shared context
unstubAllGlobalsorunstubAllEnvsare maybe good examples?I'd kind of expect some
killAllMocksfunction.Alternative
I guess there is an argument that we should mock less things. Or not run with
isolate: false.But 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.
Alternatively I guess we could also say that
resetAllMocksorrestoreAllMocksshould 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.Additional context
I'd be open to a suggestion on how to solve this problem with existing methods - without resorting to:
isolate:falseorfileParallelism: falseValidations