当前位置: 首页 > 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.dtcms.com/a/317954.html

相关文章:

  • Python数据可视化:从基础到高级实战指南
  • 【代码随想录day 12】 力扣 144.145.94.前序遍历中序遍历后序遍历
  • 【数据可视化-82】中国城市幸福指数可视化分析:Python + PyEcharts 打造炫酷城市幸福指数可视化大屏
  • 架构层防护在高并发场景下的实践
  • Linux系统之Docker命令与镜像、容器管理
  • Spring Cloud系列—Eureka服务注册/发现
  • ElasticSearch~DSL常用语法
  • Unity 调节 Rigidbody2D 响应速度的解决方案【资料】
  • CS课程项目设计8:基于Canvas支持AI人机对战的五子棋游戏
  • Lesson 35 Stop thief!
  • MATLAB实现的改进遗传算法用于有约束优化问题
  • Java 工具类的“活化石”:Apache Commons 核心用法、性能陷阱与现代替代方案
  • 03-mysql/redis/apache安装记录
  • 从《中国开源年度报告》看中国开源力量的十年变迁中,Apache SeaTunnel 的跃迁
  • SmartMediaKit 模块化音视频框架实战指南:场景链路 + 能力矩阵全解析
  • LinkedList 深度解析:核心原理与实践
  • uniapp开发中 解决App端 点击input输入框 整体上移
  • DocBench:面向大模型文档阅读系统的评估基准与数据集分析
  • win10/11网络防火墙阻止网络连接?【图文详解】防火墙阻止连接网络的解决方法
  • 电商 API 接口接入案例剖析​
  • LLAVA Visual Instruction Tuning——视觉语言通用模型的先驱
  • 从零开始学AI——12.2
  • LeetCode 188:买卖股票的最佳时机 IV
  • 基于跨境电商场景的智能商品管理系统,采用Bootstrap+Django+MySQL技术架构,实现用户行为追踪、智能推荐、多维度商品展示等核心功能
  • Python与自动化运维:构建智能IT基础设施的终极方案
  • QT----简单的htttp服务器与客户端
  • 【python】OpenCV—Defect Detection
  • 用人工智能设计海报
  • 深入底层:如何优雅部署 SeaTunnel 分离集群到 Kubernetes
  • mysql索引的用法