expectTypeOf
- 類型:
<T>(a: unknown) => ExpectTypeOf
not
- 類型:
ExpectTypeOf
您可以使用 .not
屬性來否定所有斷言。
toEqualTypeOf
- 類型:
<T>(expected: T) => void
此匹配器會檢查類型是否完全相等。如果兩個物件的值不同,但類型相同,此匹配器不會失敗。但如果物件缺少屬性,則會失敗。
import { expectTypeOf } from 'vitest';
expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>();
expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 });
expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 });
expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>();
toMatchTypeOf
- 類型:
<T>(expected: T) => void
此匹配器會檢查 expectTypeOf
的類型是否擴展了提供的類型。它與 toEqual
不同,更類似於 expect 的 toMatchObject()
。使用此匹配器,您可以檢查物件是否「符合」某種類型。
import { expectTypeOf } from 'vitest';
expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 });
expectTypeOf<number>().toMatchTypeOf<string | number>();
expectTypeOf<string | number>().not.toMatchTypeOf<number>();
extract
- 類型:
ExpectTypeOf<ExtractedUnion>
您可以使用 .extract
來縮小類型範圍,以便進行進一步的測試。
import { expectTypeOf } from 'vitest';
type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T };
interface CSSProperties {
margin?: string;
padding?: string;
}
function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
return {};
}
const cssProperties: CSSProperties = { margin: '1px', padding: '2px' };
expectTypeOf(getResponsiveProp(cssProperties))
.extract<{ xs?: any }>() // 從聯合類型中提取某個類型
.toEqualTypeOf<{
xs?: CSSProperties;
sm?: CSSProperties;
md?: CSSProperties;
}>();
expectTypeOf(getResponsiveProp(cssProperties))
.extract<unknown[]>() // 從聯合類型中提取一個陣列
.toEqualTypeOf<CSSProperties[]>();
WARNING
如果在聯合類型中找不到任何類型,.extract
將返回 never
。
exclude
- 類型:
ExpectTypeOf<NonExcludedUnion>
您可以使用 .exclude
從聯合類型中移除類型,以便進行進一步的測試。
import { expectTypeOf } from 'vitest';
type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T };
interface CSSProperties {
margin?: string;
padding?: string;
}
function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
return {};
}
const cssProperties: CSSProperties = { margin: '1px', padding: '2px' };
expectTypeOf(getResponsiveProp(cssProperties))
.exclude<unknown[]>()
.exclude<{ xs?: unknown }>() // 或者直接 .exclude<unknown[] | { xs?: unknown }>()
.toEqualTypeOf<CSSProperties>();
WARNING
如果在聯合類型中找不到任何類型,.exclude
將返回 never
。
returns
- 類型:
ExpectTypeOf<ReturnValue>
您可以使用 .returns
來提取函數的返回值類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]);
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
parameters
- 類型:
ExpectTypeOf<Parameters>
您可以使用 .parameters
提取函數的參數類型,以對其值執行斷言。參數會以陣列形式回傳。
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().parameters.toEqualTypeOf<[]>();
expectTypeOf<HasParam>().parameters.toEqualTypeOf<[string]>();
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
TIP
您也可以使用 .toBeCallableWith
匹配器作為更具表達力的斷言。
parameter
- 類型:
(nth: number) => ExpectTypeOf
您可以使用 .parameter(number)
方法提取特定位置的函數參數類型,以對其執行其他斷言。
import { expectTypeOf } from 'vitest';
function foo(a: number, b: string) {
return [a, b];
}
expectTypeOf(foo).parameter(0).toBeNumber();
expectTypeOf(foo).parameter(1).toBeString();
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
constructorParameters
- 類型:
ExpectTypeOf<ConstructorParameters>
您可以使用此方法提取建構函式的參數類型,並以陣列形式回傳,以便對其執行斷言。
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).constructorParameters.toEqualTypeOf<
[] | [string | number | Date]
>();
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
TIP
您也可以使用 .toBeConstructibleWith
匹配器作為更具表達力的斷言。
instance
- 類型:
ExpectTypeOf<ConstructableInstance>
此屬性提供對可以對提供的類別的實例執行的匹配器的訪問權限。
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).instance.toHaveProperty('toISOString');
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
items
- 類型:
ExpectTypeOf<T>
您可以使用 .items
獲取陣列元素的類型,以執行進一步的斷言。
import { expectTypeOf } from 'vitest';
expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>();
expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>();
resolves
- 類型:
ExpectTypeOf<ResolvedPromise>
此匹配器會提取 Promise
的解析值類型,因此您可以對其執行其他斷言。
import { expectTypeOf } from 'vitest';
async function asyncFunc() {
return 123;
}
expectTypeOf(asyncFunc).returns.resolves.toBeNumber();
expectTypeOf(Promise.resolve('string')).resolves.toBeString();
WARNING
如果在非 Promise 類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
guards
- 類型:
ExpectTypeOf<Guard>
此匹配器會提取類型守衛(例如,v is number
)的類型,因此您可以對其執行斷言。
import { expectTypeOf } from 'vitest';
function isString(v: any): v is string {
return typeof v === 'string';
}
expectTypeOf(isString).guards.toBeString();
WARNING
若該值非類型守衛函數,則會返回 never
,因此您將無法將其與其他匹配器串連。
asserts
- 類型:
ExpectTypeOf<Assert>
此匹配器會提取 assertion 函數(例如,assert v is number
)的類型,因此您可以對其執行斷言。
import { expectTypeOf } from 'vitest';
function assertNumber(v: any): asserts v is number {
if (typeof v !== 'number') throw new TypeError('Nope !');
}
expectTypeOf(assertNumber).asserts.toBeNumber();
WARNING
若該值非 assertion 函數,則會返回 never
,因此您將無法將其與其他匹配器串連。
toBeAny
- 類型:
() => void
使用此匹配器,您可以檢查提供的類型是否為 any
類型。若類型過於具體,測試就會失敗。
import { expectTypeOf } from 'vitest';
expectTypeOf<any>().toBeAny();
expectTypeOf({} as any).toBeAny();
expectTypeOf('string').not.toBeAny();
toBeUnknown
- 類型:
() => void
此匹配器檢查提供的類型是否為 unknown
類型。
import { expectTypeOf } from 'vitest';
expectTypeOf().toBeUnknown();
expectTypeOf({} as unknown).toBeUnknown();
expectTypeOf('string').not.toBeUnknown();
toBeNever
- 類型:
() => void
此匹配器檢查提供的類型是否為 never
類型。
import { expectTypeOf } from 'vitest';
expectTypeOf<never>().toBeNever();
expectTypeOf((): never => {}).returns.toBeNever();
toBeFunction
- 類型:
() => void
此匹配器會檢查提供的類型是否為函數類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeFunction();
expectTypeOf((): never => {}).toBeFunction();
toBeObject
- 類型:
() => void
此匹配器會檢查提供的類型是否為物件類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeObject();
expectTypeOf({}).toBeObject();
toBeArray
- 類型:
() => void
此匹配器會檢查提供的類型是否為陣列類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeArray();
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2]).toBeArray();
expectTypeOf([{}, 42]).toBeArray();
toBeString
- 類型:
() => void
此匹配器會檢查提供的類型是否為字串類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeString();
expectTypeOf('').toBeString();
expectTypeOf('a').toBeString();
toBeBoolean
- 類型:
() => void
此匹配器會檢查提供的類型是否為布林類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeBoolean();
expectTypeOf(true).toBeBoolean();
expectTypeOf<boolean>().toBeBoolean();
toBeVoid
- 類型:
() => void
此匹配器檢查提供的類型是否為 void
類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf<void>().toBeVoid();
toBeSymbol
- 類型:
() => void
此匹配器檢查提供的類型是否為 symbol
類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(Symbol(1)).toBeSymbol();
expectTypeOf<symbol>().toBeSymbol();
toBeNull
- 類型:
() => void
此匹配器檢查提供的類型是否為 null
類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(null).toBeNull();
expectTypeOf<null>().toBeNull();
expectTypeOf(undefined).not.toBeNull();
toBeUndefined
- 類型:
() => void
此匹配器檢查提供的類型是否為 undefined
類型。
import { expectTypeOf } from 'vitest';
expectTypeOf(undefined).toBeUndefined();
expectTypeOf<undefined>().toBeUndefined();
expectTypeOf(null).not.toBeUndefined();
toBeNullable
- 類型:
() => void
此匹配器會檢查提供的類型是否可以為 null
或 undefined
。
import { expectTypeOf } from 'vitest';
expectTypeOf<1 | undefined>().toBeNullable();
expectTypeOf<1 | null>().toBeNullable();
expectTypeOf<1 | undefined | null>().toBeNullable();
toBeCallableWith
- 類型:
() => void
此匹配器確保您可以使用一組參數調用提供的函數類型。
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().toBeCallableWith();
expectTypeOf<HasParam>().toBeCallableWith('some string');
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
toBeConstructibleWith
- 類型:
() => void
此匹配器確保您可以使用一組建構函式的參數建立一個新的實例。
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).toBeConstructibleWith(new Date());
expectTypeOf(Date).toBeConstructibleWith('01-01-2000');
WARNING
如果在非函數類型上使用,它會返回 never
,因此您將無法將其與其他匹配器串連。
toHaveProperty
- 類型:
<K extends keyof T>(property: K) => ExpectTypeOf<T[K>
此匹配器檢查提供的物件類型上是否存在屬性。如果存在,它還會為此屬性的類型返回相同的匹配器集合,因此您可以一個接一個地串連斷言。
import { expectTypeOf } from 'vitest';
const obj = { a: 1, b: '' };
expectTypeOf(obj).toHaveProperty('a');
expectTypeOf(obj).not.toHaveProperty('c');
expectTypeOf(obj).toHaveProperty('a').toBeNumber();
expectTypeOf(obj).toHaveProperty('b').toBeString();
expectTypeOf(obj).toHaveProperty('a').not.toBeString();