Vitestで型のtest
コマンドで検査する
型だけ見てるっぽくて通常のテストコードの間違いは無視されるっぽい

というか、型のエラーも無視されている...なぜ?
必要であれば
typecheck
の設定も書く
ref例えば、libraryを作っている場合は、tsconfigでは **.test.ts
をexcludeするが、test時は見て欲しい
vitest.config.tsexport default defineConfig({
test: {
typecheck: {
include: ['src/**/*.test.{ts,tsx}'],
},
},
});
以下のいずれもtestできる
( 値SUT
| 型Sut
)が、( Expect値の型
Expected型
)と一致する
tsexpectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>()
expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 })
expectTypeOf<{ a: number}>().toEqualTypeOf<{ a: number }>()
expectTypeOf<{ a: number}>().toEqualTypeOf({ a: 1 })
not
toEqualTypeOf
toMatchTypeOf
extract
, exclude
union typeのテストに便利
関数周り
returns
関数の返り値の型の検査
役割が
assertType
と被っているような気がする(?)

tsconst id = <T>(x: T) => x;
expectTypeOf(() => id(1)).returns.toEqualTypeOf<1>(); // 引数が1の時、返り値の型は「1」になる (numberではなく)
parameters
関数の引数の型の検査
parameter
関数の引数の型を個別に検査
その関数に対しこの引数が呼べるかを検査
constructorParameters
instance
items
resolves
guards
asserts
toBeAny
toBeUnknown
toBeNever
toBeFunction
toBeObject
toBeArray
toBeString
toBeBoolean
toBeVoid
toBeSymbol
toBeNull
toBeUndefined
toBeNullable
toBeConstructibleWith
toHaveProperty
tstest('my types work properly', () => {
expectTypeOf(mount).toBeFunction()
expectTypeOf(mount).parameter(0).toMatchTypeOf<{ name: string }>()
// @ts-expect-error name is a string
assertType(mount({ name: 42 }))
})