test(compiler-sfc): more tests (#536)

This commit is contained in:
宋铄运 2019-12-11 22:26:14 +08:00 committed by Evan You
parent 532d3b68ab
commit 559fa27185
3 changed files with 107 additions and 6 deletions

View File

@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`source map 1`] = `
Object {
"mappings": ";;;;UAAA,aACE;IAAK,gCAAMA,WAAM",
"names": Array [
"render",
],
"sources": Array [
"example.vue",
],
"sourcesContent": Array [
"
<div><p>{{ render }}</p></div>
",
],
"version": 3,
}
`;
exports[`template errors 1`] = `
Array [
[SyntaxError: Invalid JavaScript expression. (2:13)],
[SyntaxError: v-bind is missing expression. (1:6)],
[SyntaxError: v-model can only be used on <input>, <textarea> and <select> elements. (2:17)],
]
`;

View File

@ -1,5 +1,3 @@
// TODO need more thorough tests here
import { compileTemplate } from '../src/compileTemplate'
import { parse, SFCTemplateBlock } from '../src/parse'
@ -50,3 +48,70 @@ test('warn missing preprocessor', () => {
expect(result.errors.length).toBe(1)
})
test('transform asset url options', () => {
const input = { source: `<foo bar="baz"/>`, filename: 'example.vue' }
// Object option
const { code: code1 } = compileTemplate({
...input,
transformAssetUrls: { foo: ['bar'] }
})
expect(code1).toMatch(`import _imports_0 from 'baz'\n`)
// false option
const { code: code2 } = compileTemplate({
...input,
transformAssetUrls: false
})
expect(code2).not.toMatch(`import _imports_0 from 'baz'\n`)
})
test('source map', () => {
const template = parse(
`
<template>
<div><p>{{ render }}</p></div>
</template>
`,
{ filename: 'example.vue', sourceMap: true }
).template as SFCTemplateBlock
const result = compileTemplate({
filename: 'example.vue',
source: template.content
})
expect(result.map).toMatchSnapshot()
})
test('template errors', () => {
const result = compileTemplate({
filename: 'example.vue',
source: `<div :foo
:bar="a[" v-model="baz"/>`
})
expect(result.errors).toMatchSnapshot()
})
test('preprocessor errors', () => {
const template = parse(
`
<template lang="pug">
div(class='class)
</template>
`,
{ filename: 'example.vue', sourceMap: true }
).template as SFCTemplateBlock
const result = compileTemplate({
filename: 'example.vue',
source: template.content,
preprocessLang: template.lang
})
expect(result.errors.length).toBe(1)
const message = result.errors[0].toString()
expect(message).toMatch(`Error: example.vue:3:1`)
expect(message).toMatch(
`The end of the string reached with no closing bracket ) found.`
)
})

View File

@ -65,10 +65,19 @@ export function compileTemplate(
const preprocessor =
preprocessLang && consolidate[preprocessLang as keyof typeof consolidate]
if (preprocessor) {
return doCompileTemplate({
...options,
source: preprocess(options, preprocessor)
})
try {
return doCompileTemplate({
...options,
source: preprocess(options, preprocessor)
})
} catch (e) {
return {
code: `export default function render() {}`,
source: options.source,
tips: [],
errors: [e]
}
}
} else if (preprocessLang) {
return {
code: `export default function render() {}`,