error-handling.spec.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import Vue from 'vue'
  2. const components = createErrorTestComponents()
  3. describe('Error handling', () => {
  4. // hooks that prevents the component from rendering, but should not
  5. // break parent component
  6. ;[
  7. ['data', 'data()'],
  8. ['render', 'render function'],
  9. ['beforeCreate', 'beforeCreate hook'],
  10. ['created', 'created hook'],
  11. ['beforeMount', 'beforeMount hook'],
  12. ['directive bind', 'directive foo bind hook'],
  13. ['event', 'event handler for "e"']
  14. ].forEach(([type, description]) => {
  15. it(`should recover from errors in ${type}`, done => {
  16. const vm = createTestInstance(components[type])
  17. expect(`Error in ${description}`).toHaveBeenWarned()
  18. expect(`Error: ${type}`).toHaveBeenWarned()
  19. assertRootInstanceActive(vm).then(done)
  20. })
  21. })
  22. // error in mounted hook should affect neither child nor parent
  23. it('should recover from errors in mounted hook', done => {
  24. const vm = createTestInstance(components.mounted)
  25. expect(`Error in mounted hook`).toHaveBeenWarned()
  26. expect(`Error: mounted`).toHaveBeenWarned()
  27. assertBothInstancesActive(vm).then(done)
  28. })
  29. // error in beforeUpdate/updated should affect neither child nor parent
  30. ;[
  31. ['beforeUpdate', 'beforeUpdate hook'],
  32. ['updated', 'updated hook'],
  33. ['directive update', 'directive foo update hook']
  34. ].forEach(([type, description]) => {
  35. it(`should recover from errors in ${type} hook`, done => {
  36. const vm = createTestInstance(components[type])
  37. assertBothInstancesActive(vm).then(() => {
  38. expect(`Error in ${description}`).toHaveBeenWarned()
  39. expect(`Error: ${type}`).toHaveBeenWarned()
  40. }).then(done)
  41. })
  42. })
  43. ;[
  44. ['beforeDestroy', 'beforeDestroy hook'],
  45. ['destroyed', 'destroyed hook'],
  46. ['directive unbind', 'directive foo unbind hook']
  47. ].forEach(([type, description]) => {
  48. it(`should recover from errors in ${type} hook`, done => {
  49. const vm = createTestInstance(components[type])
  50. vm.ok = false
  51. waitForUpdate(() => {
  52. expect(`Error in ${description}`).toHaveBeenWarned()
  53. expect(`Error: ${type}`).toHaveBeenWarned()
  54. }).thenWaitFor(next => {
  55. assertRootInstanceActive(vm).end(next)
  56. }).then(done)
  57. })
  58. })
  59. it('should recover from errors in user watcher getter', done => {
  60. const vm = createTestInstance(components.userWatcherGetter)
  61. vm.n++
  62. waitForUpdate(() => {
  63. expect(`Error in getter for watcher`).toHaveBeenWarned()
  64. function getErrorMsg () {
  65. try {
  66. this.a.b.c
  67. } catch (e) {
  68. return e.toString()
  69. }
  70. }
  71. const msg = getErrorMsg.call(vm)
  72. expect(msg).toHaveBeenWarned()
  73. }).thenWaitFor(next => {
  74. assertBothInstancesActive(vm).end(next)
  75. }).then(done)
  76. })
  77. it('should recover from errors in user watcher callback', done => {
  78. const vm = createTestInstance(components.userWatcherCallback)
  79. vm.n++
  80. waitForUpdate(() => {
  81. expect(`Error in callback for watcher "n"`).toHaveBeenWarned()
  82. expect(`Error: userWatcherCallback`).toHaveBeenWarned()
  83. }).thenWaitFor(next => {
  84. assertBothInstancesActive(vm).end(next)
  85. }).then(done)
  86. })
  87. it('config.errorHandler should capture errors', done => {
  88. const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
  89. const vm = createTestInstance(components.render)
  90. const args = spy.calls.argsFor(0)
  91. expect(args[0].toString()).toContain('Error: render') // error
  92. expect(args[1]).toBe(vm.$refs.child) // vm
  93. expect(args[2]).toContain('render function') // description
  94. assertRootInstanceActive(vm).then(() => {
  95. Vue.config.errorHandler = null
  96. }).then(done)
  97. })
  98. it('should capture and recover from nextTick errors', done => {
  99. const err1 = new Error('nextTick')
  100. const err2 = new Error('nextTick2')
  101. const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
  102. Vue.nextTick(() => { throw err1 })
  103. Vue.nextTick(() => {
  104. expect(spy).toHaveBeenCalledWith(err1, undefined, 'nextTick')
  105. const vm = new Vue()
  106. vm.$nextTick(() => { throw err2 })
  107. Vue.nextTick(() => {
  108. // should be called with correct instance info
  109. expect(spy).toHaveBeenCalledWith(err2, vm, 'nextTick')
  110. Vue.config.errorHandler = null
  111. done()
  112. })
  113. })
  114. })
  115. })
  116. function createErrorTestComponents () {
  117. const components = {}
  118. // data
  119. components.data = {
  120. data () {
  121. throw new Error('data')
  122. },
  123. render (h) {
  124. return h('div')
  125. }
  126. }
  127. // render error
  128. components.render = {
  129. render (h) {
  130. throw new Error('render')
  131. }
  132. }
  133. // lifecycle errors
  134. ;['create', 'mount', 'update', 'destroy'].forEach(hook => {
  135. // before
  136. const before = 'before' + hook.charAt(0).toUpperCase() + hook.slice(1)
  137. const beforeComp = components[before] = {
  138. props: ['n'],
  139. render (h) {
  140. return h('div', this.n)
  141. }
  142. }
  143. beforeComp[before] = function () {
  144. throw new Error(before)
  145. }
  146. // after
  147. const after = hook.replace(/e?$/, 'ed')
  148. const afterComp = components[after] = {
  149. props: ['n'],
  150. render (h) {
  151. return h('div', this.n)
  152. }
  153. }
  154. afterComp[after] = function () {
  155. throw new Error(after)
  156. }
  157. })
  158. // directive hooks errors
  159. ;['bind', 'update', 'unbind'].forEach(hook => {
  160. const key = 'directive ' + hook
  161. const dirComp = components[key] = {
  162. props: ['n'],
  163. template: `<div v-foo="n">{{ n }}</div>`
  164. }
  165. const dirFoo = {}
  166. dirFoo[hook] = function () {
  167. throw new Error(key)
  168. }
  169. dirComp.directives = {
  170. foo: dirFoo
  171. }
  172. })
  173. // user watcher
  174. components.userWatcherGetter = {
  175. props: ['n'],
  176. created () {
  177. this.$watch(function () {
  178. return this.n + this.a.b.c
  179. }, val => {
  180. console.log('user watcher fired: ' + val)
  181. })
  182. },
  183. render (h) {
  184. return h('div', this.n)
  185. }
  186. }
  187. components.userWatcherCallback = {
  188. props: ['n'],
  189. watch: {
  190. n () {
  191. throw new Error('userWatcherCallback error')
  192. }
  193. },
  194. render (h) {
  195. return h('div', this.n)
  196. }
  197. }
  198. // event errors
  199. components.event = {
  200. beforeCreate () {
  201. this.$on('e', () => { throw new Error('event') })
  202. },
  203. mounted () {
  204. this.$emit('e')
  205. },
  206. render (h) {
  207. return h('div')
  208. }
  209. }
  210. return components
  211. }
  212. function createTestInstance (Comp) {
  213. return new Vue({
  214. data: {
  215. n: 0,
  216. ok: true
  217. },
  218. render (h) {
  219. return h('div', [
  220. 'n:' + this.n + '\n',
  221. this.ok
  222. ? h(Comp, { ref: 'child', props: { n: this.n }})
  223. : null
  224. ])
  225. }
  226. }).$mount()
  227. }
  228. function assertRootInstanceActive (vm, chain) {
  229. expect(vm.$el.innerHTML).toContain('n:0\n')
  230. vm.n++
  231. return waitForUpdate(() => {
  232. expect(vm.$el.innerHTML).toContain('n:1\n')
  233. })
  234. }
  235. function assertBothInstancesActive (vm) {
  236. vm.n = 0
  237. return waitForUpdate(() => {
  238. expect(vm.$refs.child.$el.innerHTML).toContain('0')
  239. }).thenWaitFor(next => {
  240. assertRootInstanceActive(vm).then(() => {
  241. expect(vm.$refs.child.$el.innerHTML).toContain('1')
  242. }).end(next)
  243. })
  244. }