expectTypeOf
- Tipo:
<T>(a: unknown) => ExpectTypeOf
not
- Tipo:
ExpectTypeOf
Puoi negare qualsiasi asserzione usando la proprietà .not
.
toEqualTypeOf
- Tipo:
<T>(expected: T) => void
Questo matcher verifica se i tipi sono esattamente uguali. Non fallisce se due oggetti hanno valori diversi ma lo stesso tipo. Tuttavia, fallisce se un oggetto manca di una proprietà.
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
- Tipo:
<T>(expected: T) => void
Questo matcher verifica se il tipo previsto estende il tipo fornito. È diverso da toEqualTypeOf
e più simile a toMatchObject()
di expect. Con questo matcher, puoi controllare se un tipo "corrisponde" a un altro.
import { expectTypeOf } from 'vitest';
expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 });
expectTypeOf<number>().toMatchTypeOf<string | number>();
expectTypeOf<string | number>().not.toMatchTypeOf<number>();
extract
- Tipo:
ExpectTypeOf<ExtractedUnion>
Puoi usare .extract
per restringere i tipi di un'unione per ulteriori test.
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 }>() // seleziona l'ultimo tipo da un'unione
.toEqualTypeOf<{
xs?: CSSProperties;
sm?: CSSProperties;
md?: CSSProperties;
}>();
expectTypeOf(getResponsiveProp(cssProperties))
.extract<unknown[]>() // estrae un array da un'unione
.toEqualTypeOf<CSSProperties[]>();
WARNING
Se nessun tipo viene trovato nell'unione, .extract
restituirà never
.
exclude
- Tipo:
ExpectTypeOf<NonExcludedUnion>
Puoi usare .exclude
per rimuovere tipi da un'unione per ulteriori test.
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 }>() // or just .exclude<unknown[] | { xs?: unknown }>()
.toEqualTypeOf<CSSProperties>();
WARNING
Se nessun tipo viene trovato nell'unione, .exclude
restituirà never
.
returns
- Tipo:
ExpectTypeOf<ReturnValue>
Puoi usare .returns
per estrarre il tipo di valore restituito di una funzione.
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]);
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
parameters
- Tipo:
ExpectTypeOf<Parameters>
Puoi estrarre i tipi dei parametri di una funzione con .parameters
per eseguire asserzioni sui loro valori. I parametri sono restituiti come un array.
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().parameters.toEqualTypeOf<[]>();
expectTypeOf<HasParam>().parameters.toEqualTypeOf<[string]>();
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
TIP
Puoi anche usare il matcher .toBeCallableWith
come asserzione più espressiva.
parameter
- Tipo:
(nth: number) => ExpectTypeOf
Puoi estrarre il tipo di un determinato argomento di funzione con la chiamata .parameter(number)
per eseguire ulteriori asserzioni su di esso.
import { expectTypeOf } from 'vitest';
function foo(a: number, b: string) {
return [a, b];
}
expectTypeOf(foo).parameter(0).toBeNumber();
expectTypeOf(foo).parameter(1).toBeString();
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
constructorParameters
- Tipo:
ExpectTypeOf<ConstructorParameters>
Puoi estrarre i tipi dei parametri del costruttore come un array e fare asserzioni su di essi tramite questo metodo.
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).constructorParameters.toEqualTypeOf<
[] | [string | number | Date]
>();
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
TIP
Puoi anche usare il matcher .toBeConstructibleWith
come asserzione più espressiva.
instance
- Tipo:
ExpectTypeOf<ConstructableInstance>
Questa proprietà fornisce accesso ai matcher che possono essere utilizzati su un'istanza della classe fornita.
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).instance.toHaveProperty('toISOString');
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
items
- Tipo:
ExpectTypeOf<T>
Puoi ottenere il tipo degli elementi di un array con .items
per eseguire ulteriori asserzioni.
import { expectTypeOf } from 'vitest';
expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>();
expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>();
resolves
- Tipo:
ExpectTypeOf<ResolvedPromise>
Questo matcher estrae il tipo del valore risolto di una Promise
, quindi puoi eseguire altre asserzioni su di esso.
import { expectTypeOf } from 'vitest';
async function asyncFunc() {
return 123;
}
expectTypeOf(asyncFunc).returns.resolves.toBeNumber();
expectTypeOf(Promise.resolve('string')).resolves.toBeString();
WARNING
Se usato su un tipo che non è una Promise, restituirà never
, quindi non potrai concatenarlo con altri matcher.
guards
- Tipo:
ExpectTypeOf<Guard>
Questo matcher estrae il tipo di guardia (ad esempio, v is number
), consentendoti di eseguire asserzioni su di esso.
import { expectTypeOf } from 'vitest';
function isString(v: any): v is string {
return typeof v === 'string';
}
expectTypeOf(isString).guards.toBeString();
WARNING
Restituisce never
se il valore non è una funzione di guardia, quindi non potrai concatenarlo con altri matcher.
asserts
- Tipo:
ExpectTypeOf<Assert>
Questo matcher estrae il tipo di asserzione (ad esempio, assert v is number
), consentendoti di eseguire asserzioni su di esso.
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
Restituisce never
se il valore non è una funzione assert, quindi non potrai concatenarlo con altri matcher.
toBeAny
- Tipo:
() => void
Con questo matcher puoi controllare se il tipo fornito è un tipo any
. Se il tipo è più specifico, il test fallirà.
import { expectTypeOf } from 'vitest';
expectTypeOf<any>().toBeAny();
expectTypeOf({} as any).toBeAny();
expectTypeOf('string').not.toBeAny();
toBeUnknown
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è un tipo unknown
.
import { expectTypeOf } from 'vitest';
expectTypeOf().toBeUnknown();
expectTypeOf({} as unknown).toBeUnknown();
expectTypeOf('string').not.toBeUnknown();
toBeNever
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è un tipo never
.
import { expectTypeOf } from 'vitest';
expectTypeOf<never>().toBeNever();
expectTypeOf((): never => {}).returns.toBeNever();
toBeFunction
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è una function
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeFunction();
expectTypeOf((): never => {}).toBeFunction();
toBeObject
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è un object
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeObject();
expectTypeOf({}).toBeObject();
toBeArray
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è Array<T>
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeArray();
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2]).toBeArray();
expectTypeOf([{}, 42]).toBeArray();
toBeString
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è una string
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeString();
expectTypeOf('').toBeString();
expectTypeOf('a').toBeString();
toBeBoolean
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è boolean
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeBoolean();
expectTypeOf(true).toBeBoolean();
expectTypeOf<boolean>().toBeBoolean();
toBeVoid
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è void
.
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf<void>().toBeVoid();
toBeSymbol
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è un symbol
.
import { expectTypeOf } from 'vitest';
expectTypeOf(Symbol(1)).toBeSymbol();
expectTypeOf<symbol>().toBeSymbol();
toBeNull
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è null
.
import { expectTypeOf } from 'vitest';
expectTypeOf(null).toBeNull();
expectTypeOf<null>().toBeNull();
expectTypeOf(undefined).not.toBeNull();
toBeUndefined
- Tipo:
() => void
Questo matcher controlla se il tipo fornito è undefined
.
import { expectTypeOf } from 'vitest';
expectTypeOf(undefined).toBeUndefined();
expectTypeOf<undefined>().toBeUndefined();
expectTypeOf(null).not.toBeUndefined();
toBeNullable
- Tipo:
() => void
Questo matcher controlla se il tipo fornito ammette null
o undefined
.
import { expectTypeOf } from 'vitest';
expectTypeOf<1 | undefined>().toBeNullable();
expectTypeOf<1 | null>().toBeNullable();
expectTypeOf<1 | undefined | null>().toBeNullable();
toBeCallableWith
- Tipo:
() => void
Questo matcher garantisce che la funzione fornita possa essere chiamata con un determinato insieme di parametri.
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().toBeCallableWith();
expectTypeOf<HasParam>().toBeCallableWith('some string');
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
toBeConstructibleWith
- Tipo:
() => void
Questo matcher garantisce che si possa creare una nuova istanza della classe fornita con un determinato insieme di parametri del costruttore.
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).toBeConstructibleWith(new Date());
expectTypeOf(Date).toBeConstructibleWith('01-01-2000');
WARNING
Se usato su un tipo che non è una funzione, restituirà never
, quindi non potrai concatenarlo con altri matcher.
toHaveProperty
- Tipo:
<K extends keyof T>(property: K) => ExpectTypeOf<T[K>
Questo matcher verifica se una proprietà esiste nell'oggetto fornito. Se esiste, restituisce anche lo stesso insieme di matcher per il tipo di questa proprietà, consentendoti di concatenare le asserzioni una dopo l'altra.
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();