diff --git a/src/mount.ts b/src/mount.ts index 6850b5f17..8cfde239f 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -10,7 +10,6 @@ import { ComponentOptionsWithoutProps, ExtractPropTypes, WritableComputedOptions, - ComponentPropsOptions, AppConfig, VNodeProps, ComponentOptionsMixin, @@ -21,7 +20,8 @@ import { ExtractDefaultPropTypes, VNode, EmitsOptions, - ComputedOptions + ComputedOptions, + ComponentPropsOptions } from 'vue' import { MountingOptions, Slot } from './types' @@ -337,6 +337,15 @@ export function mount( const Parent = defineComponent({ name: MOUNT_PARENT_NAME, render() { + // https://github.com/vuejs/vue-test-utils-next/issues/651 + // script setup components include an empty `expose` array as part of the + // code generated by the SFC compiler. Eg a component might look like + // { expose: [], setup: [Function], render: [Function] } + // not sure why (yet), but the empty expose array causes events to not be + // correctly captured. + // TODO: figure out why this is happening and understand the implications of + // the expose rfc for Test Utils. + delete component.expose return h(component, props, slots) } }) diff --git a/tests/components/EmitsEventSFC.vue b/tests/components/EmitsEventSFC.vue new file mode 100644 index 000000000..bb7d92f24 --- /dev/null +++ b/tests/components/EmitsEventSFC.vue @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/tests/components/EmitsEventScriptSetup.vue b/tests/components/EmitsEventScriptSetup.vue new file mode 100644 index 000000000..043e8e81d --- /dev/null +++ b/tests/components/EmitsEventScriptSetup.vue @@ -0,0 +1,15 @@ + + + \ No newline at end of file diff --git a/tests/emit.spec.ts b/tests/emit.spec.ts index 9862a3a90..b55531bc2 100644 --- a/tests/emit.spec.ts +++ b/tests/emit.spec.ts @@ -6,6 +6,8 @@ import { SetupContext } from 'vue' import { Vue } from 'vue-class-component' +import EmitsEventSFC from './components/EmitsEventSFC.vue' +import EmitsEventScriptSetup from './components/EmitsEventScriptSetup.vue' import { mount } from '../src' @@ -319,4 +321,17 @@ describe('emitted', () => { await wrapper.trigger('click') expect(wrapper.emitted('foo')).toHaveLength(1) }) + + it.each([EmitsEventSFC, EmitsEventScriptSetup])( + 'captures emitted events', + async (component) => { + const wrapper = mount(component) + await wrapper.trigger('click') + + expect(wrapper.emitted().click).toHaveLength(1) + expect(wrapper.emitted().bar).toHaveLength(2) + expect(wrapper.emitted().bar[0]).toEqual(['mounted']) + expect(wrapper.emitted().bar[1]).toEqual(['click']) + } + ) })