Skip to content

Fix asserting errors in async lifecycle hooks #142

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

Merged
merged 4 commits into from
Jun 6, 2020
Merged

Fix asserting errors in async lifecycle hooks #142

merged 4 commits into from
Jun 6, 2020

Conversation

dlindenkreuz
Copy link
Contributor

Right now, when an async Vue lifecycle hook such as mounted throws/rejects, it seems that expect(cleanup).toThrow() is the only way to reliably assert an error. When auto-cleanup is enabled, this would mean that cleanup is called multiple times for some tests.

wrapper.destroy, called by cleanup, rethrows any errors (see @vue/test-utils throwIfInstancesThrew). Without try/finally in place, the destroyed wrapper never gets deleted from mountedWrappers when wrapper.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 out console.error temporarily. Feel free to remove this if you're fine with some errors being logged.

`wrapper.destroy()` will rethrow any errors via vue test utils `throwIfInstancesThrew`. ensure that wrappers are deleted from mountedWrappers at any time
@codecov
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #142 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #142   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           69        70    +1     
  Branches        13        13           
=========================================
+ Hits            69        70    +1     
Impacted Files Coverage Δ
src/vue-testing-library.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b54744a...00e68ad. Read the comment docs.

Copy link
Member

@afontcu afontcu left a comment

Choose a reason for hiding this comment

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

Looks good! Nice catch :)

@afontcu afontcu merged commit 4c5f724 into testing-library:master Jun 6, 2020
@afontcu
Copy link
Member

afontcu commented Jun 6, 2020

🎉 This PR is included in version 5.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants