A Guide/Help to use with Vitest and Composition API #174
Answered by posva
joseluisgs asked this question in Q&A
-
| Hi I would like to use with Vitest and Composition API Bu I can't get to use it this is my component <button data-testid="DayButton" class="btn btn-primary" @click="goToDaybook()" > DayBook </button><script setup> import { useRouter } from 'vue-router' const router = useRouter() const goToDaybook = () => router.push({ name: 'daybook-no-entry' }) const goToAbout = () => router.push({ name: 'about' }) </script>this is my test import { createRouterMock, injectRouterMock } from 'vue-router-mock' test('should be go to a daybook-no-entry', async () => { const router = createRouterMock() injectRouterMock(router) const wrapper = mount(HomeView) expect(wrapper.router).toBe(router) // Mock del metodo goToDayBook wrapper.vm.goToDaybook = vi.fn() // Trabajamos con el boton const dayButton = wrapper.find('[data-testid="DayButton"]') expect(dayButton.exists()).toBe(true) // texto de boton es daybook expect(dayButton.text()).toBe('DayBook') await dayButton.trigger('click') // Cone sto ya sabemos que lo ha llamado!!! expect(wrapper.vm.goToDaybook).toHaveBeenCalledTimes(1) // Router push ha sido llamado expect(wrapper.router.push).toHaveBeenCalledTimes(1) })I have this problem FAIL tests/unit/views/HomeViewTest.spec.js > View -> HomeView > Hacer click en el botón debe redireccionar a daybook-no-entry Error: No Spy Available ❯ createSpy node_modules/vue-router-mock/dist/index.mjs:40:9 ❯ Module.createRouterMock node_modules/vue-router-mock/dist/index.mjs:70:45 ❯ tests/unit/views/HomeViewTest.spec.js:15:19 13| 14| test('Hacer click en el botón debe redireccionar a daybook-no-entry', async () => { 15| const router = createRouterMock() | ^ 16| injectRouterMock(router) 17| Could you help me or give me a guide?? |
Beta Was this translation helpful? Give feedback.
Answered by posva Aug 19, 2022
Replies: 1 comment 5 replies
-
| See https://github.com/posva/vue-router-mock#testing-libraries |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by joseluisgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
See https://github.com/posva/vue-router-mock#testing-libraries