Skip to content

Commit

Permalink
fix: prevent auto-cleanup to throw when asserting errors in async lif…
Browse files Browse the repository at this point in the history
…ecycle hooks (#142)

* fix: delete wrappers before destroying

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

* test: add tests for re-throwing async lifecycle hooks

* Update src/__tests__/cleanup-throw.js

Co-authored-by: Adrià Fontcuberta <[email protected]>
  • Loading branch information
dlindenkreuz and afontcu authored Jun 6, 2020
1 parent b54744a commit 4c5f724
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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

0 comments on commit 4c5f724

Please sign in to comment.