errorCaptured.spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import Vue from 'vue'
  2. describe('Options errorCaptured', () => {
  3. let globalSpy
  4. beforeEach(() => {
  5. globalSpy = Vue.config.errorHandler = jasmine.createSpy()
  6. })
  7. afterEach(() => {
  8. Vue.config.errorHandler = null
  9. })
  10. it('should capture error from child component', () => {
  11. const spy = jasmine.createSpy()
  12. let child
  13. let err
  14. const Child = {
  15. created () {
  16. child = this
  17. err = new Error('child')
  18. throw err
  19. },
  20. render () {}
  21. }
  22. new Vue({
  23. errorCaptured: spy,
  24. render: h => h(Child)
  25. }).$mount()
  26. expect(spy).toHaveBeenCalledWith(err, child, 'created hook')
  27. // should propagate by default
  28. expect(globalSpy).toHaveBeenCalledWith(err, child, 'created hook')
  29. })
  30. it('should be able to render the error in itself', done => {
  31. let child
  32. const Child = {
  33. created () {
  34. child = this
  35. throw new Error('error from child')
  36. },
  37. render () {}
  38. }
  39. const vm = new Vue({
  40. data: {
  41. error: null
  42. },
  43. errorCaptured (e, vm, info) {
  44. expect(vm).toBe(child)
  45. this.error = e.toString() + ' in ' + info
  46. },
  47. render (h) {
  48. if (this.error) {
  49. return h('pre', this.error)
  50. }
  51. return h(Child)
  52. }
  53. }).$mount()
  54. waitForUpdate(() => {
  55. expect(vm.$el.textContent).toContain('error from child')
  56. expect(vm.$el.textContent).toContain('in created hook')
  57. }).then(done)
  58. })
  59. it('should not propagate to global handler when returning true', () => {
  60. const spy = jasmine.createSpy()
  61. let child
  62. let err
  63. const Child = {
  64. created () {
  65. child = this
  66. err = new Error('child')
  67. throw err
  68. },
  69. render () {}
  70. }
  71. new Vue({
  72. errorCaptured (err, vm, info) {
  73. spy(err, vm, info)
  74. return false
  75. },
  76. render: h => h(Child, {})
  77. }).$mount()
  78. expect(spy).toHaveBeenCalledWith(err, child, 'created hook')
  79. // should not propagate
  80. expect(globalSpy).not.toHaveBeenCalled()
  81. })
  82. it('should propagate to global handler if itself throws error', () => {
  83. let child
  84. let err
  85. const Child = {
  86. created () {
  87. child = this
  88. err = new Error('child')
  89. throw err
  90. },
  91. render () {}
  92. }
  93. let err2
  94. const vm = new Vue({
  95. errorCaptured () {
  96. err2 = new Error('foo')
  97. throw err2
  98. },
  99. render: h => h(Child, {})
  100. }).$mount()
  101. expect(globalSpy).toHaveBeenCalledWith(err, child, 'created hook')
  102. expect(globalSpy).toHaveBeenCalledWith(err2, vm, 'errorCaptured hook')
  103. })
  104. it('should work across multiple parents, mixins and extends', () => {
  105. const calls = []
  106. const Child = {
  107. created () {
  108. throw new Error('child')
  109. },
  110. render () {}
  111. }
  112. const ErrorBoundaryBase = {
  113. errorCaptured () {
  114. calls.push(1)
  115. }
  116. }
  117. const mixin = {
  118. errorCaptured () {
  119. calls.push(2)
  120. }
  121. }
  122. const ErrorBoundaryExtended = {
  123. extends: ErrorBoundaryBase,
  124. mixins: [mixin],
  125. errorCaptured () {
  126. calls.push(3)
  127. },
  128. render: h => h(Child)
  129. }
  130. Vue.config.errorHandler = () => {
  131. calls.push(5)
  132. }
  133. new Vue({
  134. errorCaptured () {
  135. calls.push(4)
  136. },
  137. render: h => h(ErrorBoundaryExtended)
  138. }).$mount()
  139. expect(calls).toEqual([1, 2, 3, 4, 5])
  140. })
  141. it('should work across multiple parents, mixins and extends with return false', () => {
  142. const calls = []
  143. const Child = {
  144. created () {
  145. throw new Error('child')
  146. },
  147. render () {}
  148. }
  149. const ErrorBoundaryBase = {
  150. errorCaptured () {
  151. calls.push(1)
  152. }
  153. }
  154. const mixin = {
  155. errorCaptured () {
  156. calls.push(2)
  157. }
  158. }
  159. const ErrorBoundaryExtended = {
  160. extends: ErrorBoundaryBase,
  161. mixins: [mixin],
  162. errorCaptured () {
  163. calls.push(3)
  164. return false
  165. },
  166. render: h => h(Child)
  167. }
  168. Vue.config.errorHandler = () => {
  169. calls.push(5)
  170. }
  171. new Vue({
  172. errorCaptured () {
  173. calls.push(4)
  174. },
  175. render: h => h(ErrorBoundaryExtended)
  176. }).$mount()
  177. expect(calls).toEqual([1, 2, 3])
  178. })
  179. })