-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy patherror-handling.spec.js
312 lines (284 loc) · 8.29 KB
/
error-handling.spec.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import Vue from 'vue'
const components = createErrorTestComponents()
describe('Error handling', () => {
// hooks that prevents the component from rendering, but should not
// break parent component
;[
['data', 'data()'],
['render', 'render'],
['beforeCreate', 'beforeCreate hook'],
['created', 'created hook'],
['beforeMount', 'beforeMount hook'],
['directive bind', 'directive foo bind hook'],
['event', 'event handler for "e"']
].forEach(([type, description]) => {
it(`should recover from errors in ${type}`, done => {
const vm = createTestInstance(components[type])
expect(`Error in ${description}`).toHaveBeenWarned()
expect(`Error: ${type}`).toHaveBeenWarned()
assertRootInstanceActive(vm).then(done)
})
})
// error in mounted hook should affect neither child nor parent
it('should recover from errors in mounted hook', done => {
const vm = createTestInstance(components.mounted)
expect(`Error in mounted hook`).toHaveBeenWarned()
expect(`Error: mounted`).toHaveBeenWarned()
assertBothInstancesActive(vm).then(done)
})
// error in beforeUpdate/updated should affect neither child nor parent
;[
['beforeUpdate', 'beforeUpdate hook'],
['updated', 'updated hook'],
['directive update', 'directive foo update hook']
].forEach(([type, description]) => {
it(`should recover from errors in ${type} hook`, done => {
const vm = createTestInstance(components[type])
assertBothInstancesActive(vm).then(() => {
expect(`Error in ${description}`).toHaveBeenWarned()
expect(`Error: ${type}`).toHaveBeenWarned()
}).then(done)
})
})
;[
['beforeDestroy', 'beforeDestroy hook'],
['destroyed', 'destroyed hook'],
['directive unbind', 'directive foo unbind hook']
].forEach(([type, description]) => {
it(`should recover from errors in ${type} hook`, done => {
const vm = createTestInstance(components[type])
vm.ok = false
waitForUpdate(() => {
expect(`Error in ${description}`).toHaveBeenWarned()
expect(`Error: ${type}`).toHaveBeenWarned()
}).thenWaitFor(next => {
assertRootInstanceActive(vm).end(next)
}).then(done)
})
})
it('should recover from errors in user watcher getter', done => {
const vm = createTestInstance(components.userWatcherGetter)
vm.n++
waitForUpdate(() => {
expect(`Error in getter for watcher`).toHaveBeenWarned()
function getErrorMsg () {
try {
this.a.b.c
} catch (e) {
return e.toString()
}
}
const msg = getErrorMsg.call(vm)
expect(msg).toHaveBeenWarned()
}).thenWaitFor(next => {
assertBothInstancesActive(vm).end(next)
}).then(done)
})
it('should recover from errors in user watcher callback', done => {
const vm = createTestInstance(components.userWatcherCallback)
vm.n++
waitForUpdate(() => {
expect(`Error in callback for watcher "n"`).toHaveBeenWarned()
expect(`Error: userWatcherCallback`).toHaveBeenWarned()
}).thenWaitFor(next => {
assertBothInstancesActive(vm).end(next)
}).then(done)
})
it('should recover from errors in user immediate watcher callback', done => {
const vm = createTestInstance(components.userImmediateWatcherCallback)
waitForUpdate(() => {
expect(`Error in callback for immediate watcher "n"`).toHaveBeenWarned()
expect(`Error: userImmediateWatcherCallback error`).toHaveBeenWarned()
}).thenWaitFor(next => {
assertBothInstancesActive(vm).end(next)
}).then(done)
})
it('config.errorHandler should capture render errors', done => {
const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
const vm = createTestInstance(components.render)
const args = spy.calls.argsFor(0)
expect(args[0].toString()).toContain('Error: render') // error
expect(args[1]).toBe(vm.$refs.child) // vm
expect(args[2]).toContain('render') // description
assertRootInstanceActive(vm).then(() => {
Vue.config.errorHandler = null
}).then(done)
})
it('should capture and recover from nextTick errors', done => {
const err1 = new Error('nextTick')
const err2 = new Error('nextTick2')
const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
Vue.nextTick(() => { throw err1 })
Vue.nextTick(() => {
expect(spy).toHaveBeenCalledWith(err1, undefined, 'nextTick')
const vm = new Vue()
vm.$nextTick(() => { throw err2 })
Vue.nextTick(() => {
// should be called with correct instance info
expect(spy).toHaveBeenCalledWith(err2, vm, 'nextTick')
Vue.config.errorHandler = null
done()
})
})
})
it('should recover from errors thrown in errorHandler itself', () => {
Vue.config.errorHandler = () => {
throw new Error('error in errorHandler ¯\\_(ツ)_/¯')
}
const vm = new Vue({
render (h) {
throw new Error('error in render')
},
renderError (h, err) {
return h('div', err.toString())
}
}).$mount()
expect('error in errorHandler').toHaveBeenWarned()
expect('error in render').toHaveBeenWarned()
expect(vm.$el.textContent).toContain('error in render')
Vue.config.errorHandler = null
})
})
function createErrorTestComponents () {
const components = {}
// data
components.data = {
data () {
throw new Error('data')
},
render (h) {
return h('div')
}
}
// render error
components.render = {
render (h) {
throw new Error('render')
}
}
// lifecycle errors
;['create', 'mount', 'update', 'destroy'].forEach(hook => {
// before
const before = 'before' + hook.charAt(0).toUpperCase() + hook.slice(1)
const beforeComp = components[before] = {
props: ['n'],
render (h) {
return h('div', this.n)
}
}
beforeComp[before] = function () {
throw new Error(before)
}
// after
const after = hook.replace(/e?$/, 'ed')
const afterComp = components[after] = {
props: ['n'],
render (h) {
return h('div', this.n)
}
}
afterComp[after] = function () {
throw new Error(after)
}
})
// directive hooks errors
;['bind', 'update', 'unbind'].forEach(hook => {
const key = 'directive ' + hook
const dirComp = components[key] = {
props: ['n'],
template: `<div v-foo="n">{{ n }}</div>`
}
const dirFoo = {}
dirFoo[hook] = function () {
throw new Error(key)
}
dirComp.directives = {
foo: dirFoo
}
})
// user watcher
components.userWatcherGetter = {
props: ['n'],
created () {
this.$watch(function () {
return this.n + this.a.b.c
}, val => {
console.log('user watcher fired: ' + val)
})
},
render (h) {
return h('div', this.n)
}
}
components.userWatcherCallback = {
props: ['n'],
watch: {
n () {
throw new Error('userWatcherCallback error')
}
},
render (h) {
return h('div', this.n)
}
}
components.userImmediateWatcherCallback = {
props: ['n'],
watch: {
n: {
immediate: true,
handler () {
throw new Error('userImmediateWatcherCallback error')
}
}
},
render (h) {
return h('div', this.n)
}
}
// event errors
components.event = {
beforeCreate () {
this.$on('e', () => { throw new Error('event') })
},
mounted () {
this.$emit('e')
},
render (h) {
return h('div')
}
}
return components
}
function createTestInstance (Comp) {
return new Vue({
data: {
n: 0,
ok: true
},
render (h) {
return h('div', [
'n:' + this.n + '\n',
this.ok
? h(Comp, { ref: 'child', props: { n: this.n }})
: null
])
}
}).$mount()
}
function assertRootInstanceActive (vm, chain) {
expect(vm.$el.innerHTML).toContain('n:0\n')
vm.n++
return waitForUpdate(() => {
expect(vm.$el.innerHTML).toContain('n:1\n')
})
}
function assertBothInstancesActive (vm) {
vm.n = 0
return waitForUpdate(() => {
expect(vm.$refs.child.$el.innerHTML).toContain('0')
}).thenWaitFor(next => {
assertRootInstanceActive(vm).then(() => {
expect(vm.$refs.child.$el.innerHTML).toContain('1')
}).end(next)
})
}