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 を選ぶ理由

はじめに

機能

Vitest の設定

API

テストAPIリファレンス

モック関数

Vi

expect

expectTypeOf

assert

assertType

ガイド

コマンドラインインターフェース

テストフィルタリング

テストプロジェクト

レポーター

カバレッジ

スナップショット

モック

並列処理

型テスト

Vitest UI

インソース・テスト

テストコンテキスト

テストアノテーション

テスト環境

マッチャーの拡張

IDE統合

デバッグ

よくあるエラー

移行ガイド

Vitest 3.0 への移行

Jest からの移行

パフォーマンス

テストパフォーマンスのプロファイリング

パフォーマンスの向上

ブラウザーモード

上級者向けAPI

他のテストランナーとの比較

このページの内容

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

このマッチャーは、期待する型が指定された型を拡張しているか(互換性があるか)をチェックします。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 }>() // ユニオンから特定の型(例: { 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ライセンス の下で公開されています。

Copyright (c) 2021-Present Vitest Team

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

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

Copyright (c) 2021-Present Vitest Team