From eb893c8933d45252ff995ef2d0acf0fa86d133b4 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 31 May 2018 21:39:44 +0100 Subject: [PATCH 1/4] Remove error handler after mount --- packages/test-utils/src/mount.js | 7 ++++++- test/specs/mount.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/test-utils/src/mount.js b/packages/test-utils/src/mount.js index b1891195f..a345e6076 100644 --- a/packages/test-utils/src/mount.js +++ b/packages/test-utils/src/mount.js @@ -15,10 +15,13 @@ import warnIfNoWindow from './warn-if-no-window' Vue.config.productionTip = false Vue.config.devtools = false -Vue.config.errorHandler = errorHandler export default function mount (component: Component, options: Options = {}): VueWrapper { + const existingErrorHandler = Vue.config.errorHandler + Vue.config.errorHandler = errorHandler + warnIfNoWindow() + // Remove cached constructor delete component._Ctor const vueClass = options.localVue || createLocalVue() @@ -35,6 +38,8 @@ export default function mount (component: Component, options: Options = {}): Vue throw (componentsWithError[0]._error) } + Vue.config.errorHandler = existingErrorHandler + const wrapperOptions = { attachedToDocument: !!options.attachToDocument, sync: !!((options.sync || options.sync === undefined)) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 1f1061bb6..86b978ed7 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -235,6 +235,37 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(fn).to.throw('Error in mounted') }) + it.only('logs errors once after mount and restores user error handler', (done) => { + const TestComponent = { + template: '
', + updated: function () { + throw new Error('Error in updated') + } + } + + const wrapper = mount(TestComponent, { + sync: false + }) + wrapper.vm.$forceUpdate() + setTimeout(() => { + expect(consoleError).calledTwice + done() + }) + }) + + it.only('restores user error handler after mount', () => { + const existingErrorHandler = () => {} + Vue.config.errorHandler = existingErrorHandler + console.log(Vue.config.errorHandler) + const TestComponent = { + template: '
' + } + + mount(TestComponent) + + expect(Vue.config.errorHandler).to.equal(existingErrorHandler) + }) + it('overwrites the component options with the options other than the mounting options when the options for mount contain those', () => { const Component = { template: '
{{ foo() }}{{ bar() }}{{ baz() }}
', From 79d9f14b3659b026428216daa17786b4c2f75e71 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Fri, 1 Jun 2018 07:16:09 +0100 Subject: [PATCH 2/4] test: fix assertion for Vue < 2.1 --- test/specs/mount.spec.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 86b978ed7..9f198193e 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -235,7 +235,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(fn).to.throw('Error in mounted') }) - it.only('logs errors once after mount and restores user error handler', (done) => { + it('logs errors once after mount and restores user error handler', (done) => { const TestComponent = { template: '
', updated: function () { @@ -248,12 +248,14 @@ describeRunIf(process.env.TEST_ENV !== 'node', }) wrapper.vm.$forceUpdate() setTimeout(() => { - expect(consoleError).calledTwice + vueVersion > 2.1 + ? expect(consoleError).calledTwice + : expect(consoleError).calledOnce done() }) }) - it.only('restores user error handler after mount', () => { + it('restores user error handler after mount', () => { const existingErrorHandler = () => {} Vue.config.errorHandler = existingErrorHandler console.log(Vue.config.errorHandler) From ad26a2db1ddb240c0da2f01bc6155a7e11f7a29f Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Fri, 1 Jun 2018 20:00:49 +0100 Subject: [PATCH 3/4] test: fix leaky error handler --- test/specs/mount.spec.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 9f198193e..19e0f11ff 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -10,16 +10,14 @@ import { itDoNotRunIf } from 'conditional-specs' -describeRunIf(process.env.TEST_ENV !== 'node', +describeRunIf.only(process.env.TEST_ENV !== 'node', 'mount', () => { - let consoleError - beforeEach(() => { - consoleError = sinon.stub(console, 'error') + sinon.stub(console, 'error') }) afterEach(() => { - consoleError.restore() + console.error.restore() }) it('returns new VueWrapper with mounted Vue instance if no options are passed', () => { @@ -156,7 +154,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', } } mount(TestComponent) - expect(consoleError).calledWith(msg) + expect(console.error).calledWith(msg) }) it('deletes mounting options before passing options to component', () => { @@ -235,7 +233,8 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(fn).to.throw('Error in mounted') }) - it('logs errors once after mount and restores user error handler', (done) => { + it.only('logs errors once after mount', (done) => { + Vue.config.errorHandler = null const TestComponent = { template: '
', updated: function () { @@ -249,8 +248,8 @@ describeRunIf(process.env.TEST_ENV !== 'node', wrapper.vm.$forceUpdate() setTimeout(() => { vueVersion > 2.1 - ? expect(consoleError).calledTwice - : expect(consoleError).calledOnce + ? expect(console.error).calledTwice + : expect(console.error).calledOnce done() }) }) @@ -258,13 +257,10 @@ describeRunIf(process.env.TEST_ENV !== 'node', it('restores user error handler after mount', () => { const existingErrorHandler = () => {} Vue.config.errorHandler = existingErrorHandler - console.log(Vue.config.errorHandler) const TestComponent = { template: '
' } - mount(TestComponent) - expect(Vue.config.errorHandler).to.equal(existingErrorHandler) }) From 54151c0792e44f5845f035adc4612a5ee884d08f Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Fri, 1 Jun 2018 21:09:11 +0100 Subject: [PATCH 4/4] test:do not run tests on Vue < 2.2 --- test/specs/mount.spec.js | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 0f9a3db5e..15e40908c 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -227,26 +227,28 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(fn).to.throw('Error in mounted') }) - it('logs errors once after mount', (done) => { - Vue.config.errorHandler = null - const TestComponent = { - template: '
', - updated: function () { - throw new Error('Error in updated') + itDoNotRunIf( + vueVersion < 2.2, + 'logs errors once after mount', (done) => { + Vue.config.errorHandler = null + const TestComponent = { + template: '
', + updated: function () { + throw new Error('Error in updated') + } } - } - const wrapper = mount(TestComponent, { - sync: false - }) - wrapper.vm.$forceUpdate() - setTimeout(() => { - vueVersion > 2.1 - ? expect(console.error).calledTwice - : expect(console.error).calledOnce - done() + const wrapper = mount(TestComponent, { + sync: false + }) + wrapper.vm.$forceUpdate() + setTimeout(() => { + vueVersion > 2.1 + ? expect(console.error).calledTwice + : expect(console.error).calledOnce + done() + }) }) - }) it('restores user error handler after mount', () => { const existingErrorHandler = () => {} @@ -256,6 +258,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', } mount(TestComponent) expect(Vue.config.errorHandler).to.equal(existingErrorHandler) + Vue.config.errorHandler = null }) it('overwrites the component options with the options other than the mounting options when the options for mount contain those', () => {