test-runner-module-mocking: bug when loaded normally in previous test before intercepted in next #2700
+64
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What I did
We discussed a similar situation, but different, when the order of mocking and importing was relevant within a single test file.
But between 2 independent test files such problem wasn't discussed thoroughly, so I consider this to be a bug.
I hope my reproduction sheds some light on what goes wrong.
The biggest difference is that within a single test file you can make any order of loading modules you want, therefore this solution was OK.
But between 2 files I can't really guarantee this. In my repository I have tens of test files, many of them import reusable utilities. The module graph can be quite random depending on the unit test entry point.
The only solution I see is to have a special entry point for all unit tests which is guaranteed to be first loaded which will mock all necessary modules beforehand, but I don't think this is an acceptable solution, it scatters the test logic. We can as well just make a configuration for this then, but still same problem with scattering the test logic.
Fun fact: I was even able to use
test-runner-module-mocking
first without any issue, before smth changed in the order of test files loading, and caused this bug. Not so fun actually, as I spent a few hours debugging this and finding the root cause.