Skip to content

Commit 69dd594

Browse files
committed
chore: Expose resetAutoDestroyState()
1 parent 560b6ec commit 69dd594

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

packages/test-utils/src/auto-destroy.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
import { throwError } from 'shared/util'
44
import Wrapper from './wrapper'
55

6-
let wrapperInstances: Array<Wrapper>
6+
let isEnabled = false
7+
const wrapperInstances = []
8+
9+
export function resetAutoDestroyState() {
10+
isEnabled = false
11+
wrapperInstances.length = 0
12+
}
713

814
export function enableAutoDestroy(hook: (() => void) => void) {
9-
if (wrapperInstances) {
15+
if (isEnabled) {
1016
throwError('enableAutoDestroy cannot be called more than once')
1117
}
1218

13-
wrapperInstances = []
19+
isEnabled = true
1420

1521
hook(() => {
1622
wrapperInstances.forEach((wrapper: Wrapper) => {
@@ -19,12 +25,13 @@ export function enableAutoDestroy(hook: (() => void) => void) {
1925

2026
wrapper.destroy()
2127
})
22-
wrapperInstances = []
28+
29+
wrapperInstances.length = 0
2330
})
2431
}
2532

2633
export function trackInstance(wrapper: Wrapper) {
27-
if (!wrapperInstances) return
34+
if (!isEnabled) return
2835

2936
wrapperInstances.push(wrapper)
3037
}

packages/test-utils/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import shallowMount from './shallow-mount'
22
import mount from './mount'
3-
import { enableAutoDestroy } from './auto-destroy'
3+
import { enableAutoDestroy, resetAutoDestroyState } from './auto-destroy'
44
import createLocalVue from './create-local-vue'
55
import RouterLinkStub from './components/RouterLinkStub'
66
import createWrapper from './create-wrapper'
@@ -23,6 +23,7 @@ export default {
2323
config,
2424
enableAutoDestroy,
2525
mount,
26+
resetAutoDestroyState,
2627
shallow,
2728
shallowMount,
2829
RouterLinkStub,

test/specs/wrapper.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describeWithShallowAndMount } from '~resources/utils'
2-
import { enableAutoDestroy } from '~vue/test-utils'
2+
import { enableAutoDestroy, resetAutoDestroyState } from '~vue/test-utils'
33

44
describeWithShallowAndMount('Wrapper', mountingMethod => {
55
;['vnode', 'element', 'vm', 'options'].forEach(property => {
@@ -20,6 +20,10 @@ describeWithShallowAndMount('Wrapper', mountingMethod => {
2020
describe('enableAutoDestroy', () => {
2121
const sandbox = sinon.createSandbox()
2222

23+
beforeEach(() => {
24+
resetAutoDestroyState()
25+
})
26+
2327
it('calls the hook function', () => {
2428
const hookSpy = sandbox.spy()
2529

0 commit comments

Comments
 (0)