模拟Vue RouterView/RouterLink在Vitest(组合 API)中
P粉478188786
P粉478188786 2024-03-27 09:49:56
[Vue.js讨论组]

根据标题,这有可能吗?

我正在尝试测试Vue应用程序的一个简单组件,其中包含一个标题和一个按钮,该按钮将用户重定向到下一个页面。我想测试一下,当按钮被点击时,是否会向路由器发送一个事件/消息,并检查目标路由是否正确。

应用程序的结构如下:

App.Vue - 入口组件







router/index.ts - Vue Router配置文件

import {createRouter, createWebHistory} from "vue-router";
import HomeView from "@/views/HomeView.vue";

export enum RouteNames {
    HOME = "home",
    GAME = "game",
}

const routes = [
    {
      path: "/",
      name: RouteNames.HOME,
      component: HomeView,
      alias: "/home"
    },
    {
      path: "/game",
      name: RouteNames.GAME,
      component: () => import("@/views/GameView.vue"),
    },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

export default router;

HomeView.Vue - App组件中RouterView的入口视图




ButtonRouterLink.vue




考虑到它使用CompositionAPI和TypeScript,有没有办法在Vitest测试中模拟路由器?

这是一个测试文件结构的示例(HomeView.spec.ts):

import {shallowMount} from "@vue/test-utils";
import { describe, test, vi, expect } from "vitest";
import { RouteNames } from "@/router";
import HomeView from "../../views/HomeView.vue";

describe("Home", () => {
    test ('navigates to game route', async () => {
        // Check if the button navigates to the game route

        const wrapper = shallowMount(HomeView);

        await wrapper.find('.btn-game').trigger('click');

        expect(MOCK_ROUTER.push).toHaveBeenCalledWith({ name: RouteNames.GAME });
    });
});

我尝试了多种方法:vi.mock('vue-router'),vi.spyOn(useRoute,'push'),vue-router-mock库,但是我无法让测试运行起来,似乎路由器就是不可用的。

更新(15/04/23)

根据 @tao 的建议,我意识到在HomeView中测试点击事件并不是一个很好的测试(测试的是库而不是我的应用程序),所以我添加了一个测试 ButtonRouterLink,看看它是否正确渲染了Vue RouterLink,使用to参数:

import {mount, shallowMount} from "@vue/test-utils";
import { describe, test, vi, expect } from "vitest";
import { RouteNames } from "@/router";
import ButtonRouterLink from "../ButtonRouterLink.vue";

vi.mock('vue-router');

describe("ButtonRouterLink", () => {
    test (`correctly transforms 'to' param into a router-link prop`, async () => {
       
        const wrapper = mount(ButtonRouterLink, {
            props: {
                to: RouteNames.GAME
            }
        });

        expect(wrapper.html()).toMatchSnapshot();
    });
});

它渲染了一个空的HTML字符串""

exports[`ButtonRouterLink > correctly transforms 'to' param into a router-link prop 1`] = `""`;

伴随着一个不太有用的Vue警告(未指定预期类型):

[Vue warn]: Invalid prop: type check failed for prop "to". Expected , got Object  
  at  
  at  
  at 
[Vue warn]: Invalid prop: type check failed for prop "ariaCurrentValue". Expected , got String with value "page". 
  at  
  at  
  at 
[Vue warn]: Component is missing template or render function. 
  at  
  at  
  at 

P粉478188786
P粉478188786

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号