vitest

安装

1
pnpm add -D vitest @vue/test-utils jsdom @vitejs/plugin-vue @vitejs/plugin-vue-jsx

配置 Vitest

在项目根目录创建 vitest.config.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'

export default defineConfig({
  plugins: [
    vue(), // 关键:让 Vitest 能解析 .vue 文件
    VueJsx(),
  ],
  test: {
    globals: true,
    environment: 'jsdom',
  },
})

运行测试

package.json 中添加脚本:

1
2
3
4
5
6
{
  "scripts": {
    "test": "vitest",
    "test:coverage": "vitest run --coverage"
  }
}

运行测试:

1
npm test

vitest
http://xiaowhang.github.io/archives/27327315/
作者
Xiaowhang
发布于
2025年2月23日
许可协议