Vue-Router simulation using Vue-Test-Utils and Vitest
P粉190883225
2023-08-25 09:26:52
<p>I'm trying to understand the logic of mocking Vue-Router with Vitest. </p>
<p>To do this, I tried setting up and simulating my test environment on a very simple project. When I try to follow the official documentation of Vue-Test-Utils, I always get an error. I don't know if it's because they use Jest. </p>
<p>Using a real vue-router solved my problem, but I think mocking a vue-router is better. </p>
<p> Below I will first convey the source code of the project, and then the errors I received. </p>
<h3>Home.vue</h3>
<pre class="brush:php;toolbar:false;"><script setup lang="ts">
import {onMounted} from "vue";
import {useRoute} from "vue-router";
const route = useRoute()
onMounted(() => {
console.log(route.query)
})
</script>
<template>
<div>Home</div>
</template></pre>
<h3>Home.spec.ts</h3>
<pre class="brush:php;toolbar:false;">import {expect, it, vi} from "vitest";
import {mount} from "@vue/test-utils";
import Home from "../src/components/Home.vue"
it('Home Test', async () => {
const wrapper = mount(Home)
expect(wrapper.exists()).toBeTruthy()
})</pre>
<h3>vite.config.ts</h3>
<pre class="brush:php;toolbar:false;">/// <reference types="vitest" />
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
test: {
environment: 'jsdom',
include: ['./test/**/*.spec.ts'],
exclude: ['node_modules', 'dist'],
globals: true
}
})</pre>
<h3>My error message is as follows: ..</h3>
<h3>What I tried</h3>
<p>I tried to simulate vue-router like below</p>
<pre class="brush:php;toolbar:false;">vi.mock('vue-router', () => ({
useRoute: vi.fn(),
}))</pre>
<p>Or just</p>
<pre class="brush:php;toolbar:false;">vi.mock('vue-router')</pre>
<h4>This is my final Home.spec.ts file</h4>
<pre class="brush:php;toolbar:false;">import {expect, it, vi} from "vitest";
import {mount} from "@vue/test-utils";
import Home from "../src/components/Home.vue"
vi.mock('vue-router')
it('Home Test', async () => {
const wrapper = mount(Home, {
global: {
stubs: ["router-link", "router-view"]
}
})
expect(wrapper.exists()).toBeTruthy()
})</pre></p>
First, I want to see
router-link
orrouter-view
inHome.vue
:So,
Home.spec.ts
should look like this:Some comments/suggestions:
.spyOn()
and.mockImplementation...()
for monitoring and simulation.toHaveBeenCalled...()
Check if the mock is working as expectedmount()
The stub in the function should be related to the component used in the template (so if you don't use
it shouldn't be listed as a stub)Hope it helps, cheers!