Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registerCleanupForTesting #6114

Merged
merged 4 commits into from
Jan 8, 2025
Merged

registerCleanupForTesting #6114

merged 4 commits into from
Jan 8, 2025

Conversation

dskloetd
Copy link
Contributor

@dskloetd dskloetd commented Jan 7, 2025

Motivation

Our unit tests have always been quite brittle because we need to remember to clean up all global state between each test.
Often tests fail when run in a different order because one test accidentally depends on state left behind by other tests.

This has improved a lot now that we always reset all stores and restore all mocks, but there are still other cleanups that need to be performed in tests.

This PR proposes a generic way of registering a cleanup in the file that defines the thing that needs to be cleaned up, instead of in every test. As a proof of concept, it is then used to automatically call resetMockedConstants(); in every test that (transitively) imports $tests/utils/mockable-constants.test-utils.ts.

Changes

  1. Create an empty registerCleanupForTesting function which should be used to register cleanup functions.
  2. In frontend/vitest.setup.ts mock frontend/src/lib/utils/test-support.utils.ts such that the registered cleanup functions are called before every test.

Tests

Tests only

Todos

  • Add entry to changelog (if necessary).
    not necessary

yhabib
yhabib previously approved these changes Jan 7, 2025
Copy link
Contributor

@yhabib yhabib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a quite clever way to approach it! 🙌

frontend/vitest.setup.ts Show resolved Hide resolved
@yhabib yhabib dismissed their stale review January 7, 2025 15:47

It is a draft

@dskloetd dskloetd marked this pull request as ready for review January 8, 2025 12:00
@dskloetd dskloetd requested a review from a team as a code owner January 8, 2025 12:00
Copy link
Contributor

@mstrasinskis mstrasinskis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Contributor

@yhabib yhabib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvement

@dskloetd dskloetd enabled auto-merge January 8, 2025 12:03
@dskloetd dskloetd added this pull request to the merge queue Jan 8, 2025
Merged via the queue into main with commit ba5bba6 Jan 8, 2025
31 checks passed
@dskloetd dskloetd deleted the kloet/register-cleanup branch January 8, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants