Skip to content

Commit 556f2d6

Browse files
authored
Merge pull request #124 from cexbrayat/chore/shallow-mount-overloads
chore: shallowMount overloads
2 parents 112f29a + 6fd0c7c commit 556f2d6

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

src/mount.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,6 @@ export function mount(
259259
return createWrapper(app, App, setProps)
260260
}
261261

262-
export function shallowMount(
263-
originalComponent,
264-
options?: MountingOptions<any>
265-
) {
266-
return mount(originalComponent, { ...options, shallow: true })
262+
export const shallowMount: typeof mount = (component: any, options?: any) => {
263+
return mount(component, { ...options, shallow: true })
267264
}

test-dts/shallowMount.d-test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { expectError, expectType } from 'tsd'
2+
import { defineComponent } from 'vue'
3+
import { shallowMount } from '../src'
4+
5+
const AppWithDefine = defineComponent({
6+
props: {
7+
a: {
8+
type: String,
9+
required: true
10+
},
11+
b: Number
12+
},
13+
template: ''
14+
})
15+
16+
// accept props
17+
let wrapper = shallowMount(AppWithDefine, {
18+
props: { a: 'Hello', b: 2 }
19+
})
20+
// vm is properly typed
21+
expectType<string>(wrapper.vm.a)
22+
23+
// can receive extra props
24+
// ideally, it should not
25+
// but the props have type { a: string } & VNodeProps
26+
// which allows any property
27+
shallowMount(AppWithDefine, {
28+
props: { a: 'Hello', c: 2 }
29+
})
30+
31+
// wrong prop type should not compile
32+
expectError(
33+
shallowMount(AppWithDefine, {
34+
props: { a: 2 }
35+
})
36+
)
37+
38+
const AppWithProps = {
39+
props: {
40+
a: {
41+
type: String,
42+
required: true
43+
}
44+
},
45+
template: ''
46+
}
47+
48+
// accept props
49+
wrapper = shallowMount(AppWithProps, {
50+
props: { a: 'Hello' }
51+
})
52+
// vm is properly typed
53+
expectType<string>(wrapper.vm.a)
54+
55+
// can't receive extra props
56+
expectError(
57+
shallowMount(AppWithProps, {
58+
props: { a: 'Hello', b: 2 }
59+
})
60+
)
61+
62+
// wrong prop type should not compile
63+
expectError(
64+
shallowMount(AppWithProps, {
65+
props: { a: 2 }
66+
})
67+
)
68+
69+
const AppWithArrayProps = {
70+
props: ['a'],
71+
template: ''
72+
}
73+
74+
// accept props
75+
wrapper = shallowMount(AppWithArrayProps, {
76+
props: { a: 'Hello' }
77+
})
78+
// vm is properly typed
79+
expectType<string>(wrapper.vm.a)
80+
81+
// can receive extra props
82+
// as they are declared as `string[]`
83+
shallowMount(AppWithArrayProps, {
84+
props: { a: 'Hello', b: 2 }
85+
})
86+
87+
const AppWithoutProps = {
88+
template: ''
89+
}
90+
91+
// can't receive extra props
92+
expectError(
93+
(wrapper = shallowMount(AppWithoutProps, {
94+
props: { b: 'Hello' }
95+
}))
96+
)
97+
98+
// except if explicitly cast
99+
shallowMount(AppWithoutProps, {
100+
props: { b: 'Hello' } as never
101+
})

0 commit comments

Comments
 (0)