Skip to content

Set _error on vm before we rethrow it in error handler in order to ca… #170

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

Merged
merged 1 commit into from
Nov 11, 2017
Merged
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
21 changes: 10 additions & 11 deletions src/lib/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
function errorMessage (msg, info) {
function errorMessage (message, info) {
if (info) {
return `${msg} : additional info ${info}`
return `${message} : additional info ${info}`
}

return msg
return message
}

export default function errorHandler (err, _vm, info) {
if ((typeof err === 'object') && err.message) {
if (info) {
err.message = errorMessage(err.message, info)
}
export default function errorHandler (errorOrString, vm, info) {
const error = (typeof errorOrString === 'object')
? errorOrString
: new Error(errorOrString)

throw err
}
error.message = errorMessage(error.message, info)
vm._error = error

throw new Error(errorMessage(err, info))
throw error
}
4 changes: 4 additions & 0 deletions src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ export default function mount (component: Component, options: Options = {}): Vue
vm.$mount()
}

if (vm._error) {
throw (vm._error)
}

return new VueWrapper(vm, { attachedToDocument: !!options.attachToDocument })
}
20 changes: 17 additions & 3 deletions test/unit/specs/lib/error-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ describe('errorHandler', () => {
const errorString = 'errorString'
const info = 'additional info provided by vue'
const errorObject = new Error(errorString)

it('when error object: rethrows error', () => {
expect(() => errorHandler(errorObject)).to.throw().with.property('message', errorString)
expect(() => errorHandler(errorObject, {})).to.throw().with.property('message', errorString)
})

it('when error object: rethrown error contains vue info when provided', () => {
Expand All @@ -17,8 +16,15 @@ describe('errorHandler', () => {
})
})

it('when error object: sets vm_error to the error that is thrown', () => {
const vm = {}
expect(() => errorHandler(errorObject, vm, info)).to.throw().that.satisfies(function (err) {
return err === vm._error
})
})

it('when error string: throws error with string', () => {
expect(() => errorHandler(errorString)).to.throw().with.property('message', errorString)
expect(() => errorHandler(errorString, {})).to.throw().with.property('message', errorString)
})

it('throws error with string and appends info when provided', () => {
Expand All @@ -28,4 +34,12 @@ describe('errorHandler', () => {
return errorMessage.includes(errorString) && errorMessage.includes(info)
})
})

it('when error string: sets vm_error to the error that is thrown', () => {
const vm = {}

expect(() => errorHandler(errorObject, vm, info)).to.throw().that.satisfies(function (err) {
return err === vm._error
})
})
})
3 changes: 2 additions & 1 deletion test/unit/specs/mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ describe('mount', () => {
expect(wrapper.vm.$options.listeners).to.equal(undefined)
})

it.skip('throws an error when the component fails to mount', () => {
it('propagates errors when they are thrown', () => {
const TestComponent = {
template: '<div></div>',
mounted: function () {
throw new Error('Error in mounted')
}
}

const fn = () => mount(TestComponent)
expect(fn).to.throw()
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/specs/shallow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('shallow', () => {
expect(info.called).to.equal(false)
})

it.skip('throws an error when the component fails to mount', () => {
it('throws an error when the component fails to mount', () => {
expect(() => shallow({
template: '<div></div>',
mounted: function () {
Expand Down