Skip to content
Vitest 3
Main Navigation 指南 & API配置瀏覽器模式進階 API
3.2.0
2.1.9
1.6.1
0.34.6

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

外觀

Sidebar Navigation

簡介

為何選擇 Vitest

快速入門

功能特色

配置參考

API

測試 API 參考

模擬函式

Vi

expect

expectTypeOf

assert

assertType

指南

命令列介面

測試篩選

測試專案

報告器

程式碼覆蓋率

快照

模擬(Mocking)

平行化

型別測試

Vitest UI

內聯測試

測試上下文

測試註解

測試環境

擴展匹配器

IDE 整合

偵錯

常見錯誤

遷移指南

遷移到 Vitest 3.0

從 Jest 遷移

效能

測試效能分析

提升效能

瀏覽器模式

進階 API

與其他測試執行器的比較

本頁導覽

expectTypeOf ​

WARNING

此函數在執行時期不會執行任何操作。若要啟用型別檢查,請務必傳遞 --typecheck 旗標。

  • 型別: <T>(a: unknown) => ExpectTypeOf

not ​

  • 型別: ExpectTypeOf

您可以使用 .not 屬性來否定所有斷言。

toEqualTypeOf ​

  • 型別: <T>(expected: T) => void

此比對器會檢查型別是否完全相等。即使兩個物件的值不同,只要型別相同,此比對器就不會失敗。但若物件缺少屬性,則會失敗。

ts
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

此比對器會檢查預期型別是否為所提供型別的延伸。它與 toEqual 不同,更類似於 expect 的 toMatchObject()。使用此比對器,您可以檢查物件是否「符合」某個型別。

ts
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 來縮小型別範圍以進行進一步測試。

ts
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 從聯集中移除型別以進行進一步測試。

ts
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 來提取函數型別的返回值。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]);

WARNING

若用於非函數型別,它將返回 never,因此您將無法將其與其他比對器串聯。

parameters ​

  • 型別: ExpectTypeOf<Parameters>

您可以使用 .parameters 提取函數參數,並對其值進行斷言。參數會以陣列形式回傳。

ts
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) 來提取特定的函數參數,並對其進行其他斷言。

ts
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>

您可以使用此方法將建構函數參數提取為值陣列,並對其進行斷言。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Date).constructorParameters.toEqualTypeOf<
  [] | [string | number | Date]
>();

WARNING

若用於非函數型別,它將返回 never,因此您將無法將其與其他比對器串聯。

TIP

您也可以使用 .toBeConstructibleWith 比對器作為更具表達性的斷言。

instance ​

  • 型別: ExpectTypeOf<ConstructableInstance>

此屬性可讓您存取可在所提供類別實例上執行的比對器。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Date).instance.toHaveProperty('toISOString');

WARNING

若用於非函數型別,它將返回 never,因此您將無法將其與其他比對器串聯。

items ​

  • 型別: ExpectTypeOf<T>

您可以使用 .items 取得陣列元素的型別以執行進一步的斷言。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>();
expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>();

resolves ​

  • 型別: ExpectTypeOf<ResolvedPromise>

此比對器會提取 Promise 的解析值,以便您可以對其進行其他斷言。

ts
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),以便您可以對其進行斷言。

ts
import { expectTypeOf } from 'vitest';

function isString(v: any): v is string {
  return typeof v === 'string';
}
expectTypeOf(isString).guards.toBeString();

WARNING

如果該值不是守衛函數,則返回 never,因此您將無法將其與其他比對器串聯。

asserts ​

  • 型別: ExpectTypeOf<Assert>

此比對器會提取斷言值 (例如,assert v is number),以便您可以對其進行斷言。

ts
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

如果該值不是斷言函數,則返回 never,因此您將無法將其與其他比對器串聯。

toBeAny ​

  • 型別: () => void

使用此比對器,您可以檢查所提供的型別是否為 any 型別。如果型別過於明確,測試將會失敗。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf<any>().toBeAny();
expectTypeOf({} as any).toBeAny();
expectTypeOf('string').not.toBeAny();

toBeUnknown ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 unknown 型別。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf().toBeUnknown();
expectTypeOf({} as unknown).toBeUnknown();
expectTypeOf('string').not.toBeUnknown();

toBeNever ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 never 型別。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf<never>().toBeNever();
expectTypeOf((): never => {}).returns.toBeNever();

toBeFunction ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 function。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeFunction();
expectTypeOf((): never => {}).toBeFunction();

toBeObject ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 object。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeObject();
expectTypeOf({}).toBeObject();

toBeArray ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 Array<T>。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeArray();
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2]).toBeArray();
expectTypeOf([{}, 42]).toBeArray();

toBeString ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 string。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeString();
expectTypeOf('').toBeString();
expectTypeOf('a').toBeString();

toBeBoolean ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 boolean。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(42).not.toBeBoolean();
expectTypeOf(true).toBeBoolean();
expectTypeOf<boolean>().toBeBoolean();

toBeVoid ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 void。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf<void>().toBeVoid();

toBeSymbol ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 symbol。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(Symbol(1)).toBeSymbol();
expectTypeOf<symbol>().toBeSymbol();

toBeNull ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 null。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(null).toBeNull();
expectTypeOf<null>().toBeNull();
expectTypeOf(undefined).not.toBeNull();

toBeUndefined ​

  • 型別: () => void

此比對器會檢查所提供的型別是否為 undefined。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf(undefined).toBeUndefined();
expectTypeOf<undefined>().toBeUndefined();
expectTypeOf(null).not.toBeUndefined();

toBeNullable ​

  • 型別: () => void

此比對器會檢查您是否可以將 null 或 undefined 與所提供的型別搭配使用。

ts
import { expectTypeOf } from 'vitest';

expectTypeOf<undefined | 1>().toBeNullable();
expectTypeOf<null | 1>().toBeNullable();
expectTypeOf<undefined | null | 1>().toBeNullable();

toBeCallableWith ​

  • 型別: () => void

此比對器確保您可以使用一組參數來呼叫所提供的函數。

ts
import { expectTypeOf } from 'vitest';

type NoParam = () => void;
type HasParam = (s: string) => void;

expectTypeOf<NoParam>().toBeCallableWith();
expectTypeOf<HasParam>().toBeCallableWith('some string');

WARNING

若用於非函數型別,它將返回 never,因此您將無法將其與其他比對器串聯。

toBeConstructibleWith ​

  • 型別: () => void

此比對器確保您可以使用一組建構函數參數來建立新實例。

ts
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]>

此比對器會檢查所提供的物件上是否存在屬性。如果存在,它還會回傳該屬性型別的相同比對器集,以便您可以一個接一個地串聯斷言。

ts
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();
Pager
上一頁expect
下一頁assert

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team

https://vitest.dev/api/expect-typeof

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team