VitestのMatcher
from Vitest
docs
#WIP
not
vi.toBe
docs
primitive値が同じ、あるいは、objectの参照が同じ
Object.isと同じ
メジャーだけど微妙に使わなそうじゃない?mrsekut.icon
参照が同じことをtestしたい時ってどういうケース?
vi.toBeCloseTo
docs
/mrsekut-book-4798178187/085
JavaScriptの小数計算の誤差を加味したテストを書く
toBeDefined
toBeUndefined
toBeTruthy
toBeFalsy
toBeNull
toBeNaN
toBeTypeOf
toBeInstanceOf
toBeGreaterThan
toBeGreaterThanOrEqual
toBeLessThan
toBeLessThanOrEqual
小数があると使えないのダルいな
vi.toEqual
docs
値が同じ、構造が同じ
vi.toStrictEqual
docs
toEqual()とほぼ同じだが、より厳格
例えば、
{a: undefined, b: 2}と{b:2}を区別する
[,1]と[undefined,1]を区別する
逆に、あえてtoEqualを使うケースって何がある #??
toContain
配列内に特定の値があるかどうか
toContainEqual
toHaveLength
vi.toHaveProperty
特定のpropertyが存在するかどうか
toMatch
正規表現を使って文字列がマッチするかどうか
vi.toMatchObject
docs
objectのsubsetが含まれるかどうか
vi.toThrowError
https://vitest.dev/api/expect.html#tothrowerror
vi.toMatchSnapshot
vi.toMatchInlineSnapshot
vi.toThrowErrorMatchingSnapshot
vi.toThrowErrorMatchingInlineSnapshot
vi.toHaveBeenCalled
vi.toHaveBeenCalledTimes
呼び出された回数の検証
vi.toHaveBeenCalledWith
呼び出されたときの引数の検証
/mrsekut-book-4798178187/120
toHaveBeenLastCalledWith
toHaveBeenNthCalledWith
vi.toHaveReturned
toHaveReturnedTimes
toHaveReturnedWith
toHaveLastReturnedWith
toHaveNthReturnedWith
vi.toSatisfy
docs
booleanを返す関数を渡してtrueを返すかどうか
code:例.ts
const isOdd = (value: number) => value % 2 !== 0
it('pass with 0', () => {
expect(1).toSatisfy(isOdd)
})
vi.toBeとかでなくわざわざこれ使うことあるの?別にいいけど
resolves
Promiseのresolve値を検証
/mrsekut-book-4798178187/090 (3-7 非同期処理のテスト)
rejects
/mrsekut-book-4798178187/092
vi.expect.assertions
/mrsekut-book-4798178187/094
test code自体が間違っていてexpect()が呼ばれていない、ということを防ぐ
expect.hasAssertions
expect.anything
expect.any
expect.arrayContaining
expect.objectContaining
expect.stringContaining
expect.stringMatching
expect.addSnapshotSerializer
vi.expect.extend
matcherを独自に拡張する