forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.js
68 lines (58 loc) · 2.14 KB
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { warn } from 'shared/util'
import { findAllInstances, findAllParentInstances } from './find'
function errorHandler(errorOrString, vm, info) {
const error =
typeof errorOrString === 'object' ? errorOrString : new Error(errorOrString)
// If a user defined errorHandler was register via createLocalVue
// find and call the user defined errorHandler
const instancedErrorHandlers = findAllParentInstances(vm)
.filter(
_vm =>
_vm &&
_vm.$options &&
_vm.$options.localVue &&
_vm.$options.localVue.config &&
_vm.$options.localVue.config.errorHandler
)
.map(_vm => _vm.$options.localVue.config.errorHandler)
if (vm) {
vm._error = error
}
if (!instancedErrorHandlers.length) {
throw error
}
// should be one error handler, as only once can be registered with local vue
// regardless, if more exist (for whatever reason), invoke the other user defined error handlers
instancedErrorHandlers.forEach(instancedErrorHandler => {
instancedErrorHandler(error, vm, info)
})
}
export function throwIfInstancesThrew(vm) {
const instancesWithError = findAllInstances(vm).filter(_vm => _vm._error)
if (instancesWithError.length > 0) {
throw instancesWithError[0]._error
}
}
let hasWarned = false
// Vue swallows errors thrown by instances, even if the global error handler
// throws. In order to throw in the test, we add an _error property to an
// instance when it throws. Then we loop through the instances with
// throwIfInstancesThrew and throw an error in the test context if any
// instances threw.
export function addGlobalErrorHandler(_Vue) {
const existingErrorHandler = _Vue.config.errorHandler
if (existingErrorHandler === errorHandler) {
return
}
if (_Vue.config.errorHandler && !hasWarned) {
warn(
`Global error handler detected (Vue.config.errorHandler). \n` +
`Vue Test Utils sets a custom error handler to throw errors ` +
`thrown by instances. If you want this behavior in ` +
`your tests, you must remove the global error handler.`
)
hasWarned = true
} else {
_Vue.config.errorHandler = errorHandler
}
}