• vite + vue3 的项目中使用 vitest 做单元测试(仅供参考)


    一、配置文件

    1. // vitest.config.ts
    2. import { fileURLToPath } from 'node:url'
    3. import { mergeConfig, defineConfig } from 'vite'
    4. import { configDefaults } from 'vitest/config'
    5. // import viteConfig from './vite.config'
    6. import vue from '@vitejs/plugin-vue'
    7. import vueJsx from '@vitejs/plugin-vue-jsx'
    8. export default mergeConfig(
    9. defineConfig({
    10. // 安装了tsx插件才需配置
    11. plugins: [
    12. vue(),
    13. vueJsx(),
    14. ],
    15. }),
    16. defineConfig({
    17. test: {
    18. globals: true,
    19. // 测试环境,模拟浏览器环境的库jsdom
    20. environment: 'jsdom',
    21. // 测试覆盖工具
    22. coverage: {
    23. provider: "c8"
    24. },
    25. // 测试报告
    26. reporters: ['junit'],
    27. // 测试报告生成文件
    28. outputFile: './coverage/junit.xml',
    29. exclude: [...configDefaults.exclude, 'e2e/*'],
    30. root: fileURLToPath(new URL('./', import.meta.url)),
    31. transformMode: {
    32. web: [/\.[jt]sx$/]
    33. }
    34. }
    35. })
    36. )

    二、全量覆盖率报告

    在 vitest 中 集成了c8,c8 是测试覆盖率检查的工具,告诉开发者代码中有哪些代码行被覆盖了,哪些没有覆盖。

    package.json增加npm script

    "test:coverage": "vitest --coverage"

    如果没安装c8,运行命令的话,Vitest 会提示安装 c8,默认yes,回车执行安装。安装后,命令行删除测试覆盖率,同时在 src/coverage 下生成一个测试报告。

    三、测试报告

    在package.json增加npm script

    "test:unit": "vitest --watch=false --coverage --reporter=junit",

    四、关闭热更新

    配置一个命令行参数–watch==false 就可以关闭这种行为

    "test:unit": "vitest --watch=false --coverage"

    五、编写测试用例

    打开 vscode 新建一个 components 目录并新增一个button目录,然后再建一个__test__目录放测试文件,elemetn-plus 源码也是这样做的,在里面新建一个xxx.test.ts 或者 tsx 的文件(没安装tsx就建ts文件)。

    vitest默认会检测项目中所有.test.ts或者.test.tsx等之类的测试文件,这个是可以修改的,具体配置可以参考官网去改。

    1. // HelloWorld.spec.ts
    2. import { describe, it, expect } from 'vitest'
    3. import { mount } from '@vue/test-utils'
    4. import HelloWorld from '../HelloWorld.vue'
    5. describe('HelloWorld', () => {
    6. it('renders properly', () => {
    7. const wrapper = mount(HelloWorld, { props: { msg: 'Hello Vitest' } })
    8. expect(wrapper.text()).toContain('Hello Vitest')
    9. })
    10. })
    1. // HelloWorld.vue
    2. <script setup lang="ts">
    3. defineProps<{
    4. msg: string
    5. }>()
    6. script>
    7. <template>
    8. <div class="greetings">
    9. <h1 class="green">{{ msg }}h1>
    10. <h3>
    11. You’ve successfully created a project with
    12. <a href="https://vitejs.dev/" target="_blank" rel="noopener">Vitea> +
    13. <a href="https://vuejs.org/" target="_blank" rel="noopener">Vue 3a>. What's next?
    14. h3>
    15. div>
    16. template>
    17. <style scoped>
    18. h1 {
    19. font-weight: 500;
    20. font-size: 2.6rem;
    21. position: relative;
    22. top: -10px;
    23. }
    24. h3 {
    25. font-size: 1.2rem;
    26. }
    27. .greetings h1,
    28. .greetings h3 {
    29. text-align: center;
    30. }
    31. @media (min-width: 1024px) {
    32. .greetings h1,
    33. .greetings h3 {
    34. text-align: left;
    35. }
    36. }
    37. style>

    参考

    Vitest | 由 Vite 提供支持的极速单元测试框架

    Vite | 下一代的前端工具链

    Vitest: 现代前端测试框架 - 知乎

  • 相关阅读:
    switch case 语句(详细)
    GO语言的错误处理
    【云原生 | 29】Docker运行大数据经典分布式平台Hadoop
    python操作mysql数据库
    Linux进程概念
    基于Hardhat编写合约测试用例
    猿创征文|MySQL基本查询语句的应用(有实例与代码)
    【读书笔记->数据分析】02 数据分析准备
    MyBatis源码基础-常用类-别名注册器
    python-爬虫(可直接使用)
  • 原文地址:https://blog.csdn.net/qq_31851435/article/details/132908162