debug.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import Vue from 'vue'
  2. import { formatComponentName, warn } from 'core/util/debug'
  3. describe('Debug utilities', () => {
  4. it('properly format component names', () => {
  5. const vm = new Vue()
  6. expect(formatComponentName(vm)).toBe('<Root>')
  7. vm.$root = null
  8. vm.$options.name = 'hello-there'
  9. expect(formatComponentName(vm)).toBe('<HelloThere>')
  10. vm.$options.name = null
  11. vm.$options._componentTag = 'foo-bar-1'
  12. expect(formatComponentName(vm)).toBe('<FooBar1>')
  13. vm.$options._componentTag = null
  14. vm.$options.__file = '/foo/bar/baz/SomeThing.vue'
  15. expect(formatComponentName(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
  16. expect(formatComponentName(vm, false)).toBe('<SomeThing>')
  17. vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue'
  18. expect(formatComponentName(vm)).toBe(
  19. `<WindowsFile> at ${vm.$options.__file}`
  20. )
  21. expect(formatComponentName(vm, false)).toBe('<WindowsFile>')
  22. })
  23. it('generate correct component hierarchy trace', () => {
  24. const one = {
  25. name: 'one',
  26. render: h => h(two)
  27. }
  28. const two = {
  29. name: 'two',
  30. render: h => h(three)
  31. }
  32. const three = {
  33. name: 'three'
  34. }
  35. new Vue({
  36. render: h => h(one)
  37. }).$mount()
  38. expect(
  39. `Failed to mount component: template or render function not defined.
  40. found in
  41. ---> <Three>
  42. <Two>
  43. <One>
  44. <Root>`
  45. ).toHaveBeenWarned()
  46. })
  47. it('generate correct component hierarchy trace (recursive)', () => {
  48. let i = 0
  49. const one = {
  50. name: 'one',
  51. render: h => (i++ < 5 ? h(one) : h(two))
  52. }
  53. const two = {
  54. name: 'two',
  55. render: h => h(three)
  56. }
  57. const three = {
  58. name: 'three'
  59. }
  60. new Vue({
  61. render: h => h(one)
  62. }).$mount()
  63. expect(
  64. `Failed to mount component: template or render function not defined.
  65. found in
  66. ---> <Three>
  67. <Two>
  68. <One>... (5 recursive calls)
  69. <Root>`
  70. ).toHaveBeenWarned()
  71. })
  72. describe('warn', () => {
  73. const msg = 'message'
  74. const vm = new Vue()
  75. it('calls warnHandler if warnHandler is set', () => {
  76. const spy = (Vue.config.warnHandler = vi.fn())
  77. warn(msg, vm)
  78. expect(spy.mock.calls[0][0]).toBe(msg)
  79. expect(spy.mock.calls[0][1]).toBe(vm)
  80. // @ts-expect-error
  81. Vue.config.warnHandler = null
  82. })
  83. it('calls console.error if silent is false', () => {
  84. Vue.config.silent = false
  85. warn(msg, vm)
  86. expect(msg).toHaveBeenWarned()
  87. expect(console.error).toHaveBeenCalled()
  88. })
  89. it('does not call console.error if silent is true', () => {
  90. Vue.config.silent = true
  91. warn(msg, vm)
  92. expect(console.error).not.toHaveBeenCalled()
  93. Vue.config.silent = false
  94. })
  95. })
  96. })