setup function instead of beforeEach in unit & service level tests
#910
stalniy
started this conversation in
Contribution RFC
Replies: 1 comment
-
|
Agreed to try to write cursor rule in mdc format to write test like this |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
This RFC proposes a shift in how testing code is structured.
Instead of relying on beforeEach blocks and shared mutable variables across multiple tests, it suggests adopting a setup function pattern. This approach promotes clearer test boundaries, improves readability, and reduces hidden dependencies between tests
Motivation
beforeEach+ variables add a lot stuff at the beginning of tests, usually this is not easy to understand code with type casting and spies which developer need to reset inafterEach. Some tests may require a bit different setup from the one defined inbeforeEachwhich makes this logic more complicated.setupfunction aims to encapsulate this logic into a function that can be called manually in every tests with test specific parametersExamples
Current one
Suggested one
Main benefits
setupfunctionsetupfunctionsetupfunction at the bottom of test suite doesn't force developers to read implementation details, they can first look at test on high level and only if they need to change or understandsetupfunction, they will scroll at the bottomDrawbacks
TestInputRecommendations of writing
setupfunctionsetupsingle functioninputparametersetupfunction if creation of object under testing is super easywithSetupto define cleanup logic:where
withSetuplooks like this:Beta Was this translation helpful? Give feedback.
All reactions