Skip to content
Vitest 0
Main Navigation ガイド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 UI

ブラウザモード(実験的)

ソース内テスト

テストコンテキスト

テスト環境

マッチャー拡張

IDE連携機能

デバッグ

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

移行ガイド

よくあるエラー

API

テスト API リファレンス

モック関数

Vi

expect

expectTypeOf

assertType

設定

Vitest の設定

このページの内容

テスト API リファレンス ​

以下の型は、下記の型シグネチャで使用されます。

ts
type Awaitable<T> = T | PromiseLike<T>;
type TestFunction = () => Awaitable<void>;

interface TestOptions {
  /**
   * 実行時間が長すぎる場合にテストを失敗させます。
   */
  timeout?: number;
  /**
   * テストが失敗した場合に、指定された回数だけテストを再試行します。
   *
   * @default 0
   */
  retry?: number;
  /**
   * 毎回失敗した場合でも、同じテストを複数回繰り返します。
   * "retry" オプションが設定されている場合、失敗時には各サイクルで設定された再試行回数だけ再試行します。
   * ランダムな失敗をデバッグするのに役立ちます。
   *
   * @default 0
   */
  repeats?: number;
}

テスト関数が Promise を返す場合、ランナーは非同期処理の結果を収集するために、Promise が解決されるまで待機します。Promise が拒否された場合、テストは失敗します。

TIP

Jest では、TestFunction は (done: DoneCallback) => void 型にもなり得ます。この形式を使用する場合、done が呼び出されるまでテストは完了しません。async 関数を使用しても同じことができます。移行ガイドの Done Callback セクションを参照してください。

test ​

  • 型: (name: string | Function, fn: TestFunction, timeout?: number | TestOptions) => void

  • エイリアス: it

    test は、関連する期待値のセットを定義します。テスト名と、テスト対象の期待値を保持する関数を受け取ります。

    オプションで、タイムアウトまでの待機時間をミリ秒単位で指定できます。デフォルトは 5 秒で、testTimeout でグローバルに設定できます。

    ts
    import { expect, test } from 'vitest';
    
    test('should work as expected', () => {
      expect(Math.sqrt(4)).toBe(2);
    });

test.extend ​

  • 型: <T extends Record<string, any>>(fixtures: Fixtures<T>): TestAPI<ExtraContext & T>

  • エイリアス: it.extend

  • バージョン: Vitest 0.32.3

    test.extend を使用して、カスタムフィクスチャでテストコンテキストを拡張します。これにより、新しい test が返され、拡張可能にもなるため、必要に応じて拡張することで、より多くのフィクスチャを構成したり、既存のフィクスチャをオーバーライドしたりできます。詳細については、テストコンテキストの拡張を参照してください。

    ts
    import { expect, test } from 'vitest';
    
    const todos = [];
    const archive = [];
    
    const myTest = test.extend({
      todos: async ({ task }, use) => {
        todos.push(1, 2, 3);
        await use(todos);
        todos.length = 0;
      },
      archive,
    });
    
    myTest('add item', ({ todos }) => {
      expect(todos.length).toBe(3);
    
      todos.push(4);
      expect(todos.length).toBe(4);
    });

test.skip ​

  • 型: (name: string | Function, fn: TestFunction, timeout?: number | TestOptions) => void

  • エイリアス: it.skip

    特定のテストの実行をスキップしたいが、何らかの理由でコードを削除したくない場合は、test.skip を使用して実行を回避できます。

    ts
    import { assert, test } from 'vitest';
    
    test.skip('skipped test', () => {
      // Test skipped, no error
      assert.equal(Math.sqrt(4), 3);
    });

    コンテキストの skip メソッドを動的に呼び出すことによって、テストをスキップすることもできます。

    ts
    import { assert, test } from 'vitest';
    
    test('skipped test', context => {
      context.skip();
      // Test skipped, no error
      assert.equal(Math.sqrt(4), 3);
    });

test.skipIf ​

  • 型: (condition: any) => Test

  • エイリアス: it.skipIf

    異なる環境でテストを複数回実行する場合、テストの一部が環境固有であることがあります。テストコードを if で囲む代わりに、条件が真と評価される場合は常に test.skipIf を使用してテストをスキップできます。

    ts
    import { assert, test } from 'vitest';
    
    const isDev = process.env.NODE_ENV === 'development';
    
    test.skipIf(isDev)('prod only test', () => {
      // このテストは production 環境でのみ実行されます
    });

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

test.runIf ​

  • 型: (condition: any) => Test

  • エイリアス: it.runIf

    test.skipIf の反対です。

    ts
    import { assert, test } from 'vitest';
    
    const isDev = process.env.NODE_ENV === 'development';
    
    test.runIf(isDev)('dev only test', () => {
      // このテストは development 環境でのみ実行されます
    });

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

test.only ​

  • 型: (name: string | Function, fn: TestFunction, timeout?: number) => void

  • エイリアス: it.only

    test.only を使用して、特定のスイートで特定のテストのみを実行します。これはデバッグに役立ちます。

    オプションで、タイムアウトまでの待機時間をミリ秒単位で指定できます。デフォルトは 5 秒で、testTimeout でグローバルに設定できます。

    ts
    import { assert, test } from 'vitest';
    
    test.only('test', () => {
      // このテスト(および only が付いた他のテスト)のみが実行されます
      assert.equal(Math.sqrt(4), 2);
    });

    出力が煩雑になるため、テストスイート全体から他のすべてのテストを無視して、特定のファイル内の only テストを実行すると非常に便利な場合があります。

    これを行うには、対象のテストを含む特定のファイルで vitest を実行します。

    # vitest interesting.test.ts

test.concurrent ​

  • 型: (name: string | Function, fn: TestFunction, timeout?: number) => void

  • エイリアス: it.concurrent

    test.concurrent は、複数のテストを並行して実行するようにマークします。テスト名、テストを含む非同期関数、およびオプションのタイムアウト(ミリ秒単位)を受け取ります。

    ts
    import { describe, test } from 'vitest';
    
    // concurrent が付いた 2 つのテストは並行して実行されます
    describe('suite', () => {
      test('serial test', async () => {
        /* ... */
      });
      test.concurrent('concurrent test 1', async () => {
        /* ... */
      });
      test.concurrent('concurrent test 2', async () => {
        /* ... */
      });
    });

    test.skip、test.only、および test.todo は、並行テストで動作します。次のすべての組み合わせが有効です。

    ts
    test.concurrent(/* ... */);
    test.skip.concurrent(/* ... */); // または test.concurrent.skip(/* ... */)
    test.only.concurrent(/* ... */); // または test.concurrent.only(/* ... */)
    test.todo.concurrent(/* ... */); // または test.concurrent.todo(/* ... */)

    並行テストを実行する場合、スナップショットとアサーションは、正しいテストが検出されるように、ローカルの テストコンテキスト から expect を使用する必要があります。

    ts
    test.concurrent('test 1', async ({ expect }) => {
      expect(foo).toMatchSnapshot();
    });
    test.concurrent('test 2', async ({ expect }) => {
      expect(foo).toMatchSnapshot();
    });

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

test.todo ​

  • 型: (name: string | Function) => void

  • エイリアス: it.todo

    test.todo を使用して、後で実装されるテストのスタブを作成します。テストのレポートに項目が表示されるため、実装する必要があるテストの数がわかります。

    ts
    // このテストのエントリがレポートに表示されます
    test.todo('unimplemented test');

test.fails ​

  • 型: (name: string | Function, fn: TestFunction, timeout?: number) => void

  • エイリアス: it.fails

    test.fails を使用して、アサーションが意図的に失敗することを示します。

    ts
    import { expect, test } from 'vitest';
    
    function myAsyncFunc() {
      return new Promise(resolve => resolve(1));
    }
    test.fails('fail test', async () => {
      await expect(myAsyncFunc()).rejects.toBe(1);
    });

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

test.each ​

  • 型: (cases: ReadonlyArray<T>, ...args: any[]) => void

  • エイリアス: it.each

    異なる変数で同じテストを実行する必要がある場合は、test.each を使用します。 テスト関数パラメータの順序で、テスト名に printf 形式の書式でパラメータを挿入できます。

    • %s: string
    • %d: number
    • %i: integer
    • %f: floating point value
    • %j: json
    • %o: object
    • %#: テストケースのインデックス
    • %%: 単一のパーセント記号 ('%')
    ts
    test.each([
      [1, 1, 2],
      [1, 2, 3],
      [2, 1, 3],
    ])('add(%i, %i) -> %i', (a, b, expected) => {
      expect(a + b).toBe(expected);
    });
    
    // this will return
    // 以下のように出力されます
    // ✓ add(1, 1) -> 2
    // ✓ add(1, 2) -> 3
    // ✓ add(2, 1) -> 3

    オブジェクトを引数として使用している場合は、$ プレフィックスを使用してオブジェクトプロパティにアクセスすることもできます。

    ts
    test.each([
      { a: 1, b: 1, expected: 2 },
      { a: 1, b: 2, expected: 3 },
      { a: 2, b: 1, expected: 3 },
    ])('add($a, $b) -> $expected', ({ a, b, expected }) => {
      expect(a + b).toBe(expected);
    });
    
    // this will return
    // 以下のように出力されます
    // ✓ add(1, 1) -> 2
    // ✓ add(1, 2) -> 3
    // ✓ add(2, 1) -> 3

    オブジェクトを引数として使用している場合は、. を使用してオブジェクト属性にアクセスすることもできます。

    ts
    test.each`
      a             | b      | expected
      ${{ val: 1 }} | ${'b'} | ${'1b'}
      ${{ val: 2 }} | ${'b'} | ${'2b'}
      ${{ val: 3 }} | ${'b'} | ${'3b'}
    `('add($a.val, $b) -> $expected', ({ a, b, expected }) => {
      expect(a.val + b).toBe(expected);
    });
    
    // this will return
    // 以下のように出力されます
    // ✓ add(1, b) -> 1b
    // ✓ add(2, b) -> 2b
    // ✓ add(3, b) -> 3b

    Vitest 0.25.3 以降では、テンプレートリテラルの表形式も使用できます。

    • 最初の行は、| で区切られた列名である必要があります。
    • 1 つ以上の後続のデータ行は、${value} 構文を使用してテンプレートリテラル式として提供されます。
    ts
    test.each`
      a             | b      | expected
      ${1}          | ${1}   | ${2}
      ${'a'}        | ${'b'} | ${'ab'}
      ${[]}         | ${'b'} | ${'b'}
      ${{}}         | ${'b'} | ${'[object Object]b'}
      ${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
    `('returns $expected when $a is added $b', ({ a, b, expected }) => {
      expect(a + b).toBe(expected);
    });

    TestContext にアクセスする場合は、単一のテストで describe.each を使用します。

TIP

Vitest は、chai format メソッドで $values を処理します。値が過度に切り捨てられる場合は、構成ファイルで chaiConfig.truncateThreshold を増やすことができます。

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

bench ​

  • 型: (name: string | Function, fn: BenchFunction, options?: BenchOptions) => void

Vitest において、ベンチマークとは一連の操作を定義する関数を指します。Vitest はこの関数を複数回実行して、さまざまなパフォーマンス結果を表示します。

Vitest は、内部で tinybench ライブラリを使用しており、3 番目の引数として使用できるすべてのオプションを継承しています。

ts
import { bench } from 'vitest';

bench(
  'normal sorting',
  () => {
    const x = [1, 5, 4, 2, 3];
    x.sort((a, b) => {
      return a - b;
    });
  },
  { time: 1000 }
);
ts
export interface Options {
  /**
   * ベンチマークタスクを実行するのに必要な時間(ミリ秒)。
   * @default 500
   */
  time?: number;

  /**
   * 指定時間が経過した後でも、タスクを実行する必要がある回数。
   * @default 10
   */
  iterations?: number;

  /**
   * 現在のタイムスタンプをミリ秒単位で取得する関数。
   */
  now?: () => number;

  /**
   * ベンチマークを中止するための AbortSignal。
   */
  signal?: AbortSignal;

  /**
   * ウォームアップ時間(ミリ秒)。
   * @default 100ms
   */
  warmupTime?: number;

  /**
   * ウォームアップ処理の繰り返し回数。
   * @default 5
   */
  warmupIterations?: number;

  /**
   * 各ベンチマークタスク(サイクル)の前に実行するセットアップ関数。
   */
  setup?: Hook;

  /**
   * 各ベンチマークタスク(サイクル)の後に実行するティアダウン関数。
   */
  teardown?: Hook;
}

bench.skip ​

  • 型: (name: string | Function, fn: BenchFunction, options?: BenchOptions) => void

bench.skip 構文を使用して、特定のベンチマークの実行をスキップできます。

ts
import { bench } from 'vitest';

bench.skip('normal sorting', () => {
  const x = [1, 5, 4, 2, 3];
  x.sort((a, b) => {
    return a - b;
  });
});

bench.only ​

  • 型: (name: string | Function, fn: BenchFunction, options?: BenchOptions) => void

bench.only を使用して、特定のスイートで特定のベンチマークのみを実行します。これはデバッグに役立ちます。

ts
import { bench } from 'vitest';

bench.only('normal sorting', () => {
  const x = [1, 5, 4, 2, 3];
  x.sort((a, b) => {
    return a - b;
  });
});

bench.todo ​

  • 型: (name: string | Function) => void

bench.todo を使用して、後で実装されるベンチマークのスタブを作成します。

ts
import { bench } from 'vitest';

bench.todo('unimplemented test');

describe ​

ファイル先頭で test または bench を使用すると、それらは暗黙的なスイートの一部として扱われます。describe を使用すると、現在のコンテキストにおいて、関連するテスト、ベンチマーク、ネストされたスイートをまとめて新しいスイートとして定義できます。スイートを使用することで、テストとベンチマークを整理し、レポートをより分かりやすくすることができます。

ts
// basic.spec.ts
// テストの整理

import { describe, expect, test } from 'vitest';

const person = {
  isActive: true,
  age: 32,
};

describe('person', () => {
  test('person is defined', () => {
    expect(person).toBeDefined();
  });

  test('is active', () => {
    expect(person.isActive).toBeTruthy();
  });

  test('age limit', () => {
    expect(person.age).toBeLessThanOrEqual(32);
  });
});
ts
// basic.bench.ts
// ベンチマークの整理

import { bench, describe } from 'vitest';

describe('sort', () => {
  bench('normal', () => {
    const x = [1, 5, 4, 2, 3];
    x.sort((a, b) => {
      return a - b;
    });
  });

  bench('reverse', () => {
    const x = [1, 5, 4, 2, 3];
    x.reverse().sort((a, b) => {
      return a - b;
    });
  });
});

テストやベンチマークに階層構造がある場合は、describe ブロックをネストできます。

ts
import { describe, expect, test } from 'vitest';

function numberToCurrency(value) {
  if (typeof value !== 'number') throw new Error('Value must be a number');

  return value
    .toFixed(2)
    .toString()
    .replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

describe('numberToCurrency', () => {
  describe('given an invalid number', () => {
    test('composed of non-numbers to throw error', () => {
      expect(() => numberToCurrency('abc')).toThrowError();
    });
  });

  describe('given a valid number', () => {
    test('returns the correct currency format', () => {
      expect(numberToCurrency(10000)).toBe('10,000.00');
    });
  });
});

describe.skip ​

  • 型: (name: string | Function, fn: TestFunction, options?: number | TestOptions) => void

    特定の describe ブロックの実行をスキップするには、スイート内で describe.skip を使用します。

    ts
    import { assert, describe, test } from 'vitest';
    
    describe.skip('skipped suite', () => {
      test('sqrt', () => {
        // スイートはスキップされるため、エラーは発生しません
        assert.equal(Math.sqrt(4), 3);
      });
    });

describe.skipIf ​

  • 型: (condition: any) => void

    同じスイートを異なる環境で複数回実行する場合、一部のスイートが特定の環境に依存することがあります。スイートを if 文で囲む代わりに、条件が truthy の場合にスイートをスキップするには、describe.skipIf を使用します。

    ts
    import { describe, test } from 'vitest';
    
    const isDev = process.env.NODE_ENV === 'development';
    
    describe.skipIf(isDev)('prod only test', () => {
      // このテストは本番環境でのみ実行されます
    });

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

describe.only ​

  • 型: (name: string | Function, fn: TestFunction, options?: number | TestOptions) => void

    特定のスイートのみを実行するには、describe.only を使用します。

    ts
    // このスイート(および only でマークされた他のスイート)のみが実行されます
    describe.only('suite', () => {
      test('sqrt', () => {
        assert.equal(Math.sqrt(4), 3);
      });
    });
    
    describe('other suite', () => {
      // ... はスキップされます
    });

    テストスイート全体から他のテストを無視して、特定のファイル内の only テストのみを実行すると、出力が整理されて非常に便利です。

    これを行うには、対象のテストを含む特定のファイルに対して vitest を実行します。

    # vitest interesting.test.ts

describe.concurrent ​

  • 型: (name: string | Function, fn: TestFunction, options?: number | TestOptions) => void

    describe.concurrent をスイート内で使用すると、すべてのテストが並行実行としてマークされます。

    ts
    // このスイート内のすべてのテストは並行して実行されます
    describe.concurrent('suite', () => {
      test('concurrent test 1', async () => {
        /* ... */
      });
      test('concurrent test 2', async () => {
        /* ... */
      });
      test.concurrent('concurrent test 3', async () => {
        /* ... */
      });
    });

    .skip、.only、および .todo は、並行スイートでも動作します。次のすべての組み合わせが有効です。

    ts
    describe.concurrent(/* ... */);
    describe.skip.concurrent(/* ... */); // または describe.concurrent.skip(/* ... */)
    describe.only.concurrent(/* ... */); // または describe.concurrent.only(/* ... */)
    describe.todo.concurrent(/* ... */); // または describe.concurrent.todo(/* ... */)

並行テストを実行する場合、スナップショットとアサーションは、正しいテストが検出されるように、ローカルの テストコンテキスト から expect を使用する必要があります。

ts
describe.concurrent('suite', () => {
  test('concurrent test 1', async ({ expect }) => {
    expect(foo).toMatchSnapshot();
  });
  test('concurrent test 2', async ({ expect }) => {
    expect(foo).toMatchSnapshot();
  });
});

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

describe.sequential ​

  • 型: (name: string | Function, fn: TestFunction, options?: number | TestOptions) => void

    describe.sequential をスイート内で使用すると、すべてのテストが逐次実行としてマークされます。これは、describe.concurrent 内、または --sequence.concurrent コマンドオプションを使用する際に、特定のテストを逐次実行したい場合に役立ちます。

    ts
    describe.concurrent('suite', () => {
      test('concurrent test 1', async () => {
        /* ... */
      });
      test('concurrent test 2', async () => {
        /* ... */
      });
    
      describe.sequential('', () => {
        test('sequential test 1', async () => {
          /* ... */
        });
        test('sequential test 2', async () => {
          /* ... */
        });
      });
    });

describe.shuffle ​

  • 型: (name: string | Function, fn: TestFunction, options?: number | TestOptions) => void

    Vitest では、CLI フラグ --sequence.shuffle または構成オプション sequence.shuffle を使用して、すべてのテストをランダムな順序で実行できます。テストスイートの一部のみをランダムな順序で実行したい場合は、このフラグで対象のスイートをマークします。

    ts
    describe.shuffle('suite', () => {
      test('random test 1', async () => {
        /* ... */
      });
      test('random test 2', async () => {
        /* ... */
      });
      test('random test 3', async () => {
        /* ... */
      });
    });
    // 順序は、構成の sequence.seed オプションに依存します(デフォルトでは Date.now())

.skip、.only、および .todo は、ランダムスイートでも動作します。

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

describe.todo ​

  • 型: (name: string | Function) => void

    describe.todo は、後で実装するスイートのプレースホルダーを作成する際に使用します。レポートには、実装が必要なテストの数が表示されます。

    ts
    // このスイートのエントリがレポートに表示されます
    describe.todo('unimplemented suite');

describe.each ​

  • 型: (cases: ReadonlyArray<T>, ...args: any[]): (name: string | Function, fn: (...args: T[]) => void, options?: number | TestOptions) => void

    同じデータに依存する複数のテストがある場合は、describe.each を使用します。

    ts
    describe.each([
      { a: 1, b: 1, expected: 2 },
      { a: 1, b: 2, expected: 3 },
      { a: 2, b: 1, expected: 3 },
    ])('describe object add($a, $b)', ({ a, b, expected }) => {
      test(`returns ${expected}`, () => {
        expect(a + b).toBe(expected);
      });
    
      test(`returned value not be greater than ${expected}`, () => {
        expect(a + b).not.toBeGreaterThan(expected);
      });
    
      test(`returned value not be less than ${expected}`, () => {
        expect(a + b).not.toBeLessThan(expected);
      });
    });

    Vitest 0.25.3 以降では、テンプレート文字列テーブルも使用できます。

    • 最初の行は、| で区切られた列名である必要があります。
    • 1 つ以上の後続のデータ行は、${value} 構文を使用してテンプレートリテラル式として記述します。
    ts
    describe.each`
      a             | b      | expected
      ${1}          | ${1}   | ${2}
      ${'a'}        | ${'b'} | ${'ab'}
      ${[]}         | ${'b'} | ${'b'}
      ${{}}         | ${'b'} | ${'[object Object]b'}
      ${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
    `('describe template string add($a, $b)', ({ a, b, expected }) => {
      test(`returns ${expected}`, () => {
        expect(a + b).toBe(expected);
      });
    });

WARNING

Vitest を 型チェッカーとして使用している場合、この構文は使用できません。

セットアップとティアダウン ​

これらの関数を使用すると、テストのライフサイクルにフックし、セットアップ/ティアダウンコードの繰り返しを避けることができます。これらの関数は現在のコンテキスト(ファイル先頭で使用されている場合はファイル全体、describe ブロック内にある場合はそのスイート)に適用されます。Vitest を型チェッカーとして実行している場合、これらのフックは実行されません。

beforeEach ​

  • 型: beforeEach(fn: () => Awaitable<void>, timeout?: number)

    現在のコンテキストで実行される各テストの前に呼び出されるコールバック関数を登録します。関数が Promise を返す場合、Vitest はテスト実行前に Promise が解決されるのを待ちます。

    タイムアウト(ミリ秒)を渡すことで、タイムアウトまでの待機時間を設定できます。デフォルトは 5 秒です。

    ts
    import { beforeEach } from 'vitest';
    
    beforeEach(async () => {
      // 各テストの実行前にモックをクリアし、テストデータを追加します
      await stopMocking();
      await addUser({ name: 'John' });
    });

    この例では、beforeEach によって各テストの実行前にユーザーが追加されることが保証されます。

    Vitest v0.10.0 以降、beforeEach はオプションのクリーンアップ関数(afterEach と同等)も受け入れます。

    ts
    import { beforeEach } from 'vitest';
    
    beforeEach(async () => {
      // 各テストの実行前に一度呼び出されます
      await prepareSomething();
    
      // クリーンアップ関数。各テストの実行後に呼び出されます
      return async () => {
        await resetSomething();
      };
    });

afterEach ​

  • 型: afterEach(fn: () => Awaitable<void>, timeout?: number)

    現在のコンテキストで実行される各テストの完了後に呼び出されるコールバック関数を登録します。関数が Promise を返す場合、Vitest は処理を続行する前に Promise が解決されるのを待ちます。

    オプションで、タイムアウトするまでの待機時間を指定するためのタイムアウト(ミリ秒単位)を提供できます。デフォルトは 5 秒です。

    ts
    import { afterEach } from 'vitest';
    
    afterEach(async () => {
      await clearTestingData(); // 各テストの実行後にテストデータをクリアします
    });

    この例では、afterEach は、各テストの実行後にテストデータがクリアされるようにします。

beforeAll ​

  • 型: beforeAll(fn: () => Awaitable<void>, timeout?: number)

    現在のコンテキストで、すべてのテストの実行を開始する前に一度だけ呼び出されるコールバック関数を登録します。関数が Promise を返す場合、Vitest はテスト実行前に Promise が解決されるのを待ちます。

    オプションで、タイムアウトするまでの待機時間を指定するためのタイムアウト(ミリ秒単位)を提供できます。デフォルトは 5 秒です。

    ts
    import { beforeAll } from 'vitest';
    
    beforeAll(async () => {
      await startMocking(); // すべてのテストの実行前に一度呼び出されます
    });

    この例では、beforeAll は、テストの実行前にモックデータが設定されるようにします。

    Vitest v0.10.0 以降、beforeAll はオプションのクリーンアップ関数(afterAll と同等)も受け入れます。

    ts
    import { beforeAll } from 'vitest';
    
    beforeAll(async () => {
      // すべてのテストの実行前に一度呼び出されます
      await startMocking();
    
      // クリーンアップ関数。すべてのテストの実行後に一度呼び出されます
      return async () => {
        await stopMocking();
      };
    });

afterAll ​

  • 型: afterAll(fn: () => Awaitable<void>, timeout?: number)

    現在のコンテキストで、すべてのテストが実行された後に一度だけ呼び出されるコールバック関数を登録します。関数が Promise を返す場合、Vitest は処理を続行する前に Promise が解決されるのを待ちます。

    オプションで、タイムアウトするまでの待機時間を指定するためのタイムアウト(ミリ秒単位)を提供できます。デフォルトは 5 秒です。

    ts
    import { afterAll } from 'vitest';
    
    afterAll(async () => {
      await stopMocking(); // このメソッドは、すべてのテストの実行後に呼び出されます
    });

    この例では、afterAll によって、stopMocking メソッドがすべてのテストの実行後に呼び出されることが保証されます。

Pager
前のページよくあるエラー
次のページモック関数

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

Copyright (c) 2021-Present Vitest Team

https://v0.vitest.dev/api/

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

Copyright (c) 2021-Present Vitest Team