news 2026/4/23 16:00:30

前端——单元测试实践

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
前端——单元测试实践

背景问题:
需要为 Vue3 + Vite 项目编写单元测试。

方案思考:
使用 Vitest 作为测试框架,结合 @vue/test-utils 进行组件测试。

具体实现:
安装测试依赖:

# 安装 Vitest 和 Vue 测试工具npminstall-D vitest @vue/test-utils jsdom happy-dom# 安装断言库(Vitest 默认包含 Chai 断言)npminstall-D @vitest/coverage-v8

Vitest 配置:

// vite.config.jsimport{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'exportdefaultdefineConfig({plugins:[vue()],test:{// 启用 Vitest 的 APIglobals:true,// 指定测试环境environment:'jsdom',// 或 'happy-dom'// 包含测试文件的模式include:['tests/**/*.test.{js,ts}'],// 排除文件的模式exclude:['node_modules','dist','.idea','.git','.cache'],// 测试覆盖率配置coverage:{provider:'v8',// 或 'istanbul'reporter:['text','json','html'],reportsDirectory:'./coverage',exclude:['node_modules/**','tests/**','vite.config.js']},// 模拟 DOM 环境setupFiles:['./tests/setup.js']}})

测试工具配置:

// tests/setup.jsimport{expect,afterEach}from'vitest'import{cleanup}from'@testing-library/vue'importmatchersfrom'@vitest/expect'// 扩展 Vitest 的 expectexpect.extend(matchers)// 在每个测试后清理 DOMafterEach(()=>{cleanup()})

组件测试示例:

<!-- components/HelloWorld.vue --> <template> <h1>{{ msg }}</h1> <button @click="count++">count is: {{ count }}</button> <p>Edit <code>components/HelloWorld.vue</code> to test hot module replacement.</p> </template> <script setup> import { ref } from 'vue' defineProps({ msg: String }) const count = ref(0) </script>
// tests/unit/HelloWorld.test.jsimport{describe,it,expect}from'vitest'import{mount}from'@vue/test-utils'importHelloWorldfrom'@/components/HelloWorld.vue'describe('HelloWorld',()=>{it('renders properly',()=>{constwrapper=mount(HelloWorld,{props:{msg:'Hello World'}})expect(wrapper.text()).toContain('Hello World')})it('counter starts at 0 and increments',async()=>{constwrapper=mount(HelloWorld)// 初始值expect(wrapper.find('button').text()).toBe('count is: 0')// 点击按钮awaitwrapper.find('button').trigger('click')// 验证值已更新expect(wrapper.find('button').text()).toBe('count is: 1')})it('reactive props work correctly',()=>{constwrapper=mount(HelloWorld,{props:{msg:'Updated Message'}})expect(wrapper.find('h1').text()).toBe('Updated Message')})})

组合式函数测试:

// composables/useCounter.jsimport{ref}from'vue'exportfunctionuseCounter(initialValue=0){constcount=ref(initialValue)constincrement=()=>{count.value++}constdecrement=()=>{count.value--}constreset=()=>{count.value=initialValue}return{count,increment,decrement,reset}}
// tests/unit/composables/useCounter.test.jsimport{describe,it,expect}from'vitest'import{useCounter}from'@/composables/useCounter'describe('useCounter',()=>{it('should initialize with default value 0',()=>{const{count}=useCounter()expect(count.value).toBe(0)})it('should initialize with provided initial value',()=>{const{count}=useCounter(5)expect(count.value).toBe(5)})it('should increment counter',()=>{const{count,increment}=useCounter(0)increment()expect(count.value).toBe(1)})it('should decrement counter',()=>{const{count,decrement}=useCounter(5)decrement()expect(count.value).toBe(4)})it('should reset counter to initial value',()=>{const{count,increment,reset}=useCounter(10)increment()increment()expect(count.value).toBe(12)reset()expect(count.value).toBe(10)})})

API 测试:

// utils/request.js (测试版)importaxiosfrom'axios'// Mock axiosexportconstmockAxios={get:vi.fn(),post:vi.fn(),put:vi.fn(),delete:vi.fn()}exportdefault{get:(url,config)=>mockAxios.get(url,config),post:(url,data,config)=>mockAxios.post(url,data,config),put:(url,data,config)=>mockAxios.put(url,data,config),delete:(url,config)=>mockAxios.delete(url,config)}
// tests/unit/utils/api.test.jsimport{describe,it,expect,beforeEach,vi}from'vitest'import{mockAxios}from'@/utils/request'describe('API utilities',()=>{beforeEach(()=>{// 重置 mockvi.clearAllMocks()})it('should make GET request',async()=>{constmockResponse={data:{id:1,name:'Test'}}mockAxios.get.mockResolvedValue(mockResponse)constresult=awaitmockAxios.get('/api/users/1')expect(mockAxios.get).toHaveBeenCalledWith('/api/users/1')expect(result).toEqual(mockResponse)})it('should handle API errors',async()=>{constmockError=newError('Network Error')mockAxios.get.mockRejectedValue(mockError)awaitexpect(mockAxios.get('/api/users/1')).rejects.toThrow('Network Error')expect(mockAxios.get).toHaveBeenCalledWith('/api/users/1')})})

测试工具函数:

// tests/utils/index.jsimport{mount}from'@vue/test-utils'import{setActivePinia,createPinia}from'pinia'// 创建带 Pinia 的包装器exportfunctioncreateWrapper(Component,options={}){// 创建新的 Pinia 实例constpinia=createPinia()returnmount(Component,{...options,global:{plugins:[pinia],...(options.global||{})}})}// 模拟路由exportfunctionmockRouter(){return{push:vi.fn(),replace:vi.fn(),go:vi.fn(),back:vi.fn(),forward:vi.fn()}}// 模拟响应式数据exportfunctionmockReactive(obj){returnvi.fn(()=>obj)}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 13:09:27

物联网项目tp5怎么也获取不到请求的参数问题

做一个物联网项目时&#xff0c;thinkPHP5.0用常规的框架方法获取不到设备请求过来的参数&#xff0c;总是空。 最后用 原生PHP获取请求体 的方法得到了参数&#xff0c;也就是用PHP原生的方法获取参数&#xff0c;获取后为字符串&#xff0c;再转换为对象就可以用了 $raw fil…

作者头像 李华
网站建设 2026/4/19 3:46:07

2026牛客网春招面经,BATJ最新10000道Java中高级面试题,限时开源

前言&#xff1a; 想在面试、工作中脱颖而出&#xff1f;想在最短的时间内快速掌握 Java 的核心基础知识点&#xff1f;想要成为一位优秀的 Java 工程师&#xff1f;本篇文章能助你一臂之力&#xff01; 目前正值招聘求职旺季&#xff0c;很多同学对一些新技术名词都能侃侃而…

作者头像 李华
网站建设 2026/4/23 12:01:42

技术面:如何让你的系统抗住高并发的流量?

前言 如何能让系统抗住高并发流量&#xff0c;要考虑的因素有很多&#xff0c;但是真的让你讲一下都有哪些&#xff0c;很多人肯定就会说&#xff0c;用Redis缓存啦&#xff0c;用MQ做解耦啦&#xff0c;总之就是想起来这一块儿就说一嘴&#xff0c;想起来那一块儿也说一嘴&am…

作者头像 李华
网站建设 2026/4/23 12:00:42

南加州大学让AI说话更有口音:语言学规则与神经网络的奇妙对话

当我们听到不同地区的人说英语时&#xff0c;总能轻松区分出美式英语和英式英语的差别。但如果要让计算机生成的语音也具备这种自然的口音变化&#xff0c;事情就变得复杂多了。南加州大学信号分析与解释实验室、计算机科学系和语言学系的研究团队最近在2026年IEEE国际声学、语…

作者头像 李华
网站建设 2026/4/23 12:02:34

面试官:RocketMQ 消息堆积了怎么处理?

面试考察点 面试官提出这个问题&#xff0c;主要希望考察候选人以下几个方面的能力&#xff1a; 问题诊断能力&#xff1a;候选人能否系统性地分析消息堆积的根源&#xff0c;而不仅仅是给出解决方案。这包括区分是 “生产者流量激增” 还是 “消费者消费能力不足” 导致的问题…

作者头像 李华