About the challenges of afterEach in interactive test tools #7141
Replies: 1 comment
-
Interesting point. I think it might be unique to projects that have a visual interface where components can be viewed after the fact and cannot be generalized into a standardized built-in hook in a test runner like Vitest unless we can define it in another way ("a hook that should be called to release resources", for example).
I think if we want to have a general solution, it's best to recommend |
Beta Was this translation helpful? Give feedback.
-
Hello! Kasper from Storybook here.
At Storybook, we’re exploring adding an
afterEach
hook that runs after each story test execution, similar to how afterEach works in tools like Vitest. However, we’ve encountered some fundamental challenges with incorporating afterEach into an interactive testing tool.I’m reaching out because I suspect you’ve faced similar issues and thought it would be valuable to align our approaches.
Using
afterEach
for Cleanup Impedes InteractionIn an interactive testing environment, performing cleanup immediately after test execution can render the test uninteractive. For example, Vitest’s fork of @testing-library/react avoids DOM cleanup in afterEach and instead performs it in beforeEach:
https://github.com/vitest-dev/vitest-browser-react
But this problem occurs every time cleanup is done in
afterEach.
For another example, MSW recommends resetting all handlers inafterEach
.In Vitest Browser UI, that would mean that interacting with the test won't hit the mocked endpoints anymore. This can be solved, by doing the reset handlers in a
beforeEach
.Delaying the execution breaks if assertions are used in
afterEach
At Storybook, we haven’t yet adopted
afterEach
but recently introducedbeforeEach
, which supports a return callback for cleanup:Instead of running the cleanup callback immediately after the test, we defer it until switching stories. This avoids the interaction issue but creates a new problem: it prevents post-test assertions.
For example, a user trying to assert that console.error wasn’t called in any story wouldn’t see the assertion in Storybook due to the delayed execution:
While assertions in afterEach are sometimes discouraged, they can be valuable in scenarios like the one above. Other examples include automatically checking for accessibility violations after each test or capturing a DOM/visual snapshot.
Proposed path forward
So concluding, we think that there is need for 2 distinct stages at the end of testing lifecycle:
To address this, we are considering the following approach:
afterEach
to Storybook, but recommend its use for post-test assertions rather than cleanup.Alternatively, we could introduce a dedicated hook for post-test assertions instead of adopting afterEach. This would avoid the confusion of adding afterEach while advising against its use for cleanup.
We’re curious to hear your thoughts on the best path forward and whether there’s an opportunity to align our approaches.
Beta Was this translation helpful? Give feedback.
All reactions