Annotazioni dei Test
Vitest supporta l'annotazione dei tuoi test con messaggi e file personalizzati tramite l'API context.annotate
. Queste annotazioni saranno allegate al caso di test e trasmesse ai reporter tramite l'hook 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
La funzione annotate
restituisce una Promise, quindi deve essere attesa se si desidera gestirne il completamento. Tuttavia, Vitest attenderà automaticamente qualsiasi annotazione non ancora risolta prima che il test termini.
La visualizzazione di queste annotazioni varia a seconda del reporter utilizzato.
Reporter Integrati
default
Il reporter default
stampa le annotazioni solo se il test è fallito:
⎯⎯⎯⎯⎯⎯⎯ 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
In un terminale TTY, il reporter verbose
funziona in modo simile al reporter default
. Tuttavia, in un ambiente non-TTY, il reporter verbose
stamperà le annotazioni anche dopo ogni test.
✓ 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
Il reporter HTML mostra le annotazioni allo stesso modo dell'interfaccia utente. L'annotazione è visibile sulla riga in cui è stata chiamata. Attualmente, se l'annotazione non è stata richiamata all'interno di un file di test, non sarà visibile nell'interfaccia utente. È previsto il supporto per una vista di riepilogo dei test separata in cui sarà visibile.


junit
Il reporter junit
elenca le annotazioni all'interno del tag properties
del caso di test. Il reporter JUnit ignorerà tutti gli allegati e visualizzerà solo il tipo e il messaggio.
<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
Il reporter github-actions
visualizzerà l'annotazione come messaggio di notifica per impostazione predefinita. Puoi configurare il type
specificando il secondo argomento come notice
, warning
o error
. Se il tipo non corrisponde a nessuno di questi, Vitest visualizzerà il messaggio come una notifica.


tap
I reporter tap
e tap-flat
visualizzano le annotazioni come messaggi diagnostici su una nuova riga, preceduti dal simbolo #
. Ignoreranno tutti gli allegati e visualizzeranno solo il tipo e il messaggio:
ok 1 - an example of a test with annotation # time=143.15ms
# notice: the message of the annotation