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 inbeforeEach
which makes this logic more complicated.setup
function 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
setup
functionsetup
functionsetup
function 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 understandsetup
function, they will scroll at the bottomDrawbacks
TestInput
Recommendations of writing
setup
functionsetup
single functioninput
parametersetup
function if creation of object under testing is super easywithSetup
to define cleanup logic:where
withSetup
looks like this:Beta Was this translation helpful? Give feedback.
All reactions