@@ -8,8 +8,19 @@ const stripInlineSourceMap = require('./utils').stripInlineSourceMap
8
8
const getCustomTransformer = require ( './utils' ) . getCustomTransformer
9
9
const loadSrc = require ( './utils' ) . loadSrc
10
10
const babelTransformer = require ( 'babel-jest' ) . default
11
- const compilerUtils = require ( '@vue/component-compiler-utils' )
12
11
const generateCode = require ( './generate-code' )
12
+ const mapLines = require ( './map-lines' )
13
+ const vueComponentNamespace = require ( './constants' ) . vueComponentNamespace
14
+
15
+ let isVue27 = false
16
+ let compilerUtils
17
+
18
+ try {
19
+ compilerUtils = require ( 'vue/compiler-sfc' )
20
+ isVue27 = true
21
+ } catch ( e ) {
22
+ compilerUtils = require ( '@vue/component-compiler-utils' )
23
+ }
13
24
14
25
function resolveTransformer ( lang = 'js' , vueJestConfig ) {
15
26
const transformer = getCustomTransformer ( vueJestConfig [ 'transform' ] , lang )
@@ -42,7 +53,33 @@ function processScript(scriptPart, filePath, config) {
42
53
return result
43
54
}
44
55
45
- function processTemplate ( template , filename , config ) {
56
+ function processScriptSetup ( descriptor , filePath , config ) {
57
+ if ( ! descriptor . scriptSetup ) {
58
+ return null
59
+ }
60
+ const vueJestConfig = getVueJestConfig ( config )
61
+ const content = compilerUtils . compileScript ( descriptor , {
62
+ id : filePath ,
63
+ reactivityTransform : true ,
64
+ ...vueJestConfig . compilerOptions
65
+ } )
66
+ const contentMap = mapLines ( descriptor . scriptSetup . map , content . map )
67
+
68
+ const transformer = resolveTransformer (
69
+ descriptor . scriptSetup . lang ,
70
+ vueJestConfig
71
+ )
72
+
73
+ const result = transformer . process ( content . content , filePath , config )
74
+ result . code = stripInlineSourceMap ( result . code )
75
+ result . map = mapLines ( contentMap , result . map )
76
+
77
+ return result
78
+ }
79
+
80
+ function processTemplate ( descriptor , filename , config ) {
81
+ const { template, scriptSetup } = descriptor
82
+
46
83
if ( ! template ) {
47
84
return null
48
85
}
@@ -53,6 +90,16 @@ function processTemplate(template, filename, config) {
53
90
template . content = loadSrc ( template . src , filename )
54
91
}
55
92
93
+ let bindings
94
+ if ( isVue27 && scriptSetup ) {
95
+ const scriptSetupResult = compilerUtils . compileScript ( descriptor , {
96
+ id : filename ,
97
+ reactivityTransform : true ,
98
+ ...vueJestConfig . compilerOptions
99
+ } )
100
+ bindings = scriptSetupResult . bindings
101
+ }
102
+
56
103
const userTemplateCompilerOptions = vueJestConfig . templateCompiler || { }
57
104
const result = compilerUtils . compileTemplate ( {
58
105
source : template . content ,
@@ -63,9 +110,10 @@ function processTemplate(template, filename, config) {
63
110
preprocessOptions : vueJestConfig [ template . lang ] ,
64
111
...userTemplateCompilerOptions ,
65
112
compilerOptions : {
66
- optimize : false ,
113
+ ... ( ! isVue27 ? { optimize : false } : { } ) ,
67
114
...userTemplateCompilerOptions . compilerOptions
68
- }
115
+ } ,
116
+ ...( isVue27 ? { bindings } : { } )
69
117
} )
70
118
71
119
logResultErrors ( result )
@@ -91,16 +139,21 @@ function processStyle(styles, filename, config) {
91
139
module . exports = function ( src , filename , config ) {
92
140
const descriptor = compilerUtils . parse ( {
93
141
source : src ,
94
- compiler : VueTemplateCompiler ,
142
+ compiler : isVue27 ? undefined : VueTemplateCompiler ,
95
143
filename
96
144
} )
97
145
98
- const templateResult = processTemplate ( descriptor . template , filename , config )
146
+ const componentNamespace =
147
+ getVueJestConfig ( config ) [ 'componentNamespace' ] || vueComponentNamespace
148
+
149
+ const templateResult = processTemplate ( descriptor , filename , config )
99
150
const scriptResult = processScript ( descriptor . script , filename , config )
151
+ const scriptSetupResult = processScriptSetup ( descriptor , filename , config )
100
152
const stylesResult = processStyle ( descriptor . styles , filename , config )
101
153
const customBlocksResult = processCustomBlocks (
102
154
descriptor . customBlocks ,
103
155
filename ,
156
+ componentNamespace ,
104
157
config
105
158
)
106
159
@@ -114,6 +167,7 @@ module.exports = function(src, filename, config) {
114
167
115
168
const output = generateCode (
116
169
scriptResult ,
170
+ scriptSetupResult ,
117
171
templateResult ,
118
172
stylesResult ,
119
173
customBlocksResult ,
0 commit comments