Skip to content

Commit 760a744

Browse files
committed
test warning component trace
1 parent 3ff1788 commit 760a744

File tree

3 files changed

+98
-30
lines changed

3 files changed

+98
-30
lines changed

src/core/util/debug.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import config from '../config'
22
import { noop } from 'shared/util'
33

4-
let warn = noop
5-
let tip = noop
6-
let formatComponentName
4+
export let warn = noop
5+
export let tip = noop
6+
export let formatComponentName
77

88
if (process.env.NODE_ENV !== 'production') {
99
const hasConsole = typeof console !== 'undefined'
@@ -52,8 +52,18 @@ if (process.env.NODE_ENV !== 'production') {
5252
)
5353
}
5454

55+
const repeat = (str, n) => {
56+
let res = ''
57+
while (n) {
58+
if (n % 2 === 1) res += str
59+
if (n > 1) str += str
60+
n >>= 1
61+
}
62+
return res
63+
}
64+
5565
const generateComponentTrace = vm => {
56-
if (vm._isVue && vm.$parent && String.prototype.repeat) {
66+
if (vm._isVue && vm.$parent) {
5767
const tree = []
5868
let currentRecursiveSequence = 0
5969
while (vm) {
@@ -73,7 +83,7 @@ if (process.env.NODE_ENV !== 'production') {
7383
}
7484
return '\n\nfound in\n\n' + tree
7585
.map((vm, i) => `${
76-
i === 0 ? '---> ' : ' '.repeat(5 + i * 2)
86+
i === 0 ? '---> ' : repeat(' ', 5 + i * 2)
7787
}${
7888
Array.isArray(vm)
7989
? `${formatComponentName(vm[0])}... (${vm[1]} recursive calls)`
@@ -85,5 +95,3 @@ if (process.env.NODE_ENV !== 'production') {
8595
}
8696
}
8797
}
88-
89-
export { warn, tip, formatComponentName }

test/unit/features/debug.spec.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import Vue from 'vue'
2+
import { formatComponentName } from 'core/util/debug'
3+
4+
describe('Debug utilities', () => {
5+
it('properly format component names', () => {
6+
const vm = new Vue()
7+
expect(formatComponentName(vm)).toBe('<Root>')
8+
9+
vm.$root = null
10+
vm.$options.name = 'hello-there'
11+
expect(formatComponentName(vm)).toBe('<HelloThere>')
12+
13+
vm.$options.name = null
14+
vm.$options._componentTag = 'foo-bar-1'
15+
expect(formatComponentName(vm)).toBe('<FooBar1>')
16+
17+
vm.$options._componentTag = null
18+
vm.$options.__file = '/foo/bar/baz/SomeThing.vue'
19+
expect(formatComponentName(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
20+
expect(formatComponentName(vm, false)).toBe('<SomeThing>')
21+
22+
vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue'
23+
expect(formatComponentName(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
24+
expect(formatComponentName(vm, false)).toBe('<WindowsFile>')
25+
})
26+
27+
it('generate correct component hierarchy trace', () => {
28+
const one = {
29+
name: 'one',
30+
render: h => h(two)
31+
}
32+
const two = {
33+
name: 'two',
34+
render: h => h(three)
35+
}
36+
const three = {
37+
name: 'three'
38+
}
39+
new Vue({
40+
render: h => h(one)
41+
}).$mount()
42+
43+
expect(
44+
`Failed to mount component: template or render function not defined.
45+
46+
found in
47+
48+
---> <Three>
49+
<Two>
50+
<One>
51+
<Root>`
52+
).toHaveBeenWarned()
53+
})
54+
55+
it('generate correct component hierarchy trace (recursive)', () => {
56+
let i = 0
57+
const one = {
58+
name: 'one',
59+
render: h => i++ < 5 ? h(one) : h(two)
60+
}
61+
const two = {
62+
name: 'two',
63+
render: h => h(three)
64+
}
65+
const three = {
66+
name: 'three'
67+
}
68+
new Vue({
69+
render: h => h(one)
70+
}).$mount()
71+
72+
expect(
73+
`Failed to mount component: template or render function not defined.
74+
75+
found in
76+
77+
---> <Three>
78+
<Two>
79+
<One>... (5 recursive calls)
80+
<Root>`
81+
).toHaveBeenWarned()
82+
})
83+
})

test/unit/features/error-handling.spec.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Vue from 'vue'
2-
import { formatComponentName } from 'core/util/debug'
32

43
const components = createErrorTestComponents()
54

@@ -124,28 +123,6 @@ describe('Error handling', () => {
124123
})
125124
})
126125
})
127-
128-
it('properly format component names', () => {
129-
const vm = new Vue()
130-
expect(formatComponentName(vm)).toBe('<Root>')
131-
132-
vm.$root = null
133-
vm.$options.name = 'hello-there'
134-
expect(formatComponentName(vm)).toBe('<HelloThere>')
135-
136-
vm.$options.name = null
137-
vm.$options._componentTag = 'foo-bar-1'
138-
expect(formatComponentName(vm)).toBe('<FooBar1>')
139-
140-
vm.$options._componentTag = null
141-
vm.$options.__file = '/foo/bar/baz/SomeThing.vue'
142-
expect(formatComponentName(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
143-
expect(formatComponentName(vm, false)).toBe('<SomeThing>')
144-
145-
vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue'
146-
expect(formatComponentName(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
147-
expect(formatComponentName(vm, false)).toBe('<WindowsFile>')
148-
})
149126
})
150127

151128
function createErrorTestComponents () {

0 commit comments

Comments
 (0)