基于Vitest进行组件测试

测试种类

测试的学问很深,有很多种不同的测试类型:

  • 单元测试:测试给定函数、类和复用逻辑
  • 组件测试:检测咱们组件的挂载、渲染和交互性
  • 端到端测试:通过真实网络请求我们应用并检测夸多页面的功能特性

在我们项目中,我们是代码的制造者,我们主要关注代码逻辑、组件渲染和交互,组件逻辑也非常清楚,因此我们只做单测,属于白盒测试。

单元测试框架:Vitest

Vitest是Vue/Vite团队成员维护的单元测试框架,它可以使用和Vite同一个配置并且使用相同的转换流程,因此安装配置都非常快捷!

安装Vitest

yarn add -D vitest happy-dom @testing-library/vue

配置,vite.config.js

/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({
  // ...
  test: {
    // enable jest-like global test APIs
    globals: true,
    // simulate DOM with happy-dom
    // (requires installing happy-dom as a peer dependency)
    environment: 'happy-dom',
    // 支持tsx组件,很关键
    transformMode: {
      web: [/.[tj]sx$/]
    }
  }
})

`避免类型错误提示

ts配置添加类型

// tsconfig.json

{
 "compileroptions": {
    "types": ["vitest/globals"]
  }
}

编写测试

// Test.test.ts
test('it should work', () => {
  const { getByText } = render(Test, {
    props: {
      msg: 'hello'
    }
  })

  // assert output
  getByText('hello')
})

添加脚本

{
  // ...
  "scripts": {
    "test": "vitest"
  }
}

运行

> npm test

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a9Sg6SoZ-1665996199411)(https://secure2.wostatic.cn/static/rUPEZnMLANd91dxtk7FKgJ/image.png?auth_key=1665995535-hnGp391uUqbsLN6vTrvjCA-0-4b449808bbdea5008b631a31b2d71d89?auth_key=1665995989-8AZVfrTLaFvXbiZBrEZpPB-0-ef910e9eaf2410e0ed19cfe6eb2451fd)]

原则

  • 单元测试应该写的很小,仅覆盖单独函数、类、可复用逻辑或模块
  • 单测关注逻辑正确性而且仅关注应用程序功能的一个小块
  • 对于可视部分,组件测试验证的是基于输入的props和slots渲染输出的结果
  • 对于行为逻辑,组件测试验证响应用户输入事件后正确的渲染更新和派发事件

实战

学会基本用法和原则之后,我们需要实战一下,下面我们给我们的Button组件编写几个单测练练手。

// button.test.ts
import { render } from '@testing-library/vue'
import Button from '../src/button'

// 基础按钮
test('it should work', () => {
  const { getByRole } = render(Button)
  getByRole('button')
})

// 插槽
test('default slot should be 按钮', () => {
  const { getByText } = render(Button)
  getByText('按钮')
})
test('default slot should work', () => {
  const { getByText } = render(Button, {
    slots: {
      default() {
        return 'button'
      }
    }
  })
  getByText('button')
})

// 按钮类型
test('default type should be secondary', () => {
  // 默认secondary
  const { getByRole } = render(Button)
  const button = getByRole('button')
  expect(button.classList.contains('s-btn--secondary')).toBe(true)
})
test('type should work', () => {
  // 默认secondary
  const { getByRole } = render(Button, {
    props: {
      type: 'primary'
    }
  })
  const button = getByRole('button')
  expect(button.classList.contains('s-btn--primary')).toBe(true)
})

运行单侧

yarn test

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z9OfA6Cc-1665996199412)(https://secure2.wostatic.cn/static/vjZESUshqZrtvszDHrpi4P/image.png?auth_key=1665995535-t3NhNpKJE6FoVhwNS7hSvc-0-5f8dd01e6c87faf5bd8206ea1744268c?auth_key=1665995989-9kMnGTE5cKsaE5rbeCDUDQ-0-91f573971af8c30578a7f6a8242e075b)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林多多@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值