Skip to content

Commit 015a33d

Browse files
author
qhermel
committed
Fix: script and script setup in same vue component file (Vue 3)
1 parent 21feab9 commit 015a33d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<div>
3+
<button @click="increase">Products: {{ products }}</button>
4+
<Basic />
5+
<span>{{ msg }}</span>
6+
</div>
7+
</template>
8+
9+
<script>
10+
import Basic from './Basic.vue'
11+
</script>
12+
13+
<script setup>
14+
import { ref } from 'vue'
15+
16+
const products = ref(10)
17+
const greet = () => console.log('greet')
18+
const increase = () => {
19+
greet()
20+
num.value++
21+
}
22+
const msg = 'hello world'
23+
</script>

e2e/3.x/basic/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,9 @@ test('handles extended tsconfig.json files', () => {
214214
const elm = document.querySelector('div')
215215
expect(elm).toBeDefined()
216216
})
217+
218+
test('processes SFC with both <script> and <script setup> in same component file', () => {
219+
const wrapper = mount(ScriptAndScriptSetup)
220+
expect(wrapper.html()).toContain('Products: 10')
221+
expect(wrapper.html()).toContain('Welcome to Your Vue.js App')
222+
})

packages/vue3-jest/lib/process.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,21 @@ module.exports = function(src, filename, config) {
164164
getVueJestConfig(config)['componentNamespace'] || vueComponentNamespace
165165

166166
const templateResult = processTemplate(descriptor, filename, config)
167-
const scriptResult = processScript(descriptor.script, filename, config)
168-
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
169167
const stylesResult = processStyle(descriptor.styles, filename, config)
170168
const customBlocksResult = processCustomBlocks(
171169
descriptor.customBlocks,
172170
filename,
173171
componentNamespace,
174172
config
175173
)
174+
175+
let scriptResult
176+
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
177+
178+
if (!scriptSetupResult) {
179+
scriptResult = processScript(descriptor.script, filename, config)
180+
}
181+
176182
const output = generateCode({
177183
scriptResult,
178184
scriptSetupResult,

0 commit comments

Comments
 (0)