Skip to content

Commit cbf9ca3

Browse files
authored
fix: capture emitted events from script setup components (#663)
* fix: capture emitted events from script setup components * fix missing import
1 parent d949a21 commit cbf9ca3

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/mount.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
ComponentOptionsWithoutProps,
1111
ExtractPropTypes,
1212
WritableComputedOptions,
13-
ComponentPropsOptions,
1413
AppConfig,
1514
VNodeProps,
1615
ComponentOptionsMixin,
@@ -21,7 +20,8 @@ import {
2120
ExtractDefaultPropTypes,
2221
VNode,
2322
EmitsOptions,
24-
ComputedOptions
23+
ComputedOptions,
24+
ComponentPropsOptions
2525
} from 'vue'
2626

2727
import { MountingOptions, Slot } from './types'
@@ -337,6 +337,15 @@ export function mount(
337337
const Parent = defineComponent({
338338
name: MOUNT_PARENT_NAME,
339339
render() {
340+
// https://github.com/vuejs/vue-test-utils-next/issues/651
341+
// script setup components include an empty `expose` array as part of the
342+
// code generated by the SFC compiler. Eg a component might look like
343+
// { expose: [], setup: [Function], render: [Function] }
344+
// not sure why (yet), but the empty expose array causes events to not be
345+
// correctly captured.
346+
// TODO: figure out why this is happening and understand the implications of
347+
// the expose rfc for Test Utils.
348+
delete component.expose
340349
return h(component, props, slots)
341350
}
342351
})

tests/components/EmitsEventSFC.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<button @click="click" />
3+
</template>
4+
5+
<script lang="ts">
6+
import { defineComponent, onMounted } from 'vue';
7+
8+
export default defineComponent({
9+
emits: ['bar'],
10+
setup(props, { emit }) {
11+
onMounted(() => {
12+
emit('bar', 'mounted')
13+
})
14+
15+
return {
16+
click: () => emit('bar', 'click')
17+
}
18+
}
19+
})
20+
21+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<button @click="click" />
3+
</template>
4+
5+
<script setup lang="ts">
6+
import { onMounted, defineEmit } from 'vue';
7+
8+
const emit = defineEmit(['bar']);
9+
10+
const click = () => emit('bar', 'click')
11+
12+
onMounted(() => {
13+
emit('bar', 'mounted')
14+
})
15+
</script>

tests/emit.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
SetupContext
77
} from 'vue'
88
import { Vue } from 'vue-class-component'
9+
import EmitsEventSFC from './components/EmitsEventSFC.vue'
10+
import EmitsEventScriptSetup from './components/EmitsEventScriptSetup.vue'
911

1012
import { mount } from '../src'
1113

@@ -319,4 +321,17 @@ describe('emitted', () => {
319321
await wrapper.trigger('click')
320322
expect(wrapper.emitted('foo')).toHaveLength(1)
321323
})
324+
325+
it.each([EmitsEventSFC, EmitsEventScriptSetup])(
326+
'captures emitted events',
327+
async (component) => {
328+
const wrapper = mount(component)
329+
await wrapper.trigger('click')
330+
331+
expect(wrapper.emitted().click).toHaveLength(1)
332+
expect(wrapper.emitted().bar).toHaveLength(2)
333+
expect(wrapper.emitted().bar[0]).toEqual(['mounted'])
334+
expect(wrapper.emitted().bar[1]).toEqual(['click'])
335+
}
336+
)
322337
})

0 commit comments

Comments
 (0)