Skip to content

Introduce autoDestroy config option #1240

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

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 47 additions & 0 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,50 @@ import { config } from '@vue/test-utils'

config.silent = false
```

### `autoDestroy`

- type: `boolean | Function`
- default: `false`

This allows you to call `wrapper.destroy()` automatically—either when creating a new wrapper or by passing a hook function.

Please note that setting `autoDestroy: true` only destroys existing instances when a new instance is created which result in one wrapper instance remaining.
Therefore passing a hook function is usually a better idea.

Examples:

```js
import { config, mount } from '@vue/test-utils'

config.autoDestroy = true

const Component = {
template: '<div>come ponente</div>'
}

const firstWrapper = mount(Component)
expect(firstWrapper.text()).not.toBe('come levant')

const secondWrapper = mount(Component) // this will call firstWrapper.destroy()
expect(firstWrapper.text()).not.toBe('come sirocco')
```

```js
import { config, mount } from '@vue/test-utils'

config.autoDestroy = afterEach // will call wrapper.destroy() after each test case

const Component = {
template: '<div>come ponente</div>'
}

describe('my component', () => {
it('is not East wind', () => {
const wrapper = mount(Component)
expect(wrapper.text()).not.toBe('levant')

// wrapper.destroy() is called after this
})
})
```
3 changes: 2 additions & 1 deletion flow/config.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ declare type Config = {
mocks?: Object,
methods?: { [name: string]: Function },
provide?: Object,
silent?: boolean
silent?: boolean,
autoDestroy?: boolean | Function
}
3 changes: 2 additions & 1 deletion packages/test-utils/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export default {
mocks: {},
methods: {},
provide: {},
silent: true
silent: true,
autoDestroy: false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

false should be the default because that is the current behavior

}
18 changes: 18 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import { matches } from './matches'
import createDOMEvent from './create-dom-event'
import { throwIfInstancesThrew } from './error'

const wrapperInstances = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs to be an array because when passing a hook function it can happen that multiple wrapper instances were created.


const destroyAllInstances = () => {
wrapperInstances.forEach(wrapper => wrapper.destroy())
wrapperInstances.length = 0
}

export default class Wrapper implements BaseWrapper {
+vnode: VNode | null
+vm: Component | void
Expand Down Expand Up @@ -67,6 +74,17 @@ export default class Wrapper implements BaseWrapper {
) {
this.isFunctionalComponent = true
}

const { autoDestroy } = config
Copy link
Contributor Author

Choose a reason for hiding this comment

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

do we want to allow overriding this via wrapper options?

if (autoDestroy) {
if (autoDestroy instanceof Function) {
autoDestroy(destroyAllInstances)
Copy link
Contributor Author

@winniehell winniehell May 22, 2019

Choose a reason for hiding this comment

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

This line is wrong. It will for example result in describe > it > shallowMount > new Wrapper > afterEach. That way we have a lot of afterEach calls inside its while we only want a single one outside.

} else {
destroyAllInstances()
}

wrapperInstances.push(this)
}
}

at(): void {
Expand Down
41 changes: 41 additions & 0 deletions test/specs/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describeWithShallowAndMount } from '~resources/utils'
import Component from '~resources/components/component.vue'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import { itDoNotRunIf } from 'conditional-specs'
import { config, createLocalVue } from '~vue/test-utils'
Expand Down Expand Up @@ -109,4 +110,44 @@ describeWithShallowAndMount('config', mountingMethod => {
})
expect(console.error).calledWith(sandbox.match('[Vue warn]'))
})

describe('autoDestroy', () => {
it('does not destroy wrapper when autoDestroy is set to false', () => {
config.autoDestroy = false
const localVue = createLocalVue()
const wrapper = mountingMethod(Component, { localVue })
sandbox.spy(wrapper, 'destroy')

mountingMethod(Component, { localVue })

expect(wrapper.destroy).not.called
})

it('destroys wrapper when autoDestroy is set to true', () => {
config.autoDestroy = true
const localVue = createLocalVue()
const wrapper = mountingMethod(Component, { localVue })
sandbox.spy(wrapper, 'destroy')

mountingMethod(Component, { localVue })

expect(wrapper.destroy).called
})

it('destroys wrapper when autoDestroy hook is called', () => {
let destroyCallback
config.autoDestroy = callback => {
destroyCallback = callback
}
const localVue = createLocalVue()
const wrapper = mountingMethod(Component, { localVue })
sandbox.spy(wrapper, 'destroy')

mountingMethod(Component, { localVue })

expect(wrapper.destroy).not.called
destroyCallback()
expect(wrapper.destroy).called
})
})
})