Skip to content
หัวข้อคำอธิบาย
ความสามารถของ Vitestรองรับทั้ง Chai และ Jest
วิธีการขยาย matchersสามารถใช้ได้ทั้ง chai.use API และ expect.extend
เนื้อหาของคำแนะนำนี้เน้นการใช้ expect.extend
สำหรับผู้สนใจ Chai APIแนะนำให้ดูคู่มือของ Chai โดยตรง
วิธีการขยาย default matchersใช้ expect.extend โดยส่ง object ที่มี matchers ที่ต้องการเพิ่ม
ts
expect.extend({
  toBeFoo(received, expected) {
    const { isNot } = this
    return {
      // do not alter your "pass" based on isNot. Vitest does it for you
      pass: received === 'foo',
      message: () => `${received} is${isNot ? ' not' : ''} foo`
    }
  }
})
ts
import type { Assertion, AsymmetricMatchersContaining } from 'vitest'

interface CustomMatchers<R = unknown> {
  toBeFoo: () => R
}

declare module 'vitest' {
  interface Assertion<T = any> extends CustomMatchers<T> {}
  interface AsymmetricMatchersContaining extends CustomMatchers {}
}

Released under the MIT License