Fix asserting errors in async lifecycle hooks #142
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Right now, when an async Vue lifecycle hook such as
mounted
throws/rejects, it seems thatexpect(cleanup).toThrow()
is the only way to reliably assert an error. When auto-cleanup is enabled, this would mean thatcleanup
is called multiple times for some tests.wrapper.destroy
, called bycleanup
, rethrows any errors (see @vue/test-utilsthrowIfInstancesThrew
). Without try/finally in place, the destroyed wrapper never gets deleted frommountedWrappers
whenwrapper.destroy
throws.The assert works, but the test still fails because auto-cleanup throws in
afterEach
as it attempts to destroy the wrapper again. It would be tedious to turn off auto-cleanup for the entire test setup, just to call cleanup manually for all remaining tests.Note: I added a test that currently creates some
console.error
logs, but none of which affect the test outcome negatively. I currently use jest to stub outconsole.error
temporarily. Feel free to remove this if you're fine with some errors being logged.