errorCaptured.spec.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // ref: https://github.com/vuejs/vuex/issues/1505
  180. it('should not add watchers to render deps if they are referred from errorCaptured callback', done => {
  181. const store = new Vue({
  182. data: {
  183. errors: []
  184. }
  185. })
  186. const Child = {
  187. computed: {
  188. test() {
  189. throw new Error('render error')
  190. }
  191. },
  192. render(h) {
  193. return h('div', {
  194. attrs: {
  195. 'data-test': this.test
  196. }
  197. })
  198. }
  199. }
  200. new Vue({
  201. errorCaptured(error) {
  202. store.errors.push(error)
  203. },
  204. render: h => h(Child)
  205. }).$mount()
  206. // Ensure not to trigger infinite loop
  207. waitForUpdate(() => {
  208. expect(store.errors.length).toBe(1)
  209. expect(store.errors[0]).toEqual(new Error('render error'))
  210. }).then(done)
  211. })
  212. it('should capture error from watcher', done => {
  213. const spy = jasmine.createSpy()
  214. let child
  215. let err
  216. const Child = {
  217. data () {
  218. return {
  219. foo: null
  220. }
  221. },
  222. watch: {
  223. foo () {
  224. err = new Error('userWatcherCallback error')
  225. throw err
  226. }
  227. },
  228. created () {
  229. child = this
  230. },
  231. render () {}
  232. }
  233. new Vue({
  234. errorCaptured: spy,
  235. render: h => h(Child)
  236. }).$mount()
  237. child.foo = 'bar'
  238. waitForUpdate(() => {
  239. expect(spy).toHaveBeenCalledWith(err, child, 'callback for watcher "foo"')
  240. expect(globalSpy).toHaveBeenCalledWith(err, child, 'callback for watcher "foo"')
  241. }).then(done)
  242. })
  243. it('should capture promise error from watcher', done => {
  244. const spy = jasmine.createSpy()
  245. let child
  246. let err
  247. const Child = {
  248. data () {
  249. return {
  250. foo: null
  251. }
  252. },
  253. watch: {
  254. foo () {
  255. err = new Error('userWatcherCallback error')
  256. return Promise.reject(err)
  257. }
  258. },
  259. created () {
  260. child = this
  261. },
  262. render () {}
  263. }
  264. new Vue({
  265. errorCaptured: spy,
  266. render: h => h(Child)
  267. }).$mount()
  268. child.foo = 'bar'
  269. child.$nextTick(() => {
  270. waitForUpdate(() => {
  271. expect(spy).toHaveBeenCalledWith(err, child, 'callback for watcher "foo" (Promise/async)')
  272. expect(globalSpy).toHaveBeenCalledWith(err, child, 'callback for watcher "foo" (Promise/async)')
  273. }).then(done)
  274. })
  275. })
  276. it('should capture error from immediate watcher', done => {
  277. const spy = jasmine.createSpy()
  278. let child
  279. let err
  280. const Child = {
  281. data () {
  282. return {
  283. foo: 'foo'
  284. }
  285. },
  286. watch: {
  287. foo: {
  288. immediate: true,
  289. handler () {
  290. err = new Error('userImmediateWatcherCallback error')
  291. throw err
  292. }
  293. }
  294. },
  295. created () {
  296. child = this
  297. },
  298. render () {}
  299. }
  300. new Vue({
  301. errorCaptured: spy,
  302. render: h => h(Child)
  303. }).$mount()
  304. waitForUpdate(() => {
  305. expect(spy).toHaveBeenCalledWith(err, child, 'callback for immediate watcher "foo"')
  306. expect(globalSpy).toHaveBeenCalledWith(err, child, 'callback for immediate watcher "foo"')
  307. }).then(done)
  308. })
  309. it('should capture promise error from immediate watcher', done => {
  310. const spy = jasmine.createSpy()
  311. let child
  312. let err
  313. const Child = {
  314. data () {
  315. return {
  316. foo: 'foo'
  317. }
  318. },
  319. watch: {
  320. foo: {
  321. immediate: true,
  322. handler () {
  323. err = new Error('userImmediateWatcherCallback error')
  324. return Promise.reject(err)
  325. }
  326. }
  327. },
  328. created () {
  329. child = this
  330. },
  331. render () {}
  332. }
  333. new Vue({
  334. errorCaptured: spy,
  335. render: h => h(Child)
  336. }).$mount()
  337. waitForUpdate(() => {
  338. expect(spy).toHaveBeenCalledWith(err, child, 'callback for immediate watcher "foo" (Promise/async)')
  339. expect(globalSpy).toHaveBeenCalledWith(err, child, 'callback for immediate watcher "foo" (Promise/async)')
  340. }).then(done)
  341. })
  342. })