VITEST VUE SCRIPT SETUP: How to test if a method is called inside onMounted

810 Views Asked by At

How can you test if a method is called inside the onmount in vue with script setup. The problem is that there is documentation that is done with option api where in the test I can call the component and access its methods, with script setup it is not like that, I must use expose, when I use expose I can access the method that I want to spy on, but I cannot verify if it is called or not. In order to spy on the method, I must access the wrapper.vm and the wrapper is accessible only after mounting the component. When I want to spy on the onmount, the component is already mounted and does not detect the method call

In order to spy on the method, I must access the wrapper.vm and the wrapper is accessible only after mounting the component. When I want to spy on the onmount, the component is already mounted and does not detect the method call

Component

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

  const methodTest = async () => {
     await .......
  }

  onMounted(()=>{
    methodTest();
  })

  defineExpose({ methodTest })
</script>
<template>
  <div>Component</div>
</template>

How to test if methodTest is called inside onMounted, testing with vitest in vue 3

0

There are 0 best solutions below