Day3 vitest 之 vitest API的使用

先解释一下后续要用到的类型签名

之后的类型签名中使用了以下类型

type Awaitable<T> = T | PromiseLike<T>

type TestFunction = ()=>Awaitablt<void>
 //当测试函数返回承诺的时候,运行程序将会等待他被解析以收集异步期望,如果承诺被拒绝,测试就会失败

interface TestOptions {
  timeout?:number//如果执行时间过长,超过timeout定义的时间,测试会失败
  retry?: number//如果测试失败,会重试特定次数的测试,默认0
  repeats?:number
  //会重复相同的测试几次(repeats次),即使每次都会失败
  //如果你有retry选项,他将会使用每一个重试在每一个周期
  //对于调试随机失败有用

}

test

  • type

  • (name:string | Function,fn:Testfunction,timeout?:number | TestOptions)=>void

    您也可以提供超时(以毫秒为单位)来指定终止前等待的时间,默认为5秒.可以使用testTimeout全局配置

  • 别名

  • it

  • 用途

  • test定义了一组相关的期望,他接受测试的名称和保存测试期望的函数

  • 代码示例

//Test.test.js
import {expect,test} from "vitest"

test("should work as expect",()=>{
expect(Math.sqrt(4)).toBe(2)
})
  • 测试结果

test.extend

  • type

  • >(fixtures:Fixtures):TestAPI

  • 补充

    • Record的用法

      ```JavaScript // Record映射类型 //会将一个类型的所有属性值都映射到另外一个类型上并创造一个新的类型 // type Name = "people" | "animal" // type Person = { // name: string; // age: number; // } // type newType = Record // let res: newType = { // people: { // name: '', // age: 12 // }, // animal: { // name: '', // age: 12 // } // }

    - &的用法

      ```JavaScript
interface AA {
    name: string

}
interface BB {
    name: string,
    age: number

}
type A = AA & BB;
let a: A = { name: "", age: 11 }
let b: A = { name: "" }//不能将类型“{ name: string; }”分配给类型“A”。类型 "{ name: string; }" 中缺少属性 "age",但类型 "BB" 中需要该属性。
  • 别名

  • it.extend

  • 版本:vitest 0.32.3

  • 功能

  • 使用test.extend通过自定义装置扩展测试上下文,将返回一个新的test并且他也是可扩展的

  • 可以根据需要扩展它来组合更多的灯具或者覆盖现有的灯具

    更多可参见扩展测试上下文

  • 代码

import { expect, test } from "vitest"

const todos = [];
const archive = [];

const myTest = test.extend({
    todos: async ({ task }, use) => {

        todos.push(1, 2, 3);
        await use(todos)
        todos.length = 0;
    },
    archive
})
myTest('add item', ({ todos }) => {

    expect(todos.length).toBe(3)
    todos.push(4)
    expect(todos.length).toBe(4)
})
  • 测试结果

test.skip

  • type

  • (name:string | Function,fn:TestFunciton,timeout?:number | TestOptions)=>void

  • 别名

  • it.skip

  • 功能

  • 如果你想跳过某些测试,但由于任何原因不想删除代码,则可以使用test.skip来避免运行他们

  • 代码1

//skip.test.js
import { assert, test } from "vitest"

test.skip("skipped test", () => {
    //Test skipped,no error
    assert.equal(Math.sqrt(4), 3)

})
  • 代码2

  • 通过在其上下文动态调用skip来跳过测试

import { assert, test } from "vitest"

// test.skip("skipped test", () => {
//     //Test skipped,no error
//     assert.equal(Math.sqrt(4), 3)

// })
test('skipped test', (context) => {
    context.skip()
    assert.equal(Math.sqrt(4), 3)
})
  • 测试结果1

  • 测试结果2

test.skipif

  • type

  • (condition:any)=>Test

  • 别名

  • it.skipIf

  • 功能

  • 在某些情况下,您可能会在不同的环境中多次运行测试,并且某些测试可能是特定于环境的

  • 您可以使用test,skipIf在条件为真的时候跳过测试,而不用if包装测试代码

  • 注意

  • 当使用vitest作为类型检查器的时候,不能使用此语法

  • 代码

import { assert, test } from 'vitest'

const isDev = process.env.NODE_ENV === 'development'

test.skipIf(!isDev)('prod only test', () => {
    // this test only runs in production
})
  • 测试结果

test.runIf

  • type

  • (condition:any)=>Test

  • 别名

  • it.runIf

  • 功能

  • test.skipIf相反

  • 注意

  • 当使用vitest作为类型检查器的时候,不能使用此语法

  • 代码

import { assert, test } from 'vitest'

const isDev = process.env.NODE_ENV === 'development'

test.runIf(isDev)('dev only test', () => {
    // this test only runs in development
})
  • 测试结果

test.only

  • type

  • (name:string | Function,fn:TestFunction,timeout?:number)=>void

    您也可以提供超时(以毫秒为单位)来指定终止前等待的时间,默认为5秒.可以使用testTimeout全局配置

  • 别名

  • it.only

  • 功能

    使用test.only仅运行给定套件中的某些测试,这在调试的时候很有用

    说人话:

    就初步体验而言:观察到同一个文件中出现了test.only,同一个文件的其他test会被跳过,但是其他测试文件不会被影响跳过

    有时候在某个文件中运行Only测试非常有用,忽略整个测试套件中的所有其他测试(whole会污染测试输出),可以使用包含相关测试的特定文件运行vitest 即 vitest interesting.test.js

  • 代码

import { assert, test } from 'vitest'

test.only('test', () => {
    //只运行这个测试和其他标记为Only的测试
    assert.equal(Math.sqrt(4), 2)
})

test(" as expect", () => {
    expect(Math.sqrt(4)).toBe(2)
})
  • 测试结果1

  • 测试结果2

test.concurrent

  • type

  • (name:string | Function,fn:TestFunction,timeout?:number)=>void

  • 别名

  • it.concurrent

  • 注意

  • 当使用 Vitest 作为类型检查器时,不能使用此语法。

  • 功能

  • 标记要并行运行的连续测试,接收测试名称,包含要收集到的测试的异步函数,以及可选的超时(以毫秒为单位)

  • 代码1

import { describe, test } from "vitest"

describe("suite", () => {

    test("serial test", async () => { /** * **/ })
    //标记为并发的两个测试将并行运行
    test.concurrent("concurrent test1", async () => {/** ** **/ })
    test.concurrent("concurrent test2", async () => {/** ** **/ })
})
  • 测试结果1

  • 代码2

    test.skip,test.only,test.todo适用于并发测试一下组合均有效

import { describe, test } from "vitest"

describe("suite", () => {

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值