Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get emitted events with the script setup RFC #651

Closed
JJBocanegra opened this issue Jun 7, 2021 · 3 comments
Closed

Can't get emitted events with the script setup RFC #651

JJBocanegra opened this issue Jun 7, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@JJBocanegra
Copy link

The script setup RFC is a proposal for Vue 3 that introduce a new script type in Single File Components: <script setup>, which exposes all its top level bindings to the template.

Using this, I can't catch any events emitted from the mounted component in the tests.

NewSetupComponent.vue

<script setup lang="ts">
import { onMounted, defineEmit } from 'vue';

const emit = defineEmit(['foo']);

onMounted(() => emit('foo', 'mounted'));
</script>

NewSetupComponent.spec.ts

import { mount } from '@vue/test-utils';
import NewSetupComponent from './NewSetupComponent.vue';

describe('NewSetupComponent', () => {
  it('should emit foo', () => {
    const wrapper = mount(NewSetupComponent);

    expect(wrapper.emitted('foo')).toBeTruthy();
  });
});

This always returns undefined in the test.


If I use the usual composition API syntax, it works as expected.

SetupComponent.vue

<script lang="ts">
import { defineComponent, onMounted } from 'vue';

export default defineComponent({
  emits: ['foo'],

  setup(_props, { emit }) {
    onMounted(() => emit('foo', 'mounted'));
  },
});
</script>

SetupComponent.spec.ts

import { mount } from '@vue/test-utils';
import SetupComponent from './SetupComponent.vue';

describe('SetupComponent', () => {
  it('should', () => {
    const wrapper = mount(SetupComponent);

    expect(wrapper.emitted('foo')).toBeTruthy();
  });
});

Here the test pass.


I tried using async/await, using the old <script setup> RFC syntax, and nothing works.

Am I doing something wrong? If not, would you support this?

Thanks!

@lmiller1990 lmiller1990 added the bug Something isn't working label Jun 8, 2021
@lmiller1990
Copy link
Member

Interesting. Definitely a bug.

I will investigate this. We should definitely have more coverage around <script setup> - still experimental, but I'd like to support is as best we can.

@lmiller1990
Copy link
Member

I am going to work on this now. I did some digging, I can capture a click event. The problem is unique to custom events. I'll keep debugging.

@lmiller1990
Copy link
Member

I fixed this. This fix probably breaks something, I don't know why it works, but it's a start. #663

I have not used expose, I am not sure what the current implementation is, either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants