Skip to content

Commit 65a0207

Browse files
committed
test: test runtime compilation error warning
1 parent cf12d18 commit 65a0207

File tree

3 files changed

+67
-58
lines changed

3 files changed

+67
-58
lines changed

packages/runtime-core/__tests__/apiApp.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe('api: createApp', () => {
270270

271271
const handler = (app.config.warnHandler = jest.fn(
272272
(msg, instance, trace) => {
273-
expect(msg).toMatch(`Component is missing render function`)
273+
expect(msg).toMatch(`Component is missing template or render function`)
274274
expect(instance).toBe(ctx.renderProxy)
275275
expect(trace).toMatch(`Hello`)
276276
}

packages/runtime-core/src/component.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -348,37 +348,40 @@ function finishComponentSetup(
348348
const Component = instance.type as ComponentOptions
349349
if (!instance.render) {
350350
if (__RUNTIME_COMPILE__ && Component.template && !Component.render) {
351-
if (compile) {
352-
Component.render = compile(Component.template, {
353-
isCustomElement: instance.appContext.config.isCustomElement || NO,
354-
onError(err: CompilerError) {
355-
if (__DEV__) {
356-
const message = `Template compilation error: ${err.message}`
357-
const codeFrame =
358-
err.loc &&
359-
generateCodeFrame(
360-
Component.template!,
361-
err.loc.start.offset,
362-
err.loc.end.offset
363-
)
364-
warn(codeFrame ? `${message}\n${codeFrame}` : message)
365-
}
351+
// __RUNTIME_COMPILE__ ensures `compile` is provided
352+
Component.render = compile!(Component.template, {
353+
isCustomElement: instance.appContext.config.isCustomElement || NO,
354+
onError(err: CompilerError) {
355+
if (__DEV__) {
356+
const message = `Template compilation error: ${err.message}`
357+
const codeFrame =
358+
err.loc &&
359+
generateCodeFrame(
360+
Component.template!,
361+
err.loc.start.offset,
362+
err.loc.end.offset
363+
)
364+
warn(codeFrame ? `${message}\n${codeFrame}` : message)
366365
}
367-
})
368-
} else if (__DEV__) {
366+
}
367+
})
368+
}
369+
if (__DEV__ && !Component.render) {
370+
/* istanbul ignore if */
371+
if (!__RUNTIME_COMPILE__ && Component.template) {
369372
warn(
370373
`Component provides template but the build of Vue you are running ` +
371374
`does not support on-the-fly template compilation. Either use the ` +
372375
`full build or pre-compile the template using Vue CLI.`
373376
)
377+
} else {
378+
warn(
379+
`Component is missing${
380+
__RUNTIME_COMPILE__ ? ` template or` : ``
381+
} render function.`
382+
)
374383
}
375384
}
376-
if (__DEV__ && !Component.render) {
377-
warn(
378-
`Component is missing render function. Either provide a template or ` +
379-
`return a render function from setup().`
380-
)
381-
}
382385
instance.render = (Component.render || NOOP) as RenderFunction
383386
}
384387

packages/vue/__tests__/index.spec.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
import { createApp } from '../src'
2+
import { mockWarn } from '@vue/runtime-test'
23

3-
it('should support on-the-fly template compilation', () => {
4-
const container = document.createElement('div')
5-
const App = {
6-
template: `{{ count }}`,
7-
data() {
8-
return {
9-
count: 0
4+
describe('compiler + runtime integration', () => {
5+
mockWarn()
6+
7+
it('should support on-the-fly template compilation', () => {
8+
const container = document.createElement('div')
9+
const App = {
10+
template: `{{ count }}`,
11+
data() {
12+
return {
13+
count: 0
14+
}
1015
}
1116
}
12-
}
13-
createApp().mount(App, container)
14-
expect(container.innerHTML).toBe(`0`)
15-
})
17+
createApp().mount(App, container)
18+
expect(container.innerHTML).toBe(`0`)
19+
})
1620

17-
it('should correctly normalize class with on-the-fly template compilation', () => {
18-
const container = document.createElement('div')
19-
const App = {
20-
template: `<div :class="{ test: demoValue, test2: !demoValue }"></div>`,
21-
data() {
22-
return {
23-
demoValue: true
24-
}
21+
it('should warn template compilation errors with codeframe', () => {
22+
const container = document.createElement('div')
23+
const App = {
24+
template: `<div v-if>`
2525
}
26-
}
27-
createApp().mount(App, container)
28-
const classes = container.firstElementChild!.classList
29-
expect(classes.contains('test')).toBe(true)
30-
expect(classes.contains('test2')).toBe(false)
31-
})
26+
createApp().mount(App, container)
27+
expect(
28+
`Template compilation error: End tag was not found`
29+
).toHaveBeenWarned()
30+
expect(`v-if/v-else-if is missing expression`).toHaveBeenWarned()
31+
expect(
32+
`
33+
1 | <div v-if>
34+
| ^^^^`.trim()
35+
).toHaveBeenWarned()
36+
})
3237

33-
it('should support custom element', () => {
34-
const app = createApp()
35-
const container = document.createElement('div')
36-
const App = {
37-
template: '<custom></custom>'
38-
}
39-
app.config.isCustomElement = tag => tag === 'custom'
40-
app.mount(App, container)
41-
expect(container.innerHTML).toBe('<custom></custom>')
38+
it('should support custom element', () => {
39+
const app = createApp()
40+
const container = document.createElement('div')
41+
const App = {
42+
template: '<custom></custom>'
43+
}
44+
app.config.isCustomElement = tag => tag === 'custom'
45+
app.mount(App, container)
46+
expect(container.innerHTML).toBe('<custom></custom>')
47+
})
4248
})

0 commit comments

Comments
 (0)