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

Test variables and functions of Vue 2.7 composition API component #521

Closed
CazSaa opened this issue Dec 20, 2022 · 3 comments
Closed

Test variables and functions of Vue 2.7 composition API component #521

CazSaa opened this issue Dec 20, 2022 · 3 comments

Comments

@CazSaa
Copy link

CazSaa commented Dec 20, 2022

Hi all,

This may be a Vue issue and not Jest specific so let me know if that's the case but I thought I'd try asking here.

I'm updating a project to Vue 2.7 and I wanted to test the composition API. I created the following component:

<template>
  <div class="toggle">
    <div v-show="toggle">
      {{ text }}
      <slot :toggle="toggleSlots" name="first"></slot>
    </div>
    <div v-if="!toggle">
      <slot :toggle="toggleSlots" name="second"></slot>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const toggle = ref(true);

const text = ref('hi');

function toggleSlots() {
  toggle.value = !toggle.value;
}

setTimeout(() => {
  text.value = 'hii';
}, 1000);

</script>

and I wrote the following test:

import { createLocalVue, mount } from '@vue/test-utils';
import AppToggle from './toggle.vue';

const localVue = createLocalVue();

describe('Toggle', () => {
  it('should toggle the slots', async () => {
    const wrapper = mount(AppToggle, { localVue });

    expect(wrapper.vm.toggle).toBeTruthy(); // vm.toggle is undefined

    wrapper.vm.toggleSlots(); // vm.toggleSlots is undefined

    expect(wrapper.vm.toggle).toBeFalsy();
  });
});

If I write the component using the options API, test test passes, but with the composition API the test fails because the variables and functions are not defined on wrapper.vm. How can I access the variables and functions?

@marlokessler
Copy link

Hi there, we're experiencing the same issue. Also not sure whether it is an issue with vue2-jest or with vue. We're using Vue 2.7.14, vue2-jest v27.0.0. Would be glad for any help too!

@CazSaa
Copy link
Author

CazSaa commented Dec 22, 2022

@marlokessler I found the solution. See https://stackoverflow.com/a/74887122/14851412

@CazSaa CazSaa closed this as completed Dec 22, 2022
@marlokessler
Copy link

Sadly, it does not work for me, but seems to be another issue. I opened another issue for that: #525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants