Skip to content

Commit c2efcd2

Browse files
rrittwaglmiller1990
authored andcommitted
fix: shallowMount failed for script setup components
1 parent 9f4b0bf commit c2efcd2

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/stubs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export function stubComponents(
152152
}
153153
}
154154
}
155-
if (!name && !shallow) {
156-
return args
155+
if (!name) {
156+
return shallow ? ['stub'] : args
157157
}
158158

159159
const stub = resolveComponentStubByName(name, stubs)

tests/components/ComponentWithChildren.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<hello />
44
<component-with-input />
55
<component-without-name />
6+
<script-setup />
67
<with-props />
78
</div>
89
</template>
@@ -11,6 +12,7 @@
1112
import Hello from './Hello'
1213
import ComponentWithInput from './ComponentWithInput'
1314
import ComponentWithoutName from './ComponentWithoutName'
15+
import ScriptSetup from './ScriptSetup'
1416
import WithProps from './WithProps'
1517
1618
export default {
@@ -19,9 +21,10 @@ export default {
1921
Hello,
2022
ComponentWithInput,
2123
ComponentWithoutName,
24+
ScriptSetup,
2225
WithProps
2326
},
24-
data () {
27+
data() {
2528
return {}
2629
}
2730
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts">
2+
import Hello from './Hello'
3+
import ComponentWithInput from './ComponentWithInput'
4+
import ComponentWithoutName from './ComponentWithoutName'
5+
import ScriptSetup from './ScriptSetup'
6+
import WithProps from './WithProps'
7+
</script>
8+
9+
<template>
10+
<hello />
11+
<component-with-input />
12+
<component-without-name />
13+
<script-setup />
14+
<with-props />
15+
</template>

tests/shallowMount.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineComponent } from 'vue'
22
import { mount, shallowMount, VueWrapper } from '../src'
33
import ComponentWithChildren from './components/ComponentWithChildren.vue'
4+
import ScriptSetupWithChildren from './components/ScriptSetupWithChildren.vue'
45

56
describe('shallowMount', () => {
67
it('renders props for stubbed component in a snapshot', () => {
@@ -43,6 +44,7 @@ describe('shallowMount', () => {
4344
'<hello-stub></hello-stub>' +
4445
'<component-with-input-stub></component-with-input-stub>' +
4546
'<component-without-name-stub></component-without-name-stub>' +
47+
'<script-setup-stub></script-setup-stub>' +
4648
'<with-props-stub></with-props-stub>' +
4749
'</div>'
4850
)
@@ -55,6 +57,7 @@ describe('shallowMount', () => {
5557
'<hello-stub></hello-stub>' +
5658
'<component-with-input-stub></component-with-input-stub>' +
5759
'<component-without-name-stub></component-without-name-stub>' +
60+
'<script-setup-stub></script-setup-stub>' +
5861
'<with-props-stub></with-props-stub>' +
5962
'</div>'
6063
)
@@ -74,8 +77,27 @@ describe('shallowMount', () => {
7477
'<div>Override</div>' +
7578
'<component-with-input-stub></component-with-input-stub>' +
7679
'<component-without-name-stub></component-without-name-stub>' +
80+
'<script-setup-stub></script-setup-stub>' +
7781
'<with-props-stub></with-props-stub>' +
7882
'</div>'
7983
)
8084
})
85+
86+
it('stubs all components in a script setup component', () => {
87+
const wrapper = mount(ScriptSetupWithChildren, {
88+
shallow: true,
89+
global: {
90+
stubs: {
91+
Hello: { template: '<div>Override</div>' }
92+
}
93+
}
94+
})
95+
expect(wrapper.html()).toEqual(
96+
'<div>Override</div>' +
97+
'<component-with-input-stub></component-with-input-stub>' +
98+
'<stub></stub>' +
99+
'<stub></stub>' +
100+
'<with-props-stub></with-props-stub>'
101+
)
102+
})
81103
})

0 commit comments

Comments
 (0)