assert
Vitest riesporta il metodo assert
da chai
per verificare gli invarianti.
assert
- Tipo:
(expression: any, message?: string) => asserts expression
Afferma che la data expression
è "truthy"; in caso contrario, l'asserzione fallisce.
import { assert, test } from 'vitest';
test('assert', () => {
assert('foo' !== 'bar', 'foo non dovrebbe essere uguale a bar');
});
fail
- Tipo:
(message?: string) => never
<T>(actual: T, expected: T, message?: string, operator?: string) => never
Forza il fallimento di un'asserzione.
import { assert, test } from 'vitest';
test('assert.fail', () => {
assert.fail('messaggio di errore in caso di fallimento');
assert.fail('foo', 'bar', 'foo non è bar', '===');
});
isOk
- Tipo:
<T>(value: T, message?: string) => void
- Alias
ok
Afferma che il dato value
è "truthy".
import { assert, test } from 'vitest';
test('assert.isOk', () => {
assert.isOk('foo', 'ogni valore truthy è ok');
assert.isOk(false, 'questo fallirà poiché false non è truthy');
});
isNotOk
- Tipo:
<T>(value: T, message?: string) => void
- Alias
notOk
Afferma che il dato value
è "falsy".
import { assert, test } from 'vitest';
test('assert.isNotOk', () => {
assert.isNotOk('foo', 'questo fallirà: ogni valore truthy non è ok');
assert.isNotOk(false, 'questo passerà perché false è falsy');
});
equal
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Afferma l'uguaglianza non stretta (==
) di actual
e expected
.
import { assert, test } from 'vitest';
test('assert.equal', () => {
assert.equal(Math.sqrt(4), '2');
});
notEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Afferma la disuguaglianza non stretta (!=
) di actual
e expected
.
import { assert, test } from 'vitest';
test('assert.notEqual', () => {
assert.notEqual(Math.sqrt(4), 3);
});
strictEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Afferma l'uguaglianza stretta (===
) di actual
e expected
.
import { assert, test } from 'vitest';
test('assert.strictEqual', () => {
assert.strictEqual(Math.sqrt(4), 2);
});
deepEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Afferma che actual
è profondamente uguale a expected
.
import { assert, test } from 'vitest';
test('assert.deepEqual', () => {
assert.deepEqual({ color: 'green' }, { color: 'green' });
});
notDeepEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Afferma che actual
non è profondamente uguale a expected
.
import { assert, test } from 'vitest';
test('assert.notDeepEqual', () => {
assert.notDeepEqual({ color: 'green' }, { color: 'red' });
});
isAbove
- Tipo:
(valueToCheck: number, valueToBeAbove: number, message?: string) => void
Afferma che valueToCheck
è strettamente maggiore di (>
) valueToBeAbove
.
import { assert, test } from 'vitest';
test('assert.isAbove', () => {
assert.isAbove(5, 2, '5 è strettamente maggiore di 2');
});
isAtLeast
- Tipo:
(valueToCheck: number, valueToBeAtLeast: number, message?: string) => void
Afferma che valueToCheck
è maggiore o uguale a (>=
) valueToBeAtLeast
.
import { assert, test } from 'vitest';
test('assert.isAtLeast', () => {
assert.isAtLeast(5, 2, '5 è maggiore o uguale a 2');
assert.isAtLeast(3, 3, '3 è maggiore o uguale a 3');
});
isBelow
- Tipo:
(valueToCheck: number, valueToBeBelow: number, message?: string) => void
Afferma che valueToCheck
è strettamente minore di (<
) valueToBeBelow
.
import { assert, test } from 'vitest';
test('assert.isBelow', () => {
assert.isBelow(3, 6, '3 è strettamente minore di 6');
});
isAtMost
- Tipo:
(valueToCheck: number, valueToBeAtMost: number, message?: string) => void
Afferma che valueToCheck
è minore o uguale a (<=
) valueToBeAtMost
.
import { assert, test } from 'vitest';
test('assert.isAtMost', () => {
assert.isAtMost(3, 6, '3 è minore o uguale a 6');
assert.isAtMost(4, 4, '4 è minore o uguale a 4');
});
isTrue
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è true
.
import { assert, test } from 'vitest';
const testPassed = true;
test('assert.isTrue', () => {
assert.isTrue(testPassed);
});
isNotTrue
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è true
.
import { assert, test } from 'vitest';
const testPassed = 'ok';
test('assert.isNotTrue', () => {
assert.isNotTrue(testPassed);
});
isFalse
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è false
.
import { assert, test } from 'vitest';
const testPassed = false;
test('assert.isFalse', () => {
assert.isFalse(testPassed);
});
isNotFalse
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è false
.
import { assert, test } from 'vitest';
const testPassed = 'no';
test('assert.isNotFalse', () => {
assert.isNotFalse(testPassed);
});
isNull
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è null
.
import { assert, test } from 'vitest';
const error = null;
test('assert.isNull', () => {
assert.isNull(error, 'l\'errore è null');
});
isNotNull
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è null
.
import { assert, test } from 'vitest';
const error = { message: 'si è verificato un errore' };
test('assert.isNotNull', () => {
assert.isNotNull(error, 'l\'errore non è null ma un oggetto');
});
isNaN
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è NaN
.
import { assert, test } from 'vitest';
const calculation = 1 * 'vitest';
test('assert.isNaN', () => {
assert.isNaN(calculation, '1 * "vitest" è NaN');
});
isNotNaN
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è NaN
.
import { assert, test } from 'vitest';
const calculation = 1 * 2;
test('assert.isNotNaN', () => {
assert.isNotNaN(calculation, '1 * 2 non è NaN ma 2');
});
exists
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è né null
né undefined
.
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.exists', () => {
assert.exists(name, 'foo non è né null né undefined');
});
notExists
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è null
o undefined
.
import { assert, test } from 'vitest';
const foo = null;
const bar = undefined;
test('assert.notExists', () => {
assert.notExists(foo, 'foo è null quindi non esiste');
assert.notExists(bar, 'bar è undefined quindi non esiste');
});
isUndefined
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è undefined
.
import { assert, test } from 'vitest';
const name = undefined;
test('assert.isUndefined', () => {
assert.isUndefined(name, 'name è undefined');
});
isDefined
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è undefined
.
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.isDefined', () => {
assert.isDefined(name, 'name non è undefined');
});
isFunction
- Tipo:
<T>(value: T, message?: string) => void
- Alias:
isCallable
Afferma chevalue
è una funzione.
import { assert, test } from 'vitest';
function name() {
return 'foo';
}
test('assert.isFunction', () => {
assert.isFunction(name, 'name è una funzione');
});
isNotFunction
- Tipo:
<T>(value: T, message?: string) => void
- Alias:
isNotCallable
Afferma che value
non è una funzione.
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.isNotFunction', () => {
assert.isNotFunction(name, 'name non è una funzione ma una stringa');
});
isObject
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è un oggetto di tipo Object
(come rivelato da Object.prototype.toString
). L'asserzione non corrisponde a oggetti sottoclassati.
import { assert, test } from 'vitest';
const someThing = { color: 'red', shape: 'circle' };
test('assert.isObject', () => {
assert.isObject(someThing, 'someThing è un oggetto');
});
isNotObject
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è un oggetto di tipo Object
(come rivelato da Object.prototype.toString
). L'asserzione non corrisponde a oggetti sottoclassati.
import { assert, test } from 'vitest';
const someThing = 'redCircle';
test('assert.isNotObject', () => {
assert.isNotObject(someThing, 'someThing non è un oggetto ma una stringa');
});
isArray
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è un array.
import { assert, test } from 'vitest';
const color = ['red', 'green', 'yellow'];
test('assert.isArray', () => {
assert.isArray(color, 'color è un array');
});
isNotArray
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è un array.
import { assert, test } from 'vitest';
const color = 'red';
test('assert.isNotArray', () => {
assert.isNotArray(color, 'color non è un array ma una stringa');
});
isString
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è una stringa.
import { assert, test } from 'vitest';
const color = 'red';
test('assert.isString', () => {
assert.isString(color, 'color è una stringa');
});
isNotString
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è una stringa.
import { assert, test } from 'vitest';
const color = ['red', 'green', 'yellow'];
test('assert.isNotString', () => {
assert.isNotString(color, 'color non è una stringa ma un array');
});
isNumber
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è un numero.
import { assert, test } from 'vitest';
const colors = 3;
test('assert.isNumber', () => {
assert.isNumber(colors, 'colors è un numero');
});
isNotNumber
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è un numero.
import { assert, test } from 'vitest';
const colors = '3 colors';
test('assert.isNotNumber', () => {
assert.isNotNumber(colors, 'colors non è un numero ma una stringa');
});
isFinite
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è un numero finito (non NaN
, Infinity
).
import { assert, test } from 'vitest';
const colors = 3;
test('assert.isFinite', () => {
assert.isFinite(colors, 'colors è un numero non NaN o Infinity');
});
isBoolean
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
è un booleano.
import { assert, test } from 'vitest';
const isReady = true;
test('assert.isBoolean', () => {
assert.isBoolean(isReady, 'isReady è un booleano');
});
isNotBoolean
- Tipo:
<T>(value: T, message?: string) => void
Afferma che value
non è un booleano.
import { assert, test } from 'vitest';
const isReady = 'sure';
test('assert.isNotBoolean', () => {
assert.isNotBoolean(isReady, 'isReady non è un booleano ma una stringa');
});
typeOf
- Tipo:
<T>(value: T, name: string, message?: string) => void
Afferma che il tipo di value
è name
, come determinato da Object.prototype.toString
.
import { assert, test } from 'vitest';
test('assert.typeOf', () => {
assert.typeOf({ color: 'red' }, 'object', 'abbiamo un oggetto');
assert.typeOf(['red', 'green'], 'array', 'abbiamo un array');
assert.typeOf('red', 'string', 'abbiamo una stringa');
assert.typeOf(/red/, 'regexp', 'abbiamo un\'espressione regolare');
assert.typeOf(null, 'null', 'abbiamo un null');
assert.typeOf(undefined, 'undefined', 'abbiamo un undefined');
});
notTypeOf
- Tipo:
<T>(value: T, name: string, message?: string) => void
Afferma che il tipo di value
non è name
, come determinato da Object.prototype.toString
.
import { assert, test } from 'vitest';
test('assert.notTypeOf', () => {
assert.notTypeOf('red', 'number', '"red" non è un numero');
});
instanceOf
- Tipo:
<T>(value: T, constructor: Function, message?: string) => void
Afferma che value
è un'istanza di constructor
.
import { assert, test } from 'vitest';
function Person(name) {
this.name = name;
}
const foo = new Person('foo');
class Tea {
constructor(name) {
this.name = name;
}
}
const coffee = new Tea('coffee');
test('assert.instanceOf', () => {
assert.instanceOf(foo, Person, 'foo è un\'istanza di Person');
assert.instanceOf(coffee, Tea, 'coffee è un\'istanza di Tea');
});
notInstanceOf
- Tipo:
<T>(value: T, constructor: Function, message?: string) => void
Afferma che value
non è un'istanza di constructor
.
import { assert, test } from 'vitest';
function Person(name) {
this.name = name;
}
const foo = new Person('foo');
class Tea {
constructor(name) {
this.name = name;
}
}
const coffee = new Tea('coffee');
test('assert.notInstanceOf', () => {
assert.notInstanceOf(foo, Tea, 'foo non è un\'istanza di Tea');
});
include
- Tipo:
(haystack: string, needle: string, message?: string) => void
<T>(haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>, needle: T, message?: string) => void
<T extends object>(haystack: WeakSet<T>, needle: T, message?: string) => void
<T>(haystack: T, needle: Partial<T>, message?: string) => void
Afferma che haystack
include needle
. Può essere usato per affermare l'inclusione di un valore in un array, una sottostringa in una stringa o un sottoinsieme di proprietà in un oggetto.
import { assert, test } from 'vitest';
test('assert.include', () => {
assert.include([1, 2, 3], 2, 'l\'array contiene il valore');
assert.include('foobar', 'foo', 'la stringa contiene la sottostringa');
assert.include(
{ foo: 'bar', hello: 'universe' },
{ foo: 'bar' },
'l\'oggetto contiene la proprietà'
);
});
notInclude
- Tipo:
(haystack: string, needle: string, message?: string) => void
<T>(haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>, needle: T, message?: string) => void
<T extends object>(haystack: WeakSet<T>, needle: T, message?: string) => void
<T>(haystack: T, needle: Partial<T>, message?: string) => void
Afferma che haystack
non include needle
. Può essere usato per affermare l'assenza di un valore in un array, una sottostringa in una stringa o un sottoinsieme di proprietà in un oggetto.
import { assert, test } from 'vitest';
test('assert.notInclude', () => {
assert.notInclude([1, 2, 3], 4, 'l\'array non contiene 4');
assert.notInclude('foobar', 'baz', 'foobar non contiene baz');
assert.notInclude(
{ foo: 'bar', hello: 'universe' },
{ foo: 'baz' },
'l\'oggetto non contiene la proprietà'
);
});
deepInclude
- Tipo:
(haystack: string, needle: string, message?: string) => void
<T>(haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>, needle: T, message?: string) => void
<T>(haystack: T, needle: T extends WeakSet<any> ? never : Partial<T>, message?: string) => void
Afferma che haystack
include needle
. Può essere usato per affermare l'inclusione di un valore in un array o un sottoinsieme di proprietà in un oggetto. Viene usata l'uguaglianza profonda.
import { assert, test } from 'vitest';
const obj1 = { a: 1 };
const obj2 = { b: 2 };
test('assert.deepInclude', () => {
assert.deepInclude([obj1, obj2], { a: 1 });
assert.deepInclude({ foo: obj1, bar: obj2 }, { foo: { a: 1 } });
});
notDeepInclude
- Tipo:
(haystack: string, needle: string, message?: string) => void
<T>(haystack: readonly T[] | ReadonlySet<T> | ReadonlyMap<any, T>, needle: T, message?: string) => void
<T>(haystack: T, needle: T extends WeakSet<any> ? never : Partial<T>, message?: string) => void
Afferma che haystack
non include needle
. Può essere usato per affermare l'assenza di un valore in un array o un sottoinsieme di proprietà in un oggetto. Viene usata l'uguaglianza profonda.
import { assert, test } from 'vitest';
const obj1 = { a: 1 };
const obj2 = { b: 2 };
test('assert.notDeepInclude', () => {
assert.notDeepInclude([obj1, obj2], { a: 10 });
assert.notDeepInclude({ foo: obj1, bar: obj2 }, { foo: { a: 10 } });
});
nestedInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
include needle
. Può essere usato per affermare l'inclusione di un sottoinsieme di proprietà in un oggetto. Abilita l'uso della notazione a punti e a parentesi quadre per fare riferimento a proprietà annidate. I caratteri '[]' e '.' nei nomi delle proprietà possono essere preceduti da un carattere di escape usando doppie barre rovesciate.
import { assert, test } from 'vitest';
test('assert.nestedInclude', () => {
assert.nestedInclude({ '.a': { b: 'x' } }, { '\\.a.[b]': 'x' });
assert.nestedInclude({ a: { '[b]': 'x' } }, { 'a.\\[b\\]': 'x' });
});
notNestedInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
non include needle
. Può essere usato per affermare l'assenza di un sottoinsieme di proprietà in un oggetto. Abilita l'uso della notazione a punti e a parentesi quadre per fare riferimento a proprietà annidate. I caratteri '[]' e '.' nei nomi delle proprietà possono essere preceduti da un carattere di escape usando doppie barre rovesciate.
import { assert, test } from 'vitest';
test('assert.notNestedInclude', () => {
assert.notNestedInclude({ '.a': { b: 'x' } }, { '\\.a.b': 'y' });
assert.notNestedInclude({ a: { '[b]': 'x' } }, { 'a.\\[b\\]': 'y' });
});
deepNestedInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
include needle
. Può essere usato per affermare l'inclusione di un sottoinsieme di proprietà in un oggetto controllando l'uguaglianza profonda. Abilita l'uso della notazione a punti e a parentesi quadre per fare riferimento a proprietà annidate. I caratteri '[]' e '.' nei nomi delle proprietà possono essere preceduti da un carattere di escape usando doppie barre rovesciate.
import { assert, test } from 'vitest';
test('assert.deepNestedInclude', () => {
assert.deepNestedInclude({ a: { b: [{ x: 1 }] } }, { 'a.b[0]': { x: 1 } });
assert.deepNestedInclude(
{ '.a': { '[b]': { x: 1 } } },
{ '\\.a.\\[b\\]': { x: 1 } }
);
});
notDeepNestedInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
non include needle
. Può essere usato per affermare l'assenza di un sottoinsieme di proprietà in un oggetto controllando l'uguaglianza profonda. Abilita l'uso della notazione a punti e a parentesi quadre per fare riferimento a proprietà annidate. I caratteri '[]' e '.' nei nomi delle proprietà possono essere preceduti da un carattere di escape usando doppie barre rovesciate.
import { assert, test } from 'vitest';
test('assert.notDeepNestedInclude', () => {
assert.notDeepNestedInclude({ a: { b: [{ x: 1 }] } }, { 'a.b[0]': { y: 1 } });
assert.notDeepNestedInclude(
{ '.a': { '[b]': { x: 1 } } },
{ '\\.a.\\[b\\]': { y: 2 } }
);
});
ownInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
include needle
. Può essere usato per affermare l'inclusione di un sottoinsieme di proprietà in un oggetto ignorando le proprietà ereditate.
import { assert, test } from 'vitest';
test('assert.ownInclude', () => {
assert.ownInclude({ a: 1 }, { a: 1 });
});
notOwnInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
non include needle
. Può essere usato per affermare l'assenza di un sottoinsieme di proprietà in un oggetto ignorando le proprietà ereditate.
import { assert, test } from 'vitest';
const obj1 = {
b: 2,
};
const obj2 = Object.create(obj1);
obj2.a = 1;
test('assert.notOwnInclude', () => {
assert.notOwnInclude(obj2, { b: 2 });
});
deepOwnInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
include needle
. Può essere usato per affermare l'inclusione di un sottoinsieme di proprietà in un oggetto ignorando le proprietà ereditate e controllando l'uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.deepOwnInclude', () => {
assert.deepOwnInclude({ a: { b: 2 } }, { a: { b: 2 } });
});
notDeepOwnInclude
- Tipo:
(haystack: any, needle: any, message?: string) => void
Afferma che haystack
non include needle
. Può essere usato per affermare l'assenza di un sottoinsieme di proprietà in un oggetto ignorando le proprietà ereditate e controllando l'uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.notDeepOwnInclude', () => {
assert.notDeepOwnInclude({ a: { b: 2 } }, { a: { c: 3 } });
});
match
- Tipo:
(value: string, regexp: RegExp, message?: string) => void
Afferma che value
corrisponde all'espressione regolare regexp
.
import { assert, test } from 'vitest';
test('assert.match', () => {
assert.match('foobar', /^foo/, 'l\'espressione regolare corrisponde');
});
notMatch
- Tipo:
(value: string, regexp: RegExp, message?: string) => void
Afferma che value
non corrisponde all'espressione regolare regexp
.
import { assert, test } from 'vitest';
test('assert.notMatch', () => {
assert.notMatch('foobar', /^foo/, 'l\'espressione regolare non corrisponde');
});
property
- Tipo:
<T>(object: T, property: string, message?: string) => void
Afferma che object
ha una proprietà diretta o ereditata con il nome property
.
import { assert, test } from 'vitest';
test('assert.property', () => {
assert.property({ tea: { green: 'matcha' } }, 'tea');
assert.property({ tea: { green: 'matcha' } }, 'toString');
});
notProperty
- Tipo:
<T>(object: T, property: string, message?: string) => void
Afferma che object
non ha una proprietà diretta o ereditata con il nome property
.
import { assert, test } from 'vitest';
test('assert.notProperty', () => {
assert.notProperty({ tea: { green: 'matcha' } }, 'coffee');
});
propertyVal
- Tipo:
<T, V>(object: T, property: string, value: V, message?: string) => void
Afferma che object
ha una proprietà diretta o ereditata con il nome property
e un valore dato da value
. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.propertyVal', () => {
assert.propertyVal({ tea: 'is good' }, 'tea', 'is good');
});
notPropertyVal
- Tipo:
<T, V>(object: T, property: string, value: V, message?: string) => void
Afferma che object
non ha una proprietà diretta o ereditata con il nome property
e un valore dato da value
. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.notPropertyVal', () => {
assert.notPropertyVal({ tea: 'is good' }, 'tea', 'is bad');
assert.notPropertyVal({ tea: 'is good' }, 'coffee', 'is good');
});
deepPropertyVal
- Tipo:
<T, V>(object: T, property: string, value: V, message?: string) => void
Afferma che object
ha una proprietà diretta o ereditata con il nome property
e un valore dato da value
. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
assert.deepPropertyVal({ tea: { green: 'matcha' } }, 'tea', {
green: 'matcha',
});
});
notDeepPropertyVal
- Tipo:
<T, V>(object: T, property: string, value: V, message?: string) => void
Afferma che object
non ha una proprietà diretta o ereditata con il nome property
e un valore dato da value
. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.notDeepPropertyVal', () => {
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', {
black: 'matcha',
});
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', {
green: 'oolong',
});
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'coffee', {
green: 'matcha',
});
});
nestedProperty
- Tipo:
<T>(object: T, property: string, message?: string) => void
Afferma che object
ha una proprietà diretta o ereditata con il nome property
, che può essere una stringa che utilizza la notazione a punti e a parentesi quadre per il riferimento annidato.
import { assert, test } from 'vitest';
test('assert.nestedProperty', () => {
assert.nestedProperty({ tea: { green: 'matcha' } }, 'tea.green');
});
notNestedProperty
- Tipo:
<T>(object: T, property: string, message?: string) => void
Afferma che object
non ha una proprietà diretta o ereditata con il nome property
, che può essere una stringa che utilizza la notazione a punti e a parentesi quadre per il riferimento annidato.
import { assert, test } from 'vitest';
test('assert.notNestedProperty', () => {
assert.notNestedProperty({ tea: { green: 'matcha' } }, 'tea.oolong');
});
nestedPropertyVal
- Tipo:
<T>(object: T, property: string, value: any, message?: string) => void
Afferma che object
ha una proprietà con il nome property
e un valore dato da value
. property
può utilizzare la notazione a punti e a parentesi quadre per il riferimento annidato. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.nestedPropertyVal', () => {
assert.nestedPropertyVal({ tea: { green: 'matcha' } }, 'tea.green', 'matcha');
});
notNestedPropertyVal
- Tipo:
<T>(object: T, property: string, value: any, message?: string) => void
Afferma che object
non ha una proprietà con il nome property
e un valore dato da value
. property
può utilizzare la notazione a punti e a parentesi quadre per il riferimento annidato. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.notNestedPropertyVal', () => {
assert.notNestedPropertyVal(
{ tea: { green: 'matcha' } },
'tea.green',
'konacha'
);
assert.notNestedPropertyVal(
{ tea: { green: 'matcha' } },
'coffee.green',
'matcha'
);
});
deepNestedPropertyVal
- Tipo:
<T>(object: T, property: string, value: any, message?: string) => void
Afferma che object
ha una proprietà con il nome property
e un valore dato da value
. property
può utilizzare la notazione a punti e a parentesi quadre per il riferimento annidato. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.deepNestedPropertyVal', () => {
assert.deepNestedPropertyVal(
{ tea: { green: 'matcha' } },
'tea.green',
'matcha'
);
assert.deepNestedPropertyVal(
{ tea: { green: 'matcha' } },
'coffee.green',
'matcha'
);
});
notDeepNestedPropertyVal
- Tipo:
<T>(object: T, property: string, value: any, message?: string) => void
Afferma che object
non ha una proprietà con il nome property
e un valore dato da value
. property
può utilizzare la notazione a punti e a parentesi quadre per il riferimento annidato. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.notDeepNestedPropertyVal', () => {
assert.notDeepNestedPropertyVal(
{ tea: { green: { matcha: 'yum' } } },
'tea.green',
{ oolong: 'yum' }
);
assert.notDeepNestedPropertyVal(
{ tea: { green: { matcha: 'yum' } } },
'tea.green',
{ matcha: 'yuck' }
);
assert.notDeepNestedPropertyVal(
{ tea: { green: { matcha: 'yum' } } },
'tea.black',
{ matcha: 'yum' }
);
});
lengthOf
- Tipo:
<T extends { readonly length?: number | undefined } | { readonly size?: number | undefined }>(object: T, length: number, message?: string) => void
Afferma che object
ha una length
o size
con il valore atteso.
import { assert, test } from 'vitest';
test('assert.lengthOf', () => {
assert.lengthOf([1, 2, 3], 3, 'l\'array ha lunghezza 3');
assert.lengthOf('foobar', 6, 'la stringa ha lunghezza 6');
assert.lengthOf(new Set([1, 2, 3]), 3, 'il set ha dimensione 3');
assert.lengthOf(
new Map([
['a', 1],
['b', 2],
['c', 3],
]),
3,
'la mappa ha dimensione 3'
);
});
hasAnyKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
ha almeno una delle keys
fornite. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.hasAnyKeys', () => {
assert.hasAnyKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'iDontExist', 'baz']);
assert.hasAnyKeys(
{ foo: 1, bar: 2, baz: 3 },
{ foo: 30, iDontExist: 99, baz: 1337 }
);
assert.hasAnyKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ foo: 1 }, 'key']
);
assert.hasAnyKeys(new Set([{ foo: 'bar' }, 'anotherKey']), [
{ foo: 'bar' },
'anotherKey',
]);
});
hasAllKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
ha tutte e solo tutte le keys
fornite. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.hasAllKeys', () => {
assert.hasAllKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'bar', 'baz']);
assert.hasAllKeys(
{ foo: 1, bar: 2, baz: 3 },
{ foo: 30, bar: 99, baz: 1337 }
);
assert.hasAllKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ foo: 1 }, 'key']
);
assert.hasAllKeys(
new Set([{ foo: 'bar' }, 'anotherKey'], [{ foo: 'bar' }, 'anotherKey'])
);
});
containsAllKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
ha tutte le keys
fornite ma potrebbe avere chiavi aggiuntive non elencate. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.containsAllKeys', () => {
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'baz']);
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, ['foo', 'bar', 'baz']);
assert.containsAllKeys({ foo: 1, bar: 2, baz: 3 }, { foo: 30, baz: 1337 });
assert.containsAllKeys(
{ foo: 1, bar: 2, baz: 3 },
{ foo: 30, bar: 99, baz: 1337 }
);
assert.containsAllKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ foo: 1 }]
);
assert.containsAllKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ foo: 1 }, 'key']
);
assert.containsAllKeys(
new Set([{ foo: 'bar' }, 'anotherKey'], [{ foo: 'bar' }])
);
assert.containsAllKeys(
new Set([{ foo: 'bar' }, 'anotherKey'], [{ foo: 'bar' }, 'anotherKey'])
);
});
doesNotHaveAnyKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
non ha nessuna delle keys
fornite. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.doesNotHaveAnyKeys', () => {
assert.doesNotHaveAnyKeys({ foo: 1, bar: 2, baz: 3 }, [
'one',
'two',
'example',
]);
assert.doesNotHaveAnyKeys(
{ foo: 1, bar: 2, baz: 3 },
{ one: 1, two: 2, example: 'foo' }
);
assert.doesNotHaveAnyKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ one: 'two' }, 'example']
);
assert.doesNotHaveAnyKeys(
new Set([{ foo: 'bar' }, 'anotherKey'], [{ one: 'two' }, 'example'])
);
});
doesNotHaveAllKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
non ha almeno una delle keys
fornite. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.doesNotHaveAllKeys', () => {
assert.doesNotHaveAllKeys({ foo: 1, bar: 2, baz: 3 }, [
'one',
'two',
'example',
]);
assert.doesNotHaveAllKeys(
{ foo: 1, bar: 2, baz: 3 },
{ one: 1, two: 2, example: 'foo' }
);
assert.doesNotHaveAllKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ one: 'two' }, 'example']
);
assert.doesNotHaveAllKeys(new Set([{ foo: 'bar' }, 'anotherKey']), [
{ one: 'two' },
'example',
]);
});
hasAnyDeepKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
ha almeno una delle keys
fornite. Poiché Set
e Map
possono avere oggetti come chiavi, puoi usare questa asserzione per eseguire un confronto profondo. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.hasAnyDeepKeys', () => {
assert.hasAnyDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[1, 2],
]),
{ one: 'one' }
);
assert.hasAnyDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[1, 2],
]),
[{ one: 'one' }, { two: 'two' }]
);
assert.hasAnyDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[{ two: 'two' }, 'valueTwo'],
]),
[{ one: 'one' }, { two: 'two' }]
);
assert.hasAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), {
one: 'one',
});
assert.hasAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [
{ one: 'one' },
{ three: 'three' },
]);
assert.hasAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [
{ one: 'one' },
{ two: 'two' },
]);
});
hasAllDeepKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
ha tutte e solo tutte le keys
fornite. Poiché Set
e Map
possono avere oggetti come chiavi, puoi usare questa asserzione per eseguire un confronto profondo. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.hasAllDeepKeys', () => {
assert.hasAllDeepKeys(new Map([[{ one: 'one' }, 'valueOne']]), {
one: 'one',
});
assert.hasAllDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[{ two: 'two' }, 'valueTwo'],
]),
[{ one: 'one' }, { two: 'two' }]
);
assert.hasAllDeepKeys(new Set([{ one: 'one' }]), { one: 'one' });
assert.hasAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [
{ one: 'one' },
{ two: 'two' },
]);
});
containsAllDeepKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
contiene tutte le keys
fornite. Poiché Set
e Map
possono avere oggetti come chiavi, puoi usare questa asserzione per eseguire un confronto profondo. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.containsAllDeepKeys', () => {
assert.containsAllDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[1, 2],
]),
{ one: 'one' }
);
assert.containsAllDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[{ two: 'two' }, 'valueTwo'],
]),
[{ one: 'one' }, { two: 'two' }]
);
assert.containsAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), {
one: 'one',
});
assert.containsAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [
{ one: 'one' },
{ two: 'two' },
]);
});
doesNotHaveAnyDeepKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
non ha nessuna delle keys
fornite. Poiché Set
e Map
possono avere oggetti come chiavi, puoi usare questa asserzione per eseguire un confronto profondo. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.doesNotHaveAnyDeepKeys', () => {
assert.doesNotHaveAnyDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[1, 2],
]),
{ thisDoesNot: 'exist' }
);
assert.doesNotHaveAnyDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[{ two: 'two' }, 'valueTwo'],
]),
[{ twenty: 'twenty' }, { fifty: 'fifty' }]
);
assert.doesNotHaveAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), {
twenty: 'twenty',
});
assert.doesNotHaveAnyDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [
{ twenty: 'twenty' },
{ fifty: 'fifty' },
]);
});
doesNotHaveAllDeepKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Afferma che object
non ha almeno una delle keys
fornite. Poiché Set
e Map
possono avere oggetti come chiavi, puoi usare questa asserzione per eseguire un confronto profondo. Puoi anche fornire un singolo oggetto invece di un array di chiavi e le sue chiavi verranno utilizzate come set di chiavi atteso.
import { assert, test } from 'vitest';
test('assert.doesNotHaveAllDeepKeys', () => {
assert.doesNotHaveAllDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[1, 2],
]),
{ thisDoesNot: 'exist' }
);
assert.doesNotHaveAllDeepKeys(
new Map([
[{ one: 'one' }, 'valueOne'],
[{ two: 'two' }, 'valueTwo'],
]),
[{ twenty: 'twenty' }, { one: 'one' }]
);
assert.doesNotHaveAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), {
twenty: 'twenty',
});
assert.doesNotHaveAllDeepKeys(new Set([{ one: 'one' }, { two: 'two' }]), [
{ one: 'one' },
{ fifty: 'fifty' },
]);
});
throws
- Tipo:
(fn: () => void, errMsgMatcher?: RegExp | string, ignored?: any, message?: string) => void
(fn: () => void, errorLike?: ErrorConstructor | Error | null, errMsgMatcher?: RegExp | string | null, message?: string) => void
- Alias:
throw
Throw
Se errorLike
è un costruttore di Error
, afferma che fn
lancerà un errore che è un'istanza di errorLike
. Se errorLike
è un'istanza di Error
, afferma che l'errore lanciato è la stessa istanza di errorLike
. Se errMsgMatcher
è fornito, afferma anche che l'errore lanciato avrà un messaggio che corrisponde a errMsgMatcher
.
import { assert, test } from 'vitest';
test('assert.throws', () => {
assert.throws(fn, 'L\'errore lanciato deve avere questo messaggio');
assert.throws(fn, /L\'errore lanciato deve avere un messaggio che corrisponde a questo/);
assert.throws(fn, ReferenceError);
assert.throws(fn, errorInstance);
assert.throws(
fn,
ReferenceError,
'L\'errore lanciato deve essere un ReferenceError e avere questo messaggio'
);
assert.throws(
fn,
errorInstance,
'L\'errore lanciato deve essere la stessa istanza di errore e avere questo messaggio'
);
assert.throws(
fn,
ReferenceError,
/L\'errore lanciato deve essere un ReferenceError e corrispondere a questo/
);
assert.throws(
fn,
errorInstance,
/L\'errore lanciato deve essere la stessa istanza di errore e corrispondere a questo/
);
});
doesNotThrow
- Tipo:
(fn: () => void, errMsgMatcher?: RegExp | string, ignored?: any, message?: string) => void
- Tipo:
(fn: () => void, errorLike?: ErrorConstructor | Error | null, errMsgMatcher?: RegExp | string | null, message?: string) => void
Se errorLike
è un costruttore di Error
, afferma che fn
non lancerà un errore che è un'istanza di errorLike
. Se errorLike
è un'istanza di Error
, afferma che l'errore lanciato non è la stessa istanza di errorLike
. Se errMsgMatcher
è fornito, afferma anche che l'errore lanciato non avrà un messaggio che corrisponde a errMsgMatcher
.
import { assert, test } from 'vitest';
test('assert.doesNotThrow', () => {
assert.doesNotThrow(fn, 'Qualsiasi errore lanciato non deve avere questo messaggio');
assert.doesNotThrow(fn, /Qualsiasi errore lanciato non deve corrispondere a questo/);
assert.doesNotThrow(fn, Error);
assert.doesNotThrow(fn, errorInstance);
assert.doesNotThrow(fn, Error, 'L\'errore non deve avere questo messaggio');
assert.doesNotThrow(fn, errorInstance, 'L\'errore non deve avere questo messaggio');
assert.doesNotThrow(fn, Error, /L\'errore non deve corrispondere a questo/);
assert.doesNotThrow(fn, errorInstance, /L\'errore non deve corrispondere a questo/);
});
operator
- Tipo:
(val1: OperatorComparable, operator: Operator, val2: OperatorComparable, message?: string) => void
Confronta val1
e val2
usando operator
.
import { assert, test } from 'vitest';
test('assert.operator', () => {
assert.operator(1, '<', 2, 'tutto è ok');
});
closeTo
- Tipo:
(actual: number, expected: number, delta: number, message?: string) => void
- Alias:
approximately
Afferma che actual
è uguale a expected
, entro un intervallo di +/- delta
.
import { assert, test } from 'vitest';
test('assert.closeTo', () => {
assert.closeTo(1.5, 1, 0.5, 'i numeri sono vicini');
});
sameMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
hanno gli stessi membri in qualsiasi ordine. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.sameMembers', () => {
assert.sameMembers([1, 2, 3], [2, 1, 3], 'stessi membri');
});
notSameMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
non hanno gli stessi membri in qualsiasi ordine. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.notSameMembers', () => {
assert.notSameMembers([1, 2, 3], [5, 1, 3], 'non stessi membri');
});
sameDeepMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
hanno gli stessi membri in qualsiasi ordine. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.sameDeepMembers', () => {
assert.sameDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'stessi membri profondi'
);
});
notSameDeepMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
non hanno gli stessi membri in qualsiasi ordine. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.notSameDeepMembers', () => {
assert.notSameDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'non stessi membri profondi'
);
});
sameOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
hanno gli stessi membri nello stesso ordine. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.sameOrderedMembers', () => {
assert.sameOrderedMembers([1, 2, 3], [1, 2, 3], 'stessi membri ordinati');
});
notSameOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
non hanno gli stessi membri nello stesso ordine. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.notSameOrderedMembers', () => {
assert.notSameOrderedMembers(
[1, 2, 3],
[2, 1, 3],
'non stessi membri ordinati'
);
});
sameDeepOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
hanno gli stessi membri nello stesso ordine. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.sameDeepOrderedMembers', () => {
assert.sameDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { b: 2 }, { c: 3 }],
'stessi membri profondi ordinati'
);
});
notSameDeepOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Afferma che set1
e set2
non hanno gli stessi membri nello stesso ordine. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.notSameDeepOrderedMembers', () => {
assert.notSameDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { b: 2 }, { z: 5 }],
'non stessi membri profondi ordinati'
);
assert.notSameDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'non stessi membri profondi ordinati'
);
});
includeMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
è incluso in superset
in qualsiasi ordine. Utilizza un controllo di uguaglianza stretta (===
). I duplicati vengono ignorati.
import { assert, test } from 'vitest';
test('assert.includeMembers', () => {
assert.includeMembers([1, 2, 3], [2, 1, 2], 'include membri');
});
notIncludeMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
non è incluso in superset
in qualsiasi ordine. Utilizza un controllo di uguaglianza stretta (===
). I duplicati vengono ignorati.
import { assert, test } from 'vitest';
test('assert.notIncludeMembers', () => {
assert.notIncludeMembers([1, 2, 3], [5, 1], 'non include membri');
});
includeDeepMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
è incluso in superset
in qualsiasi ordine. Utilizza un controllo di uguaglianza profonda. I duplicati vengono ignorati.
import { assert, test } from 'vitest';
test('assert.includeDeepMembers', () => {
assert.includeDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { b: 2 }],
'include membri profondi'
);
});
notIncludeDeepMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
non è incluso in superset
in qualsiasi ordine. Utilizza un controllo di uguaglianza profonda. I duplicati vengono ignorati.
import { assert, test } from 'vitest';
test('assert.notIncludeDeepMembers', () => {
assert.notIncludeDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { f: 5 }],
'non include membri profondi'
);
});
includeOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
è incluso in superset
nello stesso ordine, a partire dal primo elemento in superset
. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.includeOrderedMembers', () => {
assert.includeOrderedMembers([1, 2, 3], [1, 2], 'include membri ordinati');
});
notIncludeOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
non è incluso in superset
nello stesso ordine, a partire dal primo elemento in superset
. Utilizza un controllo di uguaglianza stretta (===
).
import { assert, test } from 'vitest';
test('assert.notIncludeOrderedMembers', () => {
assert.notIncludeOrderedMembers(
[1, 2, 3],
[2, 1],
'non include membri ordinati'
);
assert.notIncludeOrderedMembers(
[1, 2, 3],
[2, 3],
'non include membri ordinati'
);
});
includeDeepOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
è incluso in superset
nello stesso ordine, a partire dal primo elemento in superset
. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.includeDeepOrderedMembers', () => {
assert.includeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { b: 2 }],
'include membri profondi ordinati'
);
});
notIncludeDeepOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Afferma che subset
non è incluso in superset
nello stesso ordine, a partire dal primo elemento in superset
. Utilizza un controllo di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.notIncludeDeepOrderedMembers', () => {
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { f: 5 }],
'non include membri profondi ordinati'
);
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }],
'non include membri profondi ordinati'
);
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { c: 3 }],
'non include membri profondi ordinati'
);
});
oneOf
- Tipo:
<T>(inList: T, list: T[], message?: string) => void
Afferma che il valore non-oggetto, non-array inList
appare nell'array piatto list
.
import { assert, test } from 'vitest';
test('assert.oneOf', () => {
assert.oneOf(1, [2, 1], 'Non trovato nella lista');
});
changes
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Afferma che un modifier
cambia la property
di un object
.
import { assert, test } from 'vitest';
test('assert.changes', () => {
const obj = { val: 10 };
function fn() {
obj.val = 22;
}
assert.changes(fn, obj, 'val');
});
changesBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
Afferma che un modifier
cambia la property
di un object
per un dato change
.
import { assert, test } from 'vitest';
test('assert.changesBy', () => {
const obj = { val: 10 };
function fn() {
obj.val += 2;
}
assert.changesBy(fn, obj, 'val', 2);
});
doesNotChange
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Afferma che un modifier
non cambia la property
di un object
.
import { assert, test } from 'vitest';
test('assert.doesNotChange', () => {
const obj = { val: 10 };
function fn() {
obj.val += 2;
}
assert.doesNotChange(fn, obj, 'val', 2);
});
changesButNotBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change:number, message?: string) => void
Afferma che un modifier
cambia la property
di un object
o il valore di ritorno di un modifier
, ma non per un dato change
.
import { assert, test } from 'vitest';
test('assert.changesButNotBy', () => {
const obj = { val: 10 };
function fn() {
obj.val += 10;
}
assert.changesButNotBy(fn, obj, 'val', 5);
});
increases
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Afferma che un modifier
aumenta la property
numerica di un object
.
import { assert, test } from 'vitest';
test('assert.increases', () => {
const obj = { val: 10 };
function fn() {
obj.val = 13;
}
assert.increases(fn, obj, 'val');
});
increasesBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
Afferma che un modifier
aumenta la property
numerica di un object
o il valore di ritorno di un modifier
per un dato change
.
import { assert, test } from 'vitest';
test('assert.increasesBy', () => {
const obj = { val: 10 };
function fn() {
obj.val += 10;
}
assert.increasesBy(fn, obj, 'val', 10);
});
doesNotIncrease
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Afferma che un modifier
non aumenta la property
numerica di un object
.
import { assert, test } from 'vitest';
test('assert.doesNotIncrease', () => {
const obj = { val: 10 };
function fn() {
obj.val = 8;
}
assert.doesNotIncrease(fn, obj, 'val');
});
increasesButNotBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
Afferma che un modifier
aumenta la property
numerica di un object
o il valore di ritorno di un modifier
, ma non per un dato change
.
import { assert, test } from 'vitest';
test('assert.increasesButNotBy', () => {
const obj = { val: 10 };
function fn() {
obj.val += 15;
}
assert.increasesButNotBy(fn, obj, 'val', 10);
});
decreases
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Afferma che un modifier
diminuisce la property
numerica di un object
.
import { assert, test } from 'vitest';
test('assert.decreases', () => {
const obj = { val: 10 };
function fn() {
obj.val = 5;
}
assert.decreases(fn, obj, 'val');
});
decreasesBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
Afferma che un modifier
diminuisce la property
numerica di un object
o il valore di ritorno di un modifier
per un dato change
.
import { assert, test } from 'vitest';
test('assert.decreasesBy', () => {
const obj = { val: 10 };
function fn() {
obj.val -= 5;
}
assert.decreasesBy(fn, obj, 'val', 5);
});
doesNotDecrease
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Afferma che un modifier
non diminuisce la property
numerica di un object
.
import { assert, test } from 'vitest';
test('assert.doesNotDecrease', () => {
const obj = { val: 10 };
function fn() {
obj.val = 15;
}
assert.doesNotDecrease(fn, obj, 'val');
});
doesNotDecreaseBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
Afferma che un modifier
non diminuisce la property
numerica di un object
o il valore di ritorno di un modifier
per un dato change
.
import { assert, test } from 'vitest';
test('assert.doesNotDecreaseBy', () => {
const obj = { val: 10 };
function fn() {
obj.val = 5;
}
assert.doesNotDecreaseBy(fn, obj, 'val', 1);
});
decreasesButNotBy
- Tipo:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
Afferma che un modifier
diminuisce la property
numerica di un object
o il valore di ritorno di un modifier
, ma non per un dato change
.
import { assert, test } from 'vitest';
test('assert.decreasesButNotBy', () => {
const obj = { val: 10 };
function fn() {
obj.val = 5;
}
assert.decreasesButNotBy(fn, obj, 'val', 1);
});
ifError
- Tipo:
<T>(object: T, message?: string) => void
Se object
è un valore "truthy", lancia un errore. Questo è stato aggiunto per consentire a Chai di essere un sostituto diretto della classe assert
di Node.
import { assert, test } from 'vitest';
test('assert.ifError', () => {
const err = new Error('Sono un errore personalizzato');
assert.ifError(err); // Rilancia err!
});
isExtensible
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
extensible
Afferma che object
è estensibile (possono essergli aggiunte nuove proprietà).
import { assert, test } from 'vitest';
test('assert.isExtensible', () => {
assert.isExtensible({});
});
isNotExtensible
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
notExtensible
Afferma che object
non è estensibile (non possono essergli aggiunte nuove proprietà).
import { assert, test } from 'vitest';
test('assert.isNotExtensible', () => {
const nonExtensibleObject = Object.preventExtensions({});
const sealedObject = Object.seal({});
const frozenObject = Object.freeze({});
assert.isNotExtensible(nonExtensibleObject);
assert.isNotExtensible(sealedObject);
assert.isNotExtensible(frozenObject);
});
isSealed
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
sealed
Afferma che object
è sigillato (non possono essergli aggiunte nuove proprietà e le sue proprietà esistenti non possono essere rimosse).
import { assert, test } from 'vitest';
test('assert.isSealed', () => {
const sealedObject = Object.seal({});
const frozenObject = Object.freeze({});
assert.isSealed(sealedObject);
assert.isSealed(frozenObject);
});
isNotSealed
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
notSealed
Afferma che object
non è sigillato (possono essergli aggiunte nuove proprietà e le sue proprietà esistenti possono essere rimosse).
import { assert, test } from 'vitest';
test('assert.isNotSealed', () => {
assert.isNotSealed({});
});
isFrozen
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
frozen
Afferma che l'oggetto è congelato (non possono essergli aggiunte nuove proprietà e le sue proprietà esistenti non possono essere modificate).
import { assert, test } from 'vitest';
test('assert.isFrozen', () => {
const frozenObject = Object.freeze({});
assert.frozen(frozenObject);
});
isNotFrozen
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
notFrozen
Afferma che object
non è congelato (possono essergli aggiunte nuove proprietà e le sue proprietà esistenti possono essere modificate).
import { assert, test } from 'vitest';
test('assert.isNotFrozen', () => {
assert.isNotFrozen({});
});
isEmpty
- Tipo:
<T>(target: T, message?: string) => void
- Alias:
empty
Afferma che il target
non contiene alcun valore. Per array e stringhe, controlla la proprietà length
. Per le istanze di Map
e Set
, controlla la proprietà size
. Per gli oggetti non-funzione, ottiene il conteggio delle proprie chiavi stringa enumerabili.
import { assert, test } from 'vitest';
test('assert.isEmpty', () => {
assert.isEmpty([]);
assert.isEmpty('');
assert.isEmpty(new Map());
assert.isEmpty({});
});
isNotEmpty
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
notEmpty
Afferma che il target
contiene valori. Per array e stringhe, controlla la proprietà length
. Per le istanze di Map
e Set
, controlla la proprietà size
. Per gli oggetti non-funzione, ottiene il conteggio delle proprie chiavi stringa enumerabili.
import { assert, test } from 'vitest';
test('assert.isNotEmpty', () => {
assert.isNotEmpty([1, 2]);
assert.isNotEmpty('34');
assert.isNotEmpty(new Set([5, 6]));
assert.isNotEmpty({ key: 7 });
});