首页 > 其他分享 >Vitest快速识别

Vitest快速识别

时间:2022-12-26 15:14:55浏览次数:30  
标签:Vitest number name TestOptions 测试 识别 快速 fn string

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

标签:Vitest,number,name,TestOptions,测试,识别,快速,fn,string
From: https://www.cnblogs.com/kq981024/p/17005833.html

相关文章