使用 Vue-Test-Utils 和 Vitest 进行 Vue-Router 模拟

Moc*_*ha_ 8 unit-testing mocking vue.js vue-router vitest

我试图理解用 Vitest 模拟 Vue-Router 的逻辑。

为此,我尝试在一个非常简单的项目上设置和模拟我的测试环境。当我尝试按照Vue-Test-Utils 的官方文档进行操作时,总是出现错误。我不知道是不是因为他们用Jest。

使用真正的 vue-router解决了我的问题,但我认为模拟 vue-router 更好。

下面我先传达一下项目的源码,然后是我收到的错误。

首页.vue

<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>
Run Code Online (Sandbox Code Playgroud)

主页.规格.ts

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()
})
Run Code Online (Sandbox Code Playgroud)

vite.config.ts

/// <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
  }
})
Run Code Online (Sandbox Code Playgroud)

我的错误信息如下:..

错误信息1

错误信息2

我尝试过的方法

我尝试像下面这样模拟 vue-router

vi.mock('vue-router', () => ({
    useRoute: vi.fn(),
}))
Run Code Online (Sandbox Code Playgroud)

要不就

vi.mock('vue-router')
Run Code Online (Sandbox Code Playgroud)

这是我的最终 Home.spec.ts 文件

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()
})
Run Code Online (Sandbox Code Playgroud)

小智 3

首先,我希望看到router-linkor :router-viewHome.vue

<script setup lang="ts">
import { onMounted } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute();

onMounted(() => {
  console.log(route.query);
});
</script>

<template>
  <router-link to="home">Go to home</router-link>
  <router-view />
</template>
Run Code Online (Sandbox Code Playgroud)

所以,Home.spec.ts应该是这样的:

import { expect, it, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import * as VueRouter from 'vue-router';
import Home from '../src/components/Home.vue';

describe('./path/to/Home.vue', () => {
  const useRouteSpy = vi.spyOn(VueRouter, 'useRoute');
  const getWrapper = () => mount(Home as any, {
    global: {
      stubs: {
        'router-link': { template: '<div/>' },
        'router-view': { template: '<div/>' },
      },
    },
  });

  it('the component should be mounted', () => {
    // ARRANGE
    const useRouteMock = useRouteSpy.mockImplementationOnce({ query: 'query' });
    // ACT
    const wrapper = getWrapper();
    // ASSERT
    expect(useRouteMock).toHaveBeenCalled();
    expect(wrapper.exists()).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

一些评论/建议:

  • 使用描述来界定测试上下文
  • 定义一个全局函数来挂载组件,重用而不是重复
  • 使用.spyOn()and.mockImplementation...()来监视和嘲笑
  • 使用一些结构化/直接的方式来编写测试,例如 AAA [安排、行动、断言] 或 GWT [给定、何时、然后]。我已经测试了几年并且仍在使用它,它可以帮助我理解我正在测试的内容
  • 用于.toHaveBeenCalled...()检查模拟是否按预期工作
  • 函数中的存根mount()应与模板中使用的组件相关(因此,如果您不使用<router-view>,则不应将其列为存根)

希望有帮助,干杯!