テストアノテーション
Vitest は、context.annotate
API を介して、カスタムメッセージとファイルでテストにアノテーションを付加することをサポートしています。これらのアノテーションはテストケースに紐付けられ、onTestAnnotate
フックでレポーターに渡されます。
test('hello world', async ({ annotate }) => {
await annotate('this is my test');
if (condition) {
await annotate("this should've errored", 'error');
}
const file = createTestSpecificFile();
await annotate('creates a file', { body: file });
});
WARNING
annotate
関数は Promise を返すため、その完了を待つ必要がある場合は await
する必要があります。ただし、Vitest はテストが終了する前に、await
されていないアノテーションも自動的に await
します。
レポーターによって、これらのアノテーションの表示方法は異なります。
組み込みレポーター
default
default
レポーターは、テストが失敗した場合にのみアノテーションを表示します。
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
FAIL example.test.js > an example of a test with annotation
Error: thrown error
❯ example.test.js:11:21
9 | await annotate('annotation 1')
10| await annotate('annotation 2', 'warning')
11| throw new Error('thrown error')
| ^
12| })
❯ example.test.js:9:15 notice
↳ annotation 1
❯ example.test.js:10:15 warning
↳ annotation 2
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
verbose
TTY 端末では、verbose
レポーターは default
レポーターと同様に動作します。ただし、非TTY環境では、verbose
レポーターはすべてのテストの後にアノテーションも表示します。
✓ example.test.js > an example of a test with annotation
❯ example.test.js:9:15 notice
↳ annotation 1
❯ example.test.js:10:15 warning
↳ annotation 2
html
HTML レポーターは、UI と同様の方法でアノテーションを表示します。アノテーションは呼び出し元の行で確認できます。現時点では、アノテーションがテストファイルで呼び出されていない場合、UI 上で表示されません。別途テスト概要ビューをサポート予定で、そこで表示可能になる予定です。


junit
junit
レポーターは、テストケースの properties
タグ内にアノテーションを含めます。JUnit レポーターはすべての添付ファイルを無視し、タイプとメッセージのみを表示します。
<testcase classname="basic/example.test.js" name="an example of a test with annotation" time="0.14315">
<properties>
<property name="notice" value="the message of the annotation">
</property>
</properties>
</testcase>
github-actions
github-actions
レポーターは、デフォルトではアノテーションを notice message として表示します。2 番目の引数として notice
、warning
、または error
を渡すことで type
を設定できます。タイプがこれらのいずれでもない場合、Vitest はメッセージを通知として表示します。


tap
tap
および tap-flat
レポーターは、アノテーションを #
記号で始まる新しい行の診断メッセージとして表示します。これらはすべての添付ファイルを無視し、タイプとメッセージのみを表示します。
ok 1 - an example of a test with annotation # time=143.15ms
# notice: the message of the annotation