当前位置: 首页 > news >正文

vue3 vite 使用vitest 单元测试 组件测试

单元测试

pnpm add -D vitest vue-test-utils @vue/test-utils jsdom @testing-library/vue @testing-library/jest-dom @vitest/uits项目需要加上
pnpm add -D @types/jest

修改 vite.config.ts

// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'export default defineConfig({plugins: [vue()],test: {globals: true,       // 支持 describe、it、expect 等全局函数environment: 'jsdom', // 模拟浏览器环境setupFiles: './test/setup.ts', // 可选,设置初始化文件include: ['tests/**/*.test.ts'],}
})

初始化测试文件结构

在项目根目录下创建 tests 目录:

📁 tests
│
├── 📄 setup.ts        // 初始化内容,比如 jest-dom 扩展
├── 📄 button.test.ts  // 单元测试示例
├── 📄 HelloWorld.test.ts  // 组件测试示例
  • tests/setup.ts 示例:
import '@testing-library/jest-dom'

、编写一个单元测试(函数测试)

  • src/utils/math.ts
export const add = (a: number, b: number) => a + b

tests/math.test.ts

import { describe, it, expect } from 'vitest'
import { add } from '../src/utils/math'describe('math utils', () => {it('adds numbers correctly', () => {expect(add(1, 2)).toBe(3)})
})

编写一个组件测试(UI 组件

src/components/HelloWorld.vue

<template><div><p data-testid="msg">{{ msg }}</p><button @click="count++">Count: {{ count }}</button></div>
</template><script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>

tests/HelloWorld.test.ts

import { render, fireEvent } from '@testing-library/vue'
import HelloWorld from '../src/components/HelloWorld.vue'
import { describe, it, expect } from 'vitest'describe('HelloWorld.vue', () => {it('renders props.msg', () => {const { getByTestId } = render(HelloWorld, { props: { msg: 'Hello Vitest' } })expect(getByTestId('msg')).toHaveTextContent('Hello Vitest')})it('increments counter on click', async () => {const { getByText } = render(HelloWorld, { props: { msg: 'Click test' } })const button = getByText(/Count:/)await fireEvent.click(button)expect(button.textContent).toBe('Count: 1')})
})

运行测试

pnpm vitest
# 或使用 UI 面板查看更方便
pnpm vitest --ui
测试类型工具用途适用阶段
单元测试Vitest测试函数逻辑,如格式化、计算编写完函数或逻辑模块后立刻写
组件测试Vue Test Utils + Vitest测试组件渲染/交互写完一个组件后就写
集成测试同上组件之间配合重要流程完成后写
E2E测试(端到端)Playwright / Cypress模拟用户操作,整个页面测试大功能开发完后统一写

测试用例目录

在这里插入图片描述

No test files found. You can change the file name pattern by pressing “p”

检查vite.config.ts的配置信息路径是否正确

vite.config.ts配置

test: {// jest like test apiglobals: true,// 模拟dom环境environment: 'happy-dom',include: ['src/tests/**/*.test.ts', //这里目录要正确不然会报错找不到测试脚本'src/tests/**/*.spec.ts',],setupFiles: 'src/tests/setup.ts',// 支持tsx,jsxtransformMode: {web: [/.[tj]sx$/]}},

package.json配置

 "test": "vitest"

运行命令pnpm test 即可执行

http://www.xdnf.cn/news/1250623.html

相关文章:

  • Lesson 31 Success story
  • 【C++详解】STL-set和map的介绍和使用样例、pair类型介绍、序列式容器和关联式容器
  • 蓝桥杯----锁存器、LED、蜂鸣器、继电器、Motor
  • RN项目环境搭建和使用-Mac版本(模拟器启动不起来的排查)
  • 软件定义汽车 --- 电子电气架构的驱动
  • 【pytorch(02)】Tensor(张量)概述、如何创建、常见属性,切换设备
  • AI学习之大话transformer架构
  • 2025年08月 GitHub 热门项目推荐
  • Spring选择哪种方式代理?
  • 电子电气架构 ---如何焕新升级为 48V 电气架构
  • 无人机航拍数据集|第4期 无人机太阳光伏板红外目标检测YOLO数据集10945张yolov11/yolov8/yolov5可训练
  • OpenHarmony源码解析之init进程
  • Android Activity webView页面视频悬浮小窗播放效果及技术难点
  • apache-tomcat-11.0.9安装及环境变量配置
  • 聊一聊RPC接口测试工具及方法
  • MonoFusion 与 Genie 3
  • Apollo中三种相机外参的可视化分析
  • Javascript/ES6+/Typescript重点内容篇——手撕(待总结)
  • W3D引擎游戏开发----从入门到精通【22】
  • 【科研绘图系列】R语言绘制瀑布图
  • sqli-labs靶场less40-less45
  • 012 网络—基础篇
  • 医疗AI中GPU部署的“非对等全节点架构“方案分析(上)
  • 如何创建一个vue项目
  • 5G随身WiFi怎么选?实测延迟/网速/续航,中兴V50适合商务,格行MT700适合短租、户外党~避坑指南+适用场景全解析
  • Git 分支管理:从新开发分支迁移为主分支的完整指南
  • 【数据结构初阶】--排序(四):归并排序
  • Linux基础命令的生产常用命令及其示例简单解释
  • 对接钉钉审批过程记录(C#版本)
  • C++与C语言实现Stack的对比分析