assert
Vitest riesporta il metodo assert
da chai
per la verifica delle invarianti.
assert
- Tipo:
(expression: any, message?: string) => asserts expression
Verifica che l'expression
fornita sia truthy; in caso contrario, l'asserzione fallisce.
import { assert, test } from 'vitest';
test('assert', () => {
assert('foo' !== 'bar', 'foo non deve essere uguale a bar');
});
fail
- Tipo:
(message?: string) => never
<T>(actual: T, expected: T, message?: string, operator?: string) => never
Forza intenzionalmente il fallimento di un'asserzione.
import { assert, test } from 'vitest';
test('assert.fail', () => {
assert.fail('messaggio di errore');
assert.fail('foo', 'bar', 'foo non corrisponde a bar', '===');
});
isOk
- Tipo:
<T>(value: T, message?: string) => void
- Alias
ok
Verifica che il value
fornito sia truthy.
import { assert, test } from 'vitest';
test('assert.isOk', () => {
assert.isOk('foo', 'ogni valore truthy è considerato valido');
assert.isOk(false, 'questo fallirà perché false non è truthy');
});
isNotOk
- Tipo:
<T>(value: T, message?: string) => void
- Alias
notOk
Verifica che il value
fornito sia falsy.
import { assert, test } from 'vitest';
test('assert.isNotOk', () => {
assert.isNotOk(
'foo',
'questo fallirà, poiché ogni valore truthy non è considerato falsy'
);
assert.isNotOk(false, 'questo passerà perché false è falsy');
});
equal
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Verifica l'uguaglianza non stretta (==) tra actual
(valore effettivo) e expected
(valore previsto).
import { assert, test } from 'vitest';
test('assert.equal', () => {
assert.equal(Math.sqrt(4), '2');
});
notEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Verifica la disuguaglianza non stretta (!=) tra actual
(valore effettivo) e expected
(valore previsto).
import { assert, test } from 'vitest';
test('assert.equal', () => {
assert.notEqual(Math.sqrt(4), 3);
});
strictEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Verifica l'uguaglianza stretta (===) tra actual
(valore effettivo) e expected
(valore previsto).
import { assert, test } from 'vitest';
test('assert.strictEqual', () => {
assert.strictEqual(Math.sqrt(4), 2);
});
deepEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Verifica che actual
(valore effettivo) sia profondamente uguale a expected
(valore previsto).
import { assert, test } from 'vitest';
test('assert.deepEqual', () => {
assert.deepEqual({ color: 'green' }, { color: 'green' });
});
notDeepEqual
- Tipo:
<T>(actual: T, expected: T, message?: string) => void
Verifica che actual
(valore effettivo) non sia profondamente uguale a expected
(valore previsto).
import { assert, test } from 'vitest';
test('assert.notDeepEqual', () => {
assert.notDeepEqual({ color: 'green' }, { color: 'red' });
});
isAbove
- Tipo:
(valueToCheck: number, valueToBeAbove: number, message?: string) => void
Verifica che valueToCheck
(valore da controllare) sia strettamente maggiore di (>) valueToBeAbove
(valore di riferimento).
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
Verifica che valueToCheck
(valore da controllare) sia maggiore o uguale a (>=) valueToBeAtLeast
(valore minimo).
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
Verifica che valueToCheck
(valore da controllare) sia strettamente minore di (<) valueToBeBelow
(valore di riferimento).
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
Verifica che valueToCheck
(valore da controllare) sia minore o uguale a (≤) valueToBeAtMost
(valore massimo).
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
Verifica che value
(il valore) sia true
.
import { assert, test } from 'vitest';
const testPassed = true;
test('assert.isTrue', () => {
assert.isTrue(testPassed);
});
isNotTrue
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) non sia true
.
import { assert, test } from 'vitest';
const testPassed = 'ok';
test('assert.isNotTrue', () => {
assert.isNotTrue(testPassed);
});
isFalse
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) sia false
.
import { assert, test } from 'vitest';
const testPassed = false;
test('assert.isFalse', () => {
assert.isFalse(testPassed);
});
isNotFalse
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) non sia false
.
import { assert, test } from 'vitest';
const testPassed = 'no';
test('assert.isNotFalse', () => {
assert.isNotFalse(testPassed);
});
isNull
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) sia null
.
import { assert, test } from 'vitest';
const error = null;
test('assert.isNull', () => {
assert.isNull(error, 'error è null');
});
isNotNull
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) non sia null
.
import { assert, test } from 'vitest';
const error = { message: 'error occurred' };
test('assert.isNotNull', () => {
assert.isNotNull(error, 'error non è null ma un oggetto');
});
isNaN
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) sia NaN
(Not a Number).
import { assert, test } from 'vitest';
const calculation = 1 * 'viitest';
test('assert.isNaN', () => {
assert.isNaN(calculation, "1 * 'vitest' è NaN");
});
isNotNaN
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) non sia NaN
(Not a Number).
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
Verifica che value
(il valore) non sia né null
né undefined
.
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.exists', () => {
assert.exists(name, 'foo non è né nullo né indefinito');
});
notExists
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
(il valore) sia 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
Verifica che value
(il valore) sia 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
Verifica che value
(il valore) non sia 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
Verifica chevalue
(il valore) sia 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
Verifica che value
(il valore) non sia 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
Verifica che value
(il valore) sia un oggetto di tipo Object
(come indicato da Object.prototype.toString
). Questa asserzione non si applica a oggetti di sottoclassi.
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
Verifica che value
non sia un oggetto di tipo Object
(come determinato da Object.prototype.toString
). Questa asserzione non considera gli oggetti derivati da sottoclassi.
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
Verifica che value
sia 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
Verifica che value
non sia 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
Verifica che value
sia 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
Verifica che value
non sia 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
Verifica che value
sia 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
Verifica che value
non sia 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
Verifica che value
sia un numero finito (diverso da NaN
e Infinity
).
import { assert, test } from 'vitest';
const colors = 3;
test('assert.isFinite', () => {
assert.isFinite(colors, 'colors è un numero finito (non NaN o Infinity)');
});
isBoolean
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
sia un valore booleano.
import { assert, test } from 'vitest';
const isReady = true;
test('assert.isBoolean', () => {
assert.isBoolean(isReady, 'isReady è un valore booleano');
});
isNotBoolean
- Tipo:
<T>(value: T, message?: string) => void
Verifica che value
non sia un valore booleano.
import { assert, test } from 'vitest';
const isReady = 'sure';
test('assert.isBoolean', () => {
assert.isBoolean(isReady, 'isReady non è un booleano, ma una stringa');
});
typeOf
- Tipo:
<T>(value: T, name: string, message?: string) => void
Verifica che il tipo di value
corrisponda a name
, come determinato da Object.prototype.toString
.
import { assert, test } from 'vitest';
test('assert.typeOf', () => {
assert.typeOf({ color: 'red' }, 'object', 'è un oggetto');
assert.typeOf(['red', 'green'], 'array', 'è un array');
assert.typeOf('red', 'string', 'è una stringa');
assert.typeOf(/red/, 'regexp', 'è una espressione regolare');
assert.typeOf(null, 'null', 'è un valore null');
assert.typeOf(undefined, 'undefined', 'è un valore undefined');
});
notTypeOf
- Tipo:
<T>(value: T, name: string, message?: string) => void
Verifica che il tipo di value
non corrisponda a 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
Verifica che value
sia 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
Verifica che value
non sia 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, 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
Verifica che haystack
includa needle
. Può essere utilizzato per verificare l'inclusione di un valore in un array, di una sottostringa in una stringa o di un sottoinsieme di proprietà in un oggetto.
import { assert, test } from 'vitest';
test('assert.include', () => {
assert.include([1, 2, 3], 2, "l'array include il valore 2");
assert.include('foobar', 'foo', 'la stringa include la sottostringa "foo"');
assert.include(
{ foo: 'bar', hello: 'universe' },
{ foo: 'bar' },
'l\'oggetto include la proprietà { foo: "bar" }'
);
});
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
Verifica che haystack
non includa needle
. Può essere utilizzato per verificare l'assenza di un valore in un array, di una sottostringa in una stringa o di 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 il valore 4");
assert.notInclude(
'foobar',
'baz',
"la stringa 'foobar' non contiene la sottostringa 'baz'"
);
assert.notInclude(
{ foo: 'bar', hello: 'universe' },
{ foo: 'baz' },
"l'oggetto non contiene la proprietà { foo: 'baz' }"
);
});
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
Verifica che haystack
includa needle
utilizzando un confronto profondo. Può essere utilizzato per verificare l'inclusione di un valore in un array o di un sottoinsieme di proprietà in un oggetto.
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
Verifica che haystack
non includa needle
utilizzando un confronto profondo. Può essere utilizzato per verificare l'assenza di un valore in un array o di un sottoinsieme di proprietà in un oggetto.
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
Verifica che haystack
includa needle
, consentendo la notazione con punto e parentesi quadre per fare riferimento a proprietà nidificate. ‘[]’ e ‘.’ nei nomi delle proprietà possono essere preceduti da escape usando doppi backslash.
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
Verifica che haystack
non includa needle
, consentendo la notazione con punto e parentesi quadre per fare riferimento a proprietà nidificate. ‘[]’ e ‘.’ nei nomi delle proprietà possono essere preceduti da escape usando doppi backslash.
import { assert, test } from 'vitest';
test('assert.nestedInclude', () => {
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
Verifica che haystack
includa needle
utilizzando un confronto profondo, consentendo la notazione con punto e parentesi quadre per fare riferimento a proprietà nidificate. ‘[]’ e ‘.’ nei nomi delle proprietà possono essere preceduti da escape usando doppi backslash.
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
Verifica che haystack
non includa needle
utilizzando un confronto profondo, consentendo la notazione con punto e parentesi quadre per fare riferimento a proprietà nidificate. ‘[]’ e ‘.’ nei nomi delle proprietà possono essere preceduti da escape usando doppi backslash.
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
Verifica che haystack
includa needle
, considerando solo le proprietà dirette dell'oggetto e 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
Verifica che haystack
non includa needle
, considerando solo le proprietà dirette dell'oggetto e 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
Verifica che haystack
contenga needle
. Può essere utilizzato per verificare l'inclusione di un sottoinsieme di proprietà in un oggetto, ignorando le proprietà ereditate ed eseguendo un confronto di 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
Verifica che haystack
non contenga needle
. Può essere utilizzato per verificare l'assenza di un sottoinsieme di proprietà in un oggetto, ignorando le proprietà ereditate ed eseguendo un confronto di 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
Verifica che value
corrisponda all'espressione regolare regexp
.
import { assert, test } from 'vitest';
test('assert.match', () => {
assert.match('foobar', /^foo/, 'regexp matches');
});
notMatch
- Tipo:
(value: string, regexp: RegExp, message?: string) => void
Verifica che value
non corrisponda all'espressione regolare regexp
.
import { assert, test } from 'vitest';
test('assert.notMatch', () => {
assert.notMatch('foobar', /^foo/, 'regexp does not match');
});
property
- Tipo:
<T>(object: T, property: string, message?: string) => void
Verifica che object
abbia una proprietà, diretta o ereditata, denominata 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
Verifica che object
non abbia una proprietà, diretta o ereditata, denominata 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
Verifica che object
abbia una proprietà, diretta o ereditata, denominata property
con un valore uguale a value
. Utilizza un confronto di uguaglianza stretta (===).
import { assert, test } from 'vitest';
test('assert.notPropertyVal', () => {
assert.propertyVal({ tea: 'is good' }, 'tea', 'is good');
});
notPropertyVal
- Tipo:
<T, V>(object: T, property: string, value: V, message?: string) => void
Verifica che object
non abbia una proprietà, diretta o ereditata, denominata property
con un valore diverso da value
. Utilizza un confronto 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
Verifica che object
abbia una proprietà, diretta o ereditata, denominata property
con un valore uguale a value
. Esegue un confronto profondo (deep equality).
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
Verifica che object
non abbia una proprietà, diretta o ereditata, denominata property
con un valore diverso da value
. Esegue un confronto profondo (deep equality).
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
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
Verifica che object
abbia una proprietà, diretta o ereditata, denominata property
, che può essere specificata come stringa usando la notazione puntata (dot notation) o con parentesi quadre.
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
assert.nestedProperty({ tea: { green: 'matcha' } }, 'tea.green');
});
notNestedProperty
- Tipo:
<T>(object: T, property: string, message?: string) => void
Verifica che object
non abbia una proprietà, diretta o ereditata, denominata property
, che può essere specificata come stringa usando la notazione puntata (dot notation) o con parentesi quadre.
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
assert.notNestedProperty({ tea: { green: 'matcha' } }, 'tea.oolong');
});
nestedPropertyVal
- Tipo:
<T>(object: T, property: string, value: any, message?: string) => void
Verifica che object
abbia una proprietà denominata property
con un valore uguale a value
. property
può utilizzare la notazione a punti e tra parentesi per il riferimento annidato. Utilizza un confronto 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
Verifica che object
non abbia una proprietà denominata property
con un valore uguale a value
. property
può utilizzare la notazione a punti e tra parentesi per il riferimento annidato. Utilizza un confronto 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
Verifica che object
abbia una proprietà denominata property
con un valore uguale a value
. property
può utilizzare la notazione a punti e tra parentesi per il riferimento annidato. Esegue un confronto profondo (deep equality).
import { assert, test } from 'vitest';
test('assert.notNestedPropertyVal', () => {
assert.notNestedPropertyVal(
{ tea: { green: 'matcha' } },
'tea.green',
'konacha'
);
assert.notNestedPropertyVal(
{ tea: { green: 'matcha' } },
'coffee.green',
'matcha'
);
});
notDeepNestedPropertyVal
- Tipo:
<T>(object: T, property: string, value: any, message?: string) => void
Verifica che object
non abbia una proprietà denominata property
con un valore diverso da value
. property
può utilizzare la notazione a punti e tra parentesi per il riferimento annidato. Esegue un confronto profondo (deep equality).
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
Verifica che object
abbia una proprietà length
(lunghezza) o size
(dimensione) con il valore atteso.
import { assert, test } from 'vitest';
test('assert.lengthOf', () => {
assert.lengthOf([1, 2, 3], 3, 'array has length of 3');
assert.lengthOf('foobar', 6, 'string has length of 6');
assert.lengthOf(new Set([1, 2, 3]), 3, 'set has size of 3');
assert.lengthOf(
new Map([
['a', 1],
['b', 2],
['c', 3],
]),
3,
'map has size of 3'
);
});
hasAnyKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Verifica che object
abbia almeno una delle keys
fornite. Puoi anche passare un singolo oggetto invece di un array di chiavi; in tal caso, le sue chiavi verranno utilizzate come insieme di chiavi specificato.
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
Verifica che object
abbia tutte e soltanto le keys
fornite. Puoi anche passare un singolo oggetto invece di un array di chiavi; in tal caso, le sue chiavi verranno utilizzate come insieme di chiavi specificato.
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
Verifica che object
abbia tutte le keys
fornite, ma può contenere anche chiavi aggiuntive non specificate. Puoi anche passare un singolo oggetto invece di un array di chiavi; in tal caso, le sue chiavi verranno utilizzate come insieme di chiavi specificato.
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
Verifica che object
non contenga nessuna delle keys
specificate. Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
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
Verifica che object
non contenga almeno una delle keys
specificate. Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
import { assert, test } from 'vitest';
test('assert.hasAnyKeys', () => {
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',
]);
});
hasAnyDeepKeys
- Tipo:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
Verifica che object
contenga almeno una delle keys
specificate. Poiché Set
e Map
possono avere oggetti come chiavi, è possibile utilizzare questa asserzione per eseguire un confronto approfondito (deep comparison). Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
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
Verifica che object
contenga tutte le keys
specificate e solo quelle. Poiché Set
e Map
possono avere oggetti come chiavi, è possibile utilizzare questa asserzione per eseguire un confronto approfondito (deep comparison). Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
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
Verifica che object
contenga tutte le keys
specificate. Poiché Set
e Map
possono avere oggetti come chiavi, è possibile utilizzare questa asserzione per eseguire un confronto approfondito (deep comparison). Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
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
Verifica che object
non contenga nessuna delle keys
specificate. Poiché Set
e Map
possono avere oggetti come chiavi, è possibile utilizzare questa asserzione per eseguire un confronto approfondito (deep comparison). Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
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
Verifica che object
non contenga almeno una delle keys
specificate. Poiché Set
e Map
possono avere oggetti come chiavi, è possibile utilizzare questa asserzione per eseguire un confronto approfondito (deep comparison). Invece di un array di chiavi, è possibile fornire un singolo oggetto; in tal caso, le sue chiavi saranno considerate come l'insieme di chiavi da verificare.
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 Errori, verifica che l'esecuzione di fn
generi un errore che sia un'istanza di errorLike
. Se errorLike
è un'istanza di Error
, verifica che l'errore generato sia la stessa istanza di errorLike
. Se viene fornito errMsgMatcher
, verifica inoltre che l'errore generato abbia un messaggio che corrisponda a errMsgMatcher
.
import { assert, test } from 'vitest';
test('assert.throws', () => {
assert.throws(fn, 'Error thrown must have this msg'); // L'errore generato deve avere questo messaggio
assert.throws(fn, /Error thrown must have a msg that matches this/); // L'errore generato deve avere un messaggio che corrisponda a questo
assert.throws(fn, ReferenceError);
assert.throws(fn, errorInstance);
assert.throws(
fn,
ReferenceError,
'Error thrown must be a ReferenceError and have this msg' // L'errore generato deve essere un ReferenceError e avere questo messaggio
);
assert.throws(
fn,
errorInstance,
'Error thrown must be the same errorInstance and have this msg' // L'errore generato deve essere la stessa errorInstance e avere questo messaggio
);
assert.throws(
fn,
ReferenceError,
/Error thrown must be a ReferenceError and match this/ // L'errore generato deve essere un ReferenceError e corrispondere a questo
);
assert.throws(
fn,
errorInstance,
/Error thrown must be the same errorInstance and match this/ // L'errore generato deve essere la stessa errorInstance 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 Errori, verifica che l'esecuzione di fn
non generi un errore che sia un'istanza di errorLike
. Se errorLike
è un'istanza di Error
, verifica che l'errore generato non sia la stessa istanza di errorLike
. Se viene fornito errMsgMatcher
, verifica inoltre che l'errore generato non abbia un messaggio che corrisponda a errMsgMatcher
.
import { assert, test } from 'vitest';
test('assert.doesNotThrow', () => {
assert.doesNotThrow(fn, 'Any Error thrown must not have this message'); // Qualsiasi errore generato non deve avere questo messaggio
assert.doesNotThrow(fn, /Any Error thrown must not match this/); // Qualsiasi errore generato non deve corrispondere a questo
assert.doesNotThrow(fn, Error);
assert.doesNotThrow(fn, errorInstance);
assert.doesNotThrow(fn, Error, 'Error must not have this message'); // L'errore non deve avere questo messaggio
assert.doesNotThrow(fn, errorInstance, 'Error must not have this message'); // L'errore non deve avere questo messaggio
assert.doesNotThrow(fn, Error, /Error must not match this/); // L'errore non deve corrispondere a questo
assert.doesNotThrow(fn, errorInstance, /Error must not match this/); // L'errore non deve corrispondere a questo
});
operator
- Tipo:
(val1: OperatorComparable, operator: Operator, val2: OperatorComparable, message?: string) => void
Confronta val1
e val2
utilizzando l'operatore specificato in operator
.
import { assert, test } from 'vitest';
test('assert.operator', () => {
assert.operator(1, '<', 2, 'everything is ok'); // tutto corretto
});
closeTo
- Tipo:
(actual: number, expected: number, delta: number, message?: string) => void
- Alias:
approximately
Verifica che actual
sia approssimativamente uguale a expected
, entro un intervallo di +/- delta
.
import { assert, test } from 'vitest';
test('assert.closeTo', () => {
assert.closeTo(1.5, 1, 0.5, 'numbers are close'); // i valori sono approssimativamente uguali
});
sameMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
contengano gli stessi elementi, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza stretta (===).
import { assert, test } from 'vitest';
test('assert.sameMembers', () => {
assert.sameMembers([1, 2, 3], [2, 1, 3], 'same members'); // gli stessi elementi
});
notSameMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
non contengano gli stessi elementi, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza stretta (===).
import { assert, test } from 'vitest';
test('assert.notSameMembers', () => {
assert.notSameMembers([1, 2, 3], [5, 1, 3], 'not same members'); // non gli stessi elementi
});
sameDeepMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
contengano gli stessi elementi, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza profonda (deep equality check).
import { assert, test } from 'vitest';
test('assert.sameDeepMembers', () => {
assert.sameDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'same deep members' // elementi identici anche in profondità
);
});
notSameDeepMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
non contengano gli stessi elementi, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza profonda (deep equality check).
import { assert, test } from 'vitest';
test('assert.notSameDeepMembers', () => {
assert.sameDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'same deep members' // elementi identici anche in profondità
);
});
sameOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
contengano gli stessi elementi nello stesso ordine. Utilizza un confronto di uguaglianza stretta (===).
import { assert, test } from 'vitest';
test('assert.sameOrderedMembers', () => {
assert.sameOrderedMembers([1, 2, 3], [1, 2, 3], 'same ordered members'); // stessa sequenza di elementi
});
notSameOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
non contengano gli stessi elementi nello stesso ordine. Utilizza un confronto di uguaglianza stretta (===).
import { assert, test } from 'vitest';
test('assert.notSameOrderedMembers', () => {
assert.notSameOrderedMembers(
[1, 2, 3],
[2, 1, 3],
'not same ordered members' // sequenza di elementi diversa
);
});
sameDeepOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
contengano gli stessi elementi nello stesso ordine. Utilizza un confronto di uguaglianza profonda (deep equality check).
import { assert, test } from 'vitest';
test('assert.sameDeepOrderedMembers', () => {
assert.sameDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { b: 2 }, { c: 3 }],
'same deep ordered members' // stessi membri profondi ordinati
);
});
notSameDeepOrderedMembers
- Tipo:
<T>(set1: T[], set2: T[], message?: string) => void
Verifica che set1
e set2
non contengano gli stessi elementi nello stesso ordine, utilizzando un confronto 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 }],
'not same deep ordered members'
);
assert.notSameDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'not same deep ordered members'
);
});
includeMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che tutti gli elementi di subset
siano presenti in superset
, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza stretta. I duplicati in subset
vengono ignorati.
import { assert, test } from 'vitest';
test('assert.includeMembers', () => {
assert.includeMembers([1, 2, 3], [2, 1, 2], 'include members');
});
notIncludeMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che non tutti gli elementi di subset
siano presenti in superset
, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza stretta. I duplicati in subset
vengono ignorati.
import { assert, test } from 'vitest';
test('assert.notIncludeMembers', () => {
assert.notIncludeMembers([1, 2, 3], [5, 1], 'not include members');
});
includeDeepMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che tutti gli elementi di subset
siano presenti in superset
, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza profonda. I duplicati in subset
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 deep members'
);
});
notIncludeDeepMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che non tutti gli elementi di subset
siano presenti in superset
, indipendentemente dall'ordine. Utilizza un confronto di uguaglianza profonda. I duplicati in subset
vengono ignorati.
import { assert, test } from 'vitest';
test('assert.notIncludeDeepMembers', () => {
assert.notIncludeDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { f: 5 }],
'not include deep members'
);
});
includeOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che subset
sia una sottosequenza di superset
, mantenendo lo stesso ordine e iniziando dal primo elemento di superset
. Utilizza un confronto di uguaglianza stretta.
import { assert, test } from 'vitest';
test('assert.includeOrderedMembers', () => {
assert.includeOrderedMembers([1, 2, 3], [1, 2], 'include ordered members');
});
notIncludeOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che subset
non sia una sottosequenza di superset
, mantenendo lo stesso ordine e iniziando dal primo elemento di superset
. Utilizza un confronto di uguaglianza stretta.
import { assert, test } from 'vitest';
test('assert.notIncludeOrderedMembers', () => {
assert.notIncludeOrderedMembers(
[1, 2, 3],
[2, 1],
'not include ordered members'
);
assert.notIncludeOrderedMembers(
[1, 2, 3],
[2, 3],
'not include ordered members'
);
});
includeDeepOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che subset
sia una sottosequenza di superset
, mantenendo lo stesso ordine e iniziando dal primo elemento di superset
. Utilizza un confronto di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.includeDeepOrderedMembers', () => {
assert.includeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { b: 2 }],
'include deep ordered members'
);
});
notIncludeDeepOrderedMembers
- Tipo:
<T>(superset: T[], subset: T[], message?: string) => void
Verifica che subset
non sia una sottosequenza di superset
, mantenendo lo stesso ordine e iniziando dal primo elemento di superset
. Utilizza un confronto di uguaglianza profonda.
import { assert, test } from 'vitest';
test('assert.includeDeepOrderedMembers', () => {
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { f: 5 }],
'not include deep ordered members'
);
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }],
'not include deep ordered members'
);
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { c: 3 }],
'not include deep ordered members'
);
});
oneOf
- Tipo:
<T>(inList: T, list: T[], message?: string) => void
Verifica che il valore primitivo inList
(non un oggetto o un array) sia presente nell'array list
.
import { assert, test } from 'vitest';
test('assert.oneOf', () => {
assert.oneOf(1, [2, 1], 'Not found in list');
});
changes
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Verifica che l'esecuzione della funzione modificatrice
modifichi il valore della proprietà
specificata dell'oggetto
.
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
Verifica che l'esecuzione di modifier
modifichi la property
dell'object
esattamente del valore 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
Verifica che l'esecuzione di modifier
non modifichi la property
dell'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
Verifica che l'esecuzione di modifier
modifichi la property
dell'object
, ma non esattamente del valore 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
Verifica che l'esecuzione di modifier
aumenti il valore numerico della proprietà
specificata dell'oggetto
.
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
Verifica che l'esecuzione di modifier
aumenti il valore numerico della property
dell'object
esattamente del valore change
.
import { assert, test } from 'vitest';
test('assert.increases', () => {
const obj = { val: 10 };
function fn() {
obj.val += 10;
}
assert.increases(fn, obj, 'val', 10);
});
doesNotIncrease
- Tipo:
<T>(modifier: Function, object: T, property: string, message?: string) => void
Verifica che l'esecuzione di modifier
non aumenti il valore numerico della property
dell'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
Verifica che l'esecuzione di modifier
aumenti il valore numerico della property
dell'object
, ma non esattamente del valore 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
Verifica che l'esecuzione di modifier
riduca il valore numerico della proprietà
specificata dell'oggetto
.
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
Verifica che l'esecuzione di modifier
diminuisca il valore numerico della property
dell'object
esattamente del valore 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
Verifica che l'esecuzione di modifier
non diminuisca il valore numerico della property
dell'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
Verifica che l'esecuzione di un modifier
non diminuisca il valore di una proprietà numerica di un object
o il suo valore di ritorno di un ammontare pari a 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
Verifica che l'esecuzione di un modifier
diminuisca il valore di una proprietà numerica di un object
o il suo valore di ritorno, ma non esattamente del valore change
specificato.
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
Verifica se object
è un valore falso. Se object
è un valore vero, viene lanciata un'eccezione. Questo metodo è stato aggiunto per consentire a Chai di sostituire direttamente la classe assert
di Node.js.
import { assert, test } from 'vitest';
test('assert.ifError', () => {
const err = new Error('I am a custom error');
assert.ifError(err); // Rilancia l'errore!
});
isExtensible
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
extensible
Verifica che object
sia estensibile (che sia possibile aggiungere nuove proprietà).
import { assert, test } from 'vitest';
test('assert.isExtensible', () => {
assert.isExtensible({});
});
isNotExtensible
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
notExtensible
Verifica che object
non sia estensibile (che non sia possibile aggiungere 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
Verifica che object
sia sigillato (che non sia possibile aggiungere nuove proprietà e che le proprietà esistenti non possano essere rimosse).
import { assert, test } from 'vitest';
test('assert.isSealed', () => {
const sealedObject = Object.seal({});
const frozenObject = Object.seal({});
assert.isSealed(sealedObject);
assert.isSealed(frozenObject);
});
isNotSealed
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
notSealed
Verifica che object
non sia sigillato (che sia possibile aggiungere nuove proprietà e che le proprietà esistenti possano essere rimosse).
import { assert, test } from 'vitest';
test('assert.isNotSealed', () => {
assert.isNotSealed({});
});
isFrozen
- Tipo:
<T>(object: T, message?: string) => void
- Alias:
frozen
Verifica che object
sia congelato (che non sia possibile aggiungere nuove proprietà e che le proprietà esistenti non possano 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
Verifica che object
non sia congelato (che sia possibile aggiungere nuove proprietà e che le proprietà esistenti possano essere modificate).
import { assert, test } from 'vitest';
test('assert.isNotFrozen', () => {
assert.isNotFrozen({});
});
isEmpty
- Tipo:
<T>(target: T, message?: string) => void
- Alias:
empty
Verifica che target
non contenga alcun valore. Per array e stringhe, controlla la proprietà length
. Per istanze di Map
e Set
, controlla la proprietà size
. Per gli oggetti che non sono funzioni, conta il numero di 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
Verifica che target
contenga dei valori. Per array e stringhe, controlla la proprietà length
. Per istanze di Map
e Set
, controlla la proprietà size
. Per gli oggetti che non sono funzioni, conta il numero di 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 });
});