From 076f4042de086419d3738a00b510b641d3f23238 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Sun, 3 Feb 2019 14:51:55 +0000 Subject: [PATCH 1/6] test: use sandbox to avoid memory leaks in test --- test/specs/components/TransitionStub.spec.js | 8 +++---- test/specs/config.spec.js | 13 ++++++----- test/specs/mount.spec.js | 16 ++++++++------ test/specs/mounting-options/mocks.spec.js | 7 +++--- .../mounting-options/scopedSlots.spec.js | 4 +++- test/specs/mounting-options/stubs.spec.js | 10 +++------ test/specs/mounting-options/sync.spec.js | 11 +++++----- test/specs/shallow-mount.spec.js | 9 ++++---- test/specs/wrapper-array.spec.js | 18 +++++++++------ test/specs/wrapper-array/trigger.spec.js | 10 ++++++--- test/specs/wrapper/destroy.spec.js | 17 ++++++++------ test/specs/wrapper/setData.spec.js | 8 +++---- test/specs/wrapper/setProps.spec.js | 9 ++++---- test/specs/wrapper/trigger.spec.js | 22 ++++++++----------- 14 files changed, 87 insertions(+), 75 deletions(-) diff --git a/test/specs/components/TransitionStub.spec.js b/test/specs/components/TransitionStub.spec.js index 17594edab..f30d7d5f1 100644 --- a/test/specs/components/TransitionStub.spec.js +++ b/test/specs/components/TransitionStub.spec.js @@ -3,14 +3,14 @@ import { describeWithShallowAndMount } from '~resources/utils' import { TransitionStub } from '~vue/test-utils' describeWithShallowAndMount('TransitionStub', mountingMethod => { - let consoleError + const sandbox = sinon.createSandbox() beforeEach(() => { - consoleError = sinon.stub(console, 'error') + sandbox.stub(console, 'error').callThrough() }) afterEach(() => { - consoleError.restore() + sandbox.restore() }) it('update synchronously when used as stubs for Transition', () => { @@ -63,7 +63,7 @@ describeWithShallowAndMount('TransitionStub', mountingMethod => { transition: TransitionStub } }) - expect(consoleError).calledWith(msg) + expect(console.error).calledWith(msg) }) it('handles keyed transitions', () => { diff --git a/test/specs/config.spec.js b/test/specs/config.spec.js index 4261aa198..0cfebe3ad 100644 --- a/test/specs/config.spec.js +++ b/test/specs/config.spec.js @@ -9,20 +9,23 @@ import { } from '~vue/test-utils' describeWithShallowAndMount('config', mountingMethod => { - let configStubsSave, configLogSave, configSilentSave + const sandbox = sinon.createSandbox() + let configStubsSave + let configLogSave + let configSilentSave beforeEach(() => { configStubsSave = config.stubs configLogSave = config.logModifiedComponents configSilentSave = config.silent - sinon.stub(console, 'error').callThrough() + sandbox.stub(console, 'error').callThrough() }) afterEach(() => { config.stubs = configStubsSave config.logModifiedComponents = configLogSave config.silent = configSilentSave - console.error.restore() + sandbox.restore() }) itDoNotRunIf( @@ -149,7 +152,7 @@ describeWithShallowAndMount('config', mountingMethod => { wrapper.setProps({ prop1: 'new value' }) - expect(console.error).not.calledWith(sinon.match('[Vue warn]')) + expect(console.error).not.calledWith(sandbox.match('[Vue warn]')) }) it('does throw Vue warning when silent is set to false', () => { @@ -165,6 +168,6 @@ describeWithShallowAndMount('config', mountingMethod => { wrapper.setProps({ prop1: 'new value' }) - expect(console.error).calledWith(sinon.match('[Vue warn]')) + expect(console.error).calledWith(sandbox.match('[Vue warn]')) }) }) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 882457568..a2c3e9682 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -10,15 +10,16 @@ import { describeRunIf, itDoNotRunIf, itSkipIf } from 'conditional-specs' import Vuex from 'vuex' describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { + const sandbox = sinon.createSandbox() const windowSave = window beforeEach(() => { - sinon.stub(console, 'error').callThrough() + sandbox.stub(console, 'error').callThrough() }) afterEach(() => { window = windowSave // eslint-disable-line no-native-reassign - console.error.restore() + sandbox.restore() }) it('returns new VueWrapper with mounted Vue instance if no options are passed', () => { @@ -126,10 +127,11 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { ) it('does not use cached component', () => { - ComponentWithMixin.methods.someMethod = sinon.stub() + sandbox.stub(ComponentWithMixin.methods, 'someMethod') mount(ComponentWithMixin) expect(ComponentWithMixin.methods.someMethod.callCount).to.equal(1) - ComponentWithMixin.methods.someMethod = sinon.stub() + ComponentWithMixin.methods.someMethod.restore() + sandbox.stub(ComponentWithMixin.methods, 'someMethod') mount(ComponentWithMixin) expect(ComponentWithMixin.methods.someMethod.callCount).to.equal(1) }) @@ -160,7 +162,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { }) itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => { - const stub = sinon.stub() + const stub = sandbox.stub() const TestComponent = Vue.extend({ template: '
', methods: { @@ -179,8 +181,8 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { }) it.skip('overrides component prototype', () => { - const mountSpy = sinon.spy() - const destroySpy = sinon.spy() + const mountSpy = sandbox.spy() + const destroySpy = sandbox.spy() const Component = Vue.extend({}) const { $mount: originalMount, diff --git a/test/specs/mounting-options/mocks.spec.js b/test/specs/mounting-options/mocks.spec.js index a3c437ae0..bcd356cc5 100644 --- a/test/specs/mounting-options/mocks.spec.js +++ b/test/specs/mounting-options/mocks.spec.js @@ -6,17 +6,18 @@ import { describeWithMountingMethods, vueVersion } from '~resources/utils' import { itDoNotRunIf, itSkipIf, itRunIf } from 'conditional-specs' describeWithMountingMethods('options.mocks', mountingMethod => { + const sandbox = sinon.createSandbox() let configMocksSave beforeEach(() => { configMocksSave = config.mocks config.mocks = {} - sinon.stub(console, 'error') + sandbox.stub(console, 'error').callThrough() }) afterEach(() => { config.mocks = configMocksSave - console.error.restore() + sandbox.restore() }) it('adds variables to vm when passed', () => { @@ -69,7 +70,7 @@ describeWithMountingMethods('options.mocks', mountingMethod => { mountingMethod.name === 'renderToString', 'adds variables as reactive properties to vm when passed', () => { - const stub = sinon.stub() + const stub = sandbox.stub() const $reactiveMock = { value: 'value' } const wrapper = mountingMethod( { diff --git a/test/specs/mounting-options/scopedSlots.spec.js b/test/specs/mounting-options/scopedSlots.spec.js index c65d1cb38..7b34febc4 100644 --- a/test/specs/mounting-options/scopedSlots.spec.js +++ b/test/specs/mounting-options/scopedSlots.spec.js @@ -4,10 +4,12 @@ import ComponentWithScopedSlots from '~resources/components/component-with-scope import { itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('scopedSlots', mountingMethod => { + const sandbox = sinon.createSandbox() const windowSave = window afterEach(() => { window = windowSave // eslint-disable-line no-native-reassign + sandbox.restore() }) itDoNotRunIf(vueVersion < 2.1, 'handles templates as the root node', () => { @@ -248,7 +250,7 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => { return this.$scopedSlots.default(this.val) } } - const stub = sinon.stub() + const stub = sandbox.stub() mountingMethod(TestComponent, { scopedSlots: { default: stub diff --git a/test/specs/mounting-options/stubs.spec.js b/test/specs/mounting-options/stubs.spec.js index 0c0cd1996..9b0d59a4e 100644 --- a/test/specs/mounting-options/stubs.spec.js +++ b/test/specs/mounting-options/stubs.spec.js @@ -9,14 +9,11 @@ import { describeWithMountingMethods, vueVersion } from '~resources/utils' import { itDoNotRunIf, itSkipIf, itRunIf } from 'conditional-specs' describeWithMountingMethods('options.stub', mountingMethod => { - let info - let warn + const sandbox = sinon.createSandbox() let configStubsSave let serverConfigSave beforeEach(() => { - info = sinon.stub(console, 'info') - warn = sinon.stub(console, 'error') configStubsSave = config.stubs serverConfigSave = serverConfig.stubs config.stubs = {} @@ -24,8 +21,7 @@ describeWithMountingMethods('options.stub', mountingMethod => { }) afterEach(() => { - info.restore() - warn.restore() + sandbox.restore() config.stubs = configStubsSave serverConfig.stubs = serverConfigSave }) @@ -64,7 +60,7 @@ describeWithMountingMethods('options.stub', mountingMethod => { mountingMethod.name === 'renderToString', 'replaces component with a component', () => { - const mounted = sinon.stub() + const mounted = sandbox.stub() const Stub = { template: '
', mounted diff --git a/test/specs/mounting-options/sync.spec.js b/test/specs/mounting-options/sync.spec.js index 385b2b7fe..b0395caa6 100644 --- a/test/specs/mounting-options/sync.spec.js +++ b/test/specs/mounting-options/sync.spec.js @@ -1,14 +1,15 @@ -import sinon from 'sinon' import { describeWithShallowAndMount, vueVersion } from '~resources/utils' import { itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('options.sync', mountingMethod => { + const sandbox = sinon.createSandbox() + beforeEach(() => { - sinon.stub(console, 'error').callThrough() + sandbox.stub(console, 'error').callThrough() }) afterEach(() => { - console.error.restore() + sandbox.restore() }) it('sets watchers to sync if set to true', () => { @@ -124,7 +125,7 @@ describeWithShallowAndMount('options.sync', mountingMethod => { }) it('call updated when sync is not false', () => { - const childComponentSpy = sinon.stub() + const childComponentSpy = sandbox.stub() const ChildComponent = { template: '
{{ foo }}
', props: ['foo'], @@ -132,7 +133,7 @@ describeWithShallowAndMount('options.sync', mountingMethod => { childComponentSpy() } } - const spy = sinon.stub() + const spy = sandbox.stub() const TestComponent = { template: '
{{ foo }}
', data() { diff --git a/test/specs/shallow-mount.spec.js b/test/specs/shallow-mount.spec.js index df07a232b..e0b5d888c 100644 --- a/test/specs/shallow-mount.spec.js +++ b/test/specs/shallow-mount.spec.js @@ -12,9 +12,10 @@ import { vueVersion } from '~resources/utils' import { describeRunIf, itDoNotRunIf } from 'conditional-specs' describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => { + const sandbox = sinon.createSandbox() beforeEach(() => { - sinon.stub(console, 'info').callThrough() - sinon.stub(console, 'error').callThrough() + sandbox.stub(console, 'info').callThrough() + sandbox.stub(console, 'error').callThrough() }) afterEach(() => { @@ -149,7 +150,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => { } } shallowMount(TestComponent) - expect(console.error).not.calledWith(sinon.match('[Vue warn]')) + expect(console.error).not.calledWith(sandbox.match('[Vue warn]')) } ) @@ -549,7 +550,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => { shallowMount(TestComponent) mount(TestComponent) expect(console.error).not.calledWith( - sinon.match('Unknown custom element') + sandbox.match('Unknown custom element') ) } ) diff --git a/test/specs/wrapper-array.spec.js b/test/specs/wrapper-array.spec.js index a2d60ad3f..45c31ec8b 100644 --- a/test/specs/wrapper-array.spec.js +++ b/test/specs/wrapper-array.spec.js @@ -2,6 +2,10 @@ import { Wrapper, WrapperArray } from '~vue/test-utils' import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('WrapperArray', mountingMethod => { + const sandbox = sinon.createSandbox() + + afterEach(sandbox.restore) + function getWrapperArray(wrappers) { if (!wrappers) { wrappers = [1, 2, 3].map(v => { @@ -142,7 +146,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { it('contains returns true if every wrapper.contains() returns true', () => { const selector = 'selector' - const contains = sinon.stub() + const contains = sandbox.stub() contains.withArgs(selector).returns(true) const wrapperArray = getWrapperArray([{ contains }, { contains }]) expect(wrapperArray.contains(selector)).to.equal(true) @@ -158,7 +162,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { it('is returns true if every wrapper.is() returns true', () => { const selector = 'selector' - const is = sinon.stub() + const is = sandbox.stub() is.withArgs(selector).returns(true) const wrapperArray = getWrapperArray([{ is }, { is }]) expect(wrapperArray.is(selector)).to.equal(true) @@ -221,7 +225,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { }) it('setMethods calls setMethods on each wrapper', () => { - const setMethods = sinon.stub() + const setMethods = sandbox.stub() const methods = {} const wrapperArray = getWrapperArray([{ setMethods }, { setMethods }]) wrapperArray.setMethods(methods) @@ -230,7 +234,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { }) it('setData calls setData on each wrapper', () => { - const setData = sinon.stub() + const setData = sandbox.stub() const data = {} const wrapperArray = getWrapperArray([{ setData }, { setData }]) wrapperArray.setData(data) @@ -239,7 +243,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { }) it('setProps calls setProps on each wrapper', () => { - const setProps = sinon.stub() + const setProps = sandbox.stub() const props = {} const wrapperArray = getWrapperArray([{ setProps }, { setProps }]) wrapperArray.setProps(props) @@ -248,7 +252,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { }) it('trigger calls trigger on each wrapper', () => { - const trigger = sinon.stub() + const trigger = sandbox.stub() const event = 'click' const options = {} const wrapperArray = getWrapperArray([{ trigger }, { trigger }]) @@ -258,7 +262,7 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => { }) it('destroy calls destroy on each wrapper', () => { - const destroy = sinon.stub() + const destroy = sandbox.stub() const wrapperArray = getWrapperArray([{ destroy }, { destroy }]) wrapperArray.destroy() expect(destroy.calledTwice).to.equal(true) diff --git a/test/specs/wrapper-array/trigger.spec.js b/test/specs/wrapper-array/trigger.spec.js index 90b050f61..f25720f53 100644 --- a/test/specs/wrapper-array/trigger.spec.js +++ b/test/specs/wrapper-array/trigger.spec.js @@ -3,8 +3,12 @@ import ComponentWithEvents from '~resources/components/component-with-events.vue import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('trigger', mountingMethod => { + const sandbox = sinon.createSandbox() + + afterEach(sandbox.restore) + it('causes click handler to fire when wrapper.trigger("click") is called on a Component', () => { - const clickHandler = sinon.stub() + const clickHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { clickHandler } }) @@ -15,7 +19,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('causes keydown handler to fire when wrapper.trigger("keydown") is fired on a Component', () => { - const keydownHandler = sinon.stub() + const keydownHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { keydownHandler } }) @@ -25,7 +29,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', () => { - const keydownHandler = sinon.stub() + const keydownHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { keydownHandler } }) diff --git a/test/specs/wrapper/destroy.spec.js b/test/specs/wrapper/destroy.spec.js index e9f229c05..5e2283e4d 100644 --- a/test/specs/wrapper/destroy.spec.js +++ b/test/specs/wrapper/destroy.spec.js @@ -1,27 +1,30 @@ import { describeWithShallowAndMount } from '~resources/utils' -import sinon from 'sinon' describeWithShallowAndMount('destroy', mountingMethod => { + const sandbox = sinon.createSandbox() + + afterEach(sandbox.restore) + it('triggers beforeDestroy ', () => { - const spy = sinon.stub() + const stub = sandbox.stub() mountingMethod({ render: () => {}, beforeDestroy() { - spy() + stub() } }).destroy() - expect(spy.calledOnce).to.equal(true) + expect(stub.calledOnce).to.equal(true) }) it('triggers destroy ', () => { - const spy = sinon.stub() + const stub = sandbox.stub() mountingMethod({ render: () => {}, destroyed() { - spy() + stub() } }).destroy() - expect(spy.calledOnce).to.equal(true) + expect(stub.calledOnce).to.equal(true) }) it('removes element from document.body', () => { diff --git a/test/specs/wrapper/setData.spec.js b/test/specs/wrapper/setData.spec.js index ae6a6cf42..ca8ac2f53 100644 --- a/test/specs/wrapper/setData.spec.js +++ b/test/specs/wrapper/setData.spec.js @@ -4,14 +4,14 @@ import ComponentWithWatch from '~resources/components/component-with-watch.vue' import { describeWithShallowAndMount, vueVersion } from '~resources/utils' describeWithShallowAndMount('setData', mountingMethod => { - let info + const sandbox = sinon.createSandbox() beforeEach(() => { - info = sinon.stub(console, 'info') + sandbox.stub(console, 'info').callThrough() }) afterEach(() => { - info.restore() + sandbox.restore() }) it('sets component data and updates nested vm nodes when called on Vue instance', () => { @@ -47,7 +47,7 @@ describeWithShallowAndMount('setData', mountingMethod => { const wrapper = mountingMethod(ComponentWithWatch) const data1 = 'testest' wrapper.setData({ data2: 'newProp', data1 }) - expect(info.args[1][0]).to.equal(data1) + expect(console.info.args[1][0]).to.equal(data1) }) it('throws error if node is not a Vue instance', () => { diff --git a/test/specs/wrapper/setProps.spec.js b/test/specs/wrapper/setProps.spec.js index 3cd09a37f..e3ed38923 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -5,14 +5,13 @@ import { describeWithShallowAndMount, vueVersion } from '~resources/utils' import { itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('setProps', mountingMethod => { - let info - + const sandbox = sinon.createSandbox() beforeEach(() => { - info = sinon.stub(console, 'info') + sandbox.stub(console, 'info').callThrough() }) afterEach(() => { - info.restore() + sandbox.restore() }) it('sets component props and updates DOM when called on Vue instance', () => { @@ -121,7 +120,7 @@ describeWithShallowAndMount('setProps', mountingMethod => { const wrapper = mountingMethod(ComponentWithWatch) const prop1 = 'testest' wrapper.setProps({ prop2: 'newProp', prop1 }) - expect(info.args[1][0]).to.equal(prop1) + expect(console.info.args[1][0]).to.equal(prop1) }) it('should not run watchers if prop updated is null', () => { diff --git a/test/specs/wrapper/trigger.spec.js b/test/specs/wrapper/trigger.spec.js index 1859fa912..9406fc967 100644 --- a/test/specs/wrapper/trigger.spec.js +++ b/test/specs/wrapper/trigger.spec.js @@ -9,18 +9,14 @@ import Vue from 'vue' import { itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('trigger', mountingMethod => { - let info - - beforeEach(() => { - info = sinon.stub(console, 'info') - }) + const sandbox = sinon.createSandbox() afterEach(() => { - info.restore() + sandbox.restore() }) it('causes click handler to fire when wrapper.trigger("click") is called on a Component', () => { - const clickHandler = sinon.stub() + const clickHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { clickHandler } }) @@ -31,7 +27,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('causes keydown handler to fire when wrapper.trigger("keydown") is fired on a Component', () => { - const keydownHandler = sinon.stub() + const keydownHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { keydownHandler } }) @@ -41,7 +37,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', () => { - const keydownHandler = sinon.stub() + const keydownHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { keydownHandler } }) @@ -68,7 +64,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { pageup: 33, pagedown: 34 } - const keyupHandler = sinon.stub() + const keyupHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { keyupHandler } }) @@ -88,7 +84,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('adds options to event', () => { - const clickHandler = sinon.stub() + const clickHandler = sandbox.stub() const wrapper = mountingMethod(ComponentWithEvents, { propsData: { clickHandler } }) @@ -103,7 +99,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('adds custom data to events', () => { - const stub = sinon.stub() + const stub = sandbox.stub() const TestComponent = { template: '
', methods: { @@ -123,7 +119,7 @@ describeWithShallowAndMount('trigger', mountingMethod => { }) it('does not fire on disabled elements', () => { - const clickHandler = sinon.stub() + const clickHandler = sandbox.stub() const TestComponent = { template: '