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