Vitest快速识别
Vitest接口快速识别
npm i vitest -D
// package.json
{
"scripts": {
"test": "vitest",
"coverage": "vitest run --coverage"
}
}
// vite.config.ts
test: {
globals: true,
environment: 'jsdom',
}
test: 定义了一组关于测试期望的方法( 别名it)
it(name: string, fn: TestFunction, timeout?: number | TestOptions) // 定义基本测试方法
it.skip(name: string, fn: TestFunction, timeout?: number | TestOptions) // 跳过测试
it.skipIf(condition: boolean) => Test // 满足条件不测试
it.runIf(condition: boolean)=>Test // 满足条件测试
it.only(name: string, fn: TestFunction, timeout?: number | TestOptions) // 只测试only的测试用例
it.concurrent(name: string, fn: TestFunction, timeout?: number | TestOptions) // concurrent标记的测试用力并行测试
it.todo(name: string) // 在测试报告中显示还有多少测试未实现
it.fails(name: string, fn: TestFunction, timeout?: number) // 显式断言测试失败
it.each(cases: ReadonlyArray<T>, ...args: any[]) // 不同变量运行相同测试
bench:定义了一系列操作的函数,多次运行此函数以显示不同的性能结果
bench(name: string, fn: BenchFunction, options?: BenchOptions)
bench.skip(name: string, fn: BenchFunction, options?: BenchOptions) // 跳过某些基准测试
bench.only(name: string, fn: BenchFunction, options?: BenchOptions) // 指定只测试某些基准测试
bench.todo(name: string) // 存根基准测试
describe:定义测试套件,使得测试报告更加清晰
descibe(name:string,fn:TestFunction|BenchFunction,options?: number | TestOptions)
describe.skip(name: string, fn: TestFunction, options?: number | TestOptions) // 跳过某些测试套件测试
describe.only(name: string, fn: TestFunction, options?: number | TestOptions) // 仅运行指定测试套件
describe.concurrent(name: string, fn: TestFunction, options?: number | TestOptions) // 测试套件中每个用例并发测试
describe.shuffle(name: string, fn: TestFunction, options?: number | TestOptions) // 随机运行套件中的测试用例
describe.todo(name: string) //将稍后实现的测试套件进行存档。测试报告中将显示一个记录,以便你知道还多少条未实现的测试
describe.each(ases: ReadonlyArray<T>, ...args: any[]): (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) // 测试套件中的测试用例用多个相同的变量进行测试
expect:在当前上下文中定义断言,判断测试是否通过
详情见:https://cn.vitest.dev/api/#expecthttps://cn.vitest.dev/api/#expect
Setup and Teardown:测试的生命周期
beforeEach(fn: () => Awaitable<void>, timeout?: number) // 在每个测试前的操作
afterEach(fn: () => Awaitable<void>, timeout?: number) // 在每个测试后的操作
beforeAll(fn: () => Awaitable<void>, timeout?: number) // 在所有测试操作一
afterAll(fn: () => Awaitable<void>, timeout?: number) // 在所有测试后调用一次:
Vi工具库
详情见:https://cn.vitest.dev/api/#vi
Mock相关
详见:https://cn.vitest.dev/api/#mockinstance-methods
https://cn.vitest.dev/api/#mockinstance-properties
分类:
测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律