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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/__tests__/cleanup-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {render, cleanup} from '@testing-library/vue'
import Vue from 'vue'

test('cleanup re-throws errors from async lifecycle hooks', async () => {
const err = new Error('foo')
render({
async mounted() {
await new Promise((resolve, reject) => reject(err))
},
template: `<h1>Hello World</h1>`,
})
// thrown errors are logged redundantly by vue-test-utils injected Vue.config.errorHandler
// mute console
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})

await Vue.nextTick()
expect(cleanup).toThrow(err)

// unmute console
spy.mockReset()
})
8 changes: 5 additions & 3 deletions src/vue-testing-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ function cleanupAtWrapper(wrapper) {
document.body.removeChild(wrapper.element.parentNode)
}

wrapper.destroy()

mountedWrappers.delete(wrapper)
try {
wrapper.destroy()
} finally {
mountedWrappers.delete(wrapper)
}
}

// Vue Testing Library's version of fireEvent will call DOM Testing Library's
Expand Down