Skip to content

Commit 36c6d97

Browse files
authored
fix: script and script setup in same vue file (#536)
1 parent 3dc76ff commit 36c6d97

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
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/2.x/basic/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Jsx from './components/Jsx.vue'
2121
import Constructor from './components/Constructor.vue'
2222
import { compileStyle } from '@vue/component-compiler-utils'
2323
import ScriptSetup from './components/ScriptSetup'
24+
import ScriptAndScriptSetup from './components/ScriptAndScriptSetup'
2425
import ExtendedTsConfig from './components/ExtendedTsConfig.vue'
2526

2627
jest.mock('@vue/component-compiler-utils', () => ({
@@ -165,6 +166,12 @@ test('processes SFC with <script setup>', () => {
165166
expect(wrapper.html()).toContain('Welcome to Your Vue.js App')
166167
})
167168

169+
test('processes SFC with both <script> and <script setup> in same component file', () => {
170+
const wrapper = mount(ScriptAndScriptSetup)
171+
expect(wrapper.html()).toContain('Products: 10')
172+
expect(wrapper.html()).toContain('Welcome to Your Vue.js App')
173+
})
174+
168175
test('handles extended tsconfig.json files', () => {
169176
const wrapper = mount(ExtendedTsConfig)
170177
expect(wrapper.element.tagName).toBe('DIV')

packages/vue2-jest/lib/process.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,20 @@ module.exports = function(src, filename, config) {
144144
})
145145

146146
const templateResult = processTemplate(descriptor, filename, config)
147-
const scriptResult = processScript(descriptor.script, filename, config)
148-
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
149147
const stylesResult = processStyle(descriptor.styles, filename, config)
150148
const customBlocksResult = processCustomBlocks(
151149
descriptor.customBlocks,
152150
filename,
153151
config
154152
)
155153

154+
let scriptResult
155+
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
156+
157+
if (!scriptSetupResult) {
158+
scriptResult = processScript(descriptor.script, filename, config)
159+
}
160+
156161
const isFunctional =
157162
(descriptor.template &&
158163
descriptor.template.attrs &&

0 commit comments

Comments
 (0)