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

Add onCleanup hook #7258

Open
4 tasks done
sheremet-va opened this issue Jan 15, 2025 · 1 comment
Open
4 tasks done

Add onCleanup hook #7258

sheremet-va opened this issue Jan 15, 2025 · 1 comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@sheremet-va
Copy link
Member

Clear and concise description of the problem

There are times when the test runners needs to run a cleanup callback manually and not tied it to the test runner lifecycle (avoid beforeEach/afterEach). A good example of this is calling the cleanup when switching tests in the Browser Mode UI.

Suggested solution

@vitest/runner can expose an onCleanup hook. Consumers of the runner can decide when to run passed down callbacks. By default, Vitest will run those callbacks after each test (when running tests in the Node.js environment). Vitest Browser Mode will also run those hooks after each test, except for the last one, so the UI is still available.

Alternative

No response

Additional context

See #7141 for more details

Validations

@sheremet-va sheremet-va added the p2-nice-to-have Not breaking anything but nice to have (priority) label Jan 15, 2025
@sheremet-va sheremet-va moved this to P2 - 3 in Team Board Jan 15, 2025
@sheremet-va sheremet-va moved this from P2 - 3 to Approved in Team Board Feb 14, 2025
@hi-ogawa
Copy link
Contributor

I think this was another browser mode specific hack for "cleanup".

onBeforeTryTask: VitestRunner['onBeforeTryTask'] = async (...args) => {
await userEvent.cleanup()
await super.onBeforeTryTask?.(...args)
}

I actually don't remember well why we need this in two places, but there was something tricky with resetting playwright instance's state between multiple test files.

// need to cleanup for each tester
// since playwright keyboard API is stateful on page instance level
await userEvent.cleanup()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Status: Approved
Development

No branches or pull requests

2 participants