Skip to content
Vitest 2
Main Navigation ガイドAPI設定ブラウザモード高度な
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

テスト API リファレンス

モック関数

Vi

expect

expectTypeOf

assert

assertType

このページの内容

expectTypeOf ​

WARNING

実行時、この関数は何もしません。 型チェックを有効にするには、--typecheck フラグを渡すことを忘れないでください。

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

not ​

  • 型: ExpectTypeOf

.not プロパティを使用すると、すべてのアサーションを否定できます。

toEqualTypeOf ​

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

このマッチャーは、型が完全に一致するかどうかをチェックします。2 つのオブジェクトの値が異なっていても、型が同じであれば、このマッチャーは成功します。ただし、オブジェクトにプロパティが欠けている場合は失敗します。

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

このマッチャーは、期待される型が提供された型に代入可能(assignable)かどうかをチェックします。これは toEqualTypeOf とは異なり、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 }>() // ユニオン型から { xs?: any } を抽出します
  .toEqualTypeOf<{
    xs?: CSSProperties;
    sm?: CSSProperties;
    md?: CSSProperties;
  }>();

expectTypeOf(getResponsiveProp(cssProperties))
  .extract<unknown[]>() // ユニオン型から 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 型であるかどうかをチェックできます。型が 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ライセンス の下で公開されています。

Copyright (c) 2024 Mithril Contributors

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

MITライセンス の下で公開されています。

Copyright (c) 2024 Mithril Contributors