assert(断言)
Vitest 从 chai
重新导出了 assert
方法,用于验证程序中的不变量。
assert
- 类型:
(expression: any, message?: string) => asserts expression
断言给定的 expression
为真,否则断言失败。
import { assert, test } from 'vitest';
test('assert', () => {
assert('foo' !== 'bar', 'foo 应该不等于 bar');
});
fail
- 类型:
(message?: string) => never
<T>(actual: T, expected: T, message?: string, operator?: string) => never
强制断言失败。
import { assert, test } from 'vitest';
test('assert.fail', () => {
assert.fail('失败时的错误消息');
assert.fail('foo', 'bar', 'foo 不等于 bar', '===');
});
isOk
- 类型:
<T>(value: T, message?: string) => void
- 别名:
ok
断言给定的 value
为真值(truthy)。
import { assert, test } from 'vitest';
test('assert.isOk', () => {
assert.isOk('foo', '所有真值都是有效的');
assert.isOk(false, '这会失败,因为 false 不是有效的');
});
isNotOk
- 类型:
<T>(value: T, message?: string) => void
- 别名:
notOk
断言给定的 value
为假值(falsy)。
import { assert, test } from 'vitest';
test('assert.isNotOk', () => {
assert.isNotOk('foo', '这个会失败,所有真值都不是假值');
assert.isNotOk(false, '这会通过,因为 false 是假值');
});
equal
- 类型:
<T>(actual: T, expected: T, message?: string) => void
断言 actual
和 expected
的非严格相等 (==)。
import { assert, test } from 'vitest';
test('assert.equal', () => {
assert.equal(Math.sqrt(4), '2');
});
notEqual
- 类型:
<T>(actual: T, expected: T, message?: string) => void
断言 actual
和 expected
的非严格不等 (!=)。
import { assert, test } from 'vitest';
test('assert.notEqual', () => {
assert.notEqual(Math.sqrt(4), 3);
});
strictEqual
- 类型:
<T>(actual: T, expected: T, message?: string) => void
断言 actual
和 expected
的严格相等 (===)。
import { assert, test } from 'vitest';
test('assert.strictEqual', () => {
assert.strictEqual(Math.sqrt(4), 2);
});
deepEqual
- 类型:
<T>(actual: T, expected: T, message?: string) => void
断言 actual
深度等于 expected
。
import { assert, test } from 'vitest';
test('assert.deepEqual', () => {
assert.deepEqual({ color: 'green' }, { color: 'green' });
});
notDeepEqual
- 类型:
<T>(actual: T, expected: T, message?: string) => void
断言 actual
深度不等于 expected
。
import { assert, test } from 'vitest';
test('assert.notDeepEqual', () => {
assert.notDeepEqual({ color: 'green' }, { color: 'red' });
});
isAbove
- 类型:
(valueToCheck: number, valueToBeAbove: number, message?: string) => void
断言 valueToCheck
大于 valueToBeAbove
。
import { assert, test } from 'vitest';
test('assert.isAbove', () => {
assert.isAbove(5, 2, '5 大于 2');
});
isAtLeast
- 类型:
(valueToCheck: number, valueToBeAtLeast: number, message?: string) => void
断言 valueToCheck
大于或等于 valueToBeAtLeast
。
import { assert, test } from 'vitest';
test('assert.isAtLeast', () => {
assert.isAtLeast(5, 2, '5 大于或等于 2');
assert.isAtLeast(3, 3, '3 大于或等于 3');
});
isBelow
- 类型:
(valueToCheck: number, valueToBeBelow: number, message?: string) => void
断言 valueToCheck
小于 valueToBeBelow
。
import { assert, test } from 'vitest';
test('assert.isBelow', () => {
assert.isBelow(3, 6, '3 小于 6');
});
isAtMost
- 类型:
(valueToCheck: number, valueToBeAtMost: number, message?: string) => void
断言 valueToCheck
小于或等于 valueToBeAtMost
。
import { assert, test } from 'vitest';
test('assert.isAtMost', () => {
assert.isAtMost(3, 6, '3 小于或等于 6');
assert.isAtMost(4, 4, '4 小于或等于 4');
});
isTrue
- 类型:
<T>(value: T, message?: string) => void
断言 value
是 true
。
import { assert, test } from 'vitest';
const testPassed = true;
test('assert.isTrue', () => {
assert.isTrue(testPassed);
});
isNotTrue
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是 true
。
import { assert, test } from 'vitest';
const testPassed = 'ok';
test('assert.isNotTrue', () => {
assert.isNotTrue(testPassed);
});
isFalse
- 类型:
<T>(value: T, message?: string) => void
断言 value
是 false
。
import { assert, test } from 'vitest';
const testPassed = false;
test('assert.isFalse', () => {
assert.isFalse(testPassed);
});
isNotFalse
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是 false
。
import { assert, test } from 'vitest';
const testPassed = 'no';
test('assert.isNotFalse', () => {
assert.isNotFalse(testPassed);
});
isNull
- 类型:
<T>(value: T, message?: string) => void
断言 value
是 null
。
import { assert, test } from 'vitest';
const error = null;
test('assert.isNull', () => {
assert.isNull(error, 'error 为 null');
});
isNotNull
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是 null
。
import { assert, test } from 'vitest';
const error = { message: 'error was occured' };
test('assert.isNotNull', () => {
assert.isNotNull(error, 'error 不为 null,而是一个对象');
});
isNaN
- 类型:
<T>(value: T, message?: string) => void
断言 value
是 NaN
。
import { assert, test } from 'vitest';
const calculation = 1 * 'viitest';
test('assert.isNaN', () => {
assert.isNaN(calculation, '1 * "vitest" 结果是 NaN');
});
isNotNaN
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是 NaN
。
import { assert, test } from 'vitest';
const calculation = 1 * 2;
test('assert.isNotNaN', () => {
assert.isNotNaN(calculation, '1 * 2 不为 NaN,而是 2');
});
exists
- 类型:
<T>(value: T, message?: string) => void
断言 value
既不是 null
也不是 undefined
。
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.exists', () => {
assert.exists(name, 'foo 既不是 null 也不是 undefined');
});
notExists
- 类型:
<T>(value: T, message?: string) => void
断言 value
是 null
或 undefined
。
import { assert, test } from 'vitest';
const foo = null;
const bar = undefined;
test('assert.notExists', () => {
assert.notExists(foo, 'foo 是 null,因此不存在');
assert.notExists(bar, 'bar 是 undefined,因此不存在');
});
isUndefined
- 类型:
<T>(value: T, message?: string) => void
断言 value
是 undefined
。
import { assert, test } from 'vitest';
const name = undefined;
test('assert.isUndefined', () => {
assert.isUndefined(name, 'name 是 undefined');
});
isDefined
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是 undefined
。
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.isDefined', () => {
assert.isDefined(name, 'name 不是 undefined');
});
isFunction
- 类型:
<T>(value: T, message?: string) => void
- 别名:
isCallable
断言value
是函数。
import { assert, test } from 'vitest';
function name() {
return 'foo';
}
test('assert.isFunction', () => {
assert.isFunction(name, 'name 是函数');
});
isNotFunction
- 类型:
<T>(value: T, message?: string) => void
- 别名:
isNotCallable
断言 value
不是函数。
import { assert, test } from 'vitest';
const name = 'foo';
test('assert.isNotFunction', () => {
assert.isNotFunction(name, 'name 不是函数,而是字符串');
});
isObject
- 类型:
<T>(value: T, message?: string) => void
断言 value
是一个 Object 类型的对象(由 Object.prototype.toString
揭示)。该断言不适用于子类对象。
import { assert, test } from 'vitest';
const someThing = { color: 'red', shape: 'circle' };
test('assert.isObject', () => {
assert.isObject(someThing, 'someThing 是对象');
});
isNotObject
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是一个对象(通过 Object.prototype.toString
方法判断)。此断言不适用于子类对象。
import { assert, test } from 'vitest';
const someThing = 'redCircle';
test('assert.isNotObject', () => {
assert.isNotObject(someThing, 'someThing 不是对象,而是一个字符串');
});
isArray
- 类型:
<T>(value: T, message?: string) => void
断言 value
是一个数组。
import { assert, test } from 'vitest';
const color = ['red', 'green', 'yellow'];
test('assert.isArray', () => {
assert.isArray(color, 'color 是一个数组');
});
isNotArray
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是一个数组。
import { assert, test } from 'vitest';
const color = 'red';
test('assert.isNotArray', () => {
assert.isNotArray(color, 'color 不是数组,而是一个字符串');
});
isString
- 类型:
<T>(value: T, message?: string) => void
断言 value
是一个字符串。
import { assert, test } from 'vitest';
const color = 'red';
test('assert.isString', () => {
assert.isString(color, 'color 是字符串');
});
isNotString
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是一个字符串。
import { assert, test } from 'vitest';
const color = ['red', 'green', 'yellow'];
test('assert.isNotString', () => {
assert.isNotString(color, 'color 不是字符串,而是一个数组');
});
isNumber
- 类型:
<T>(value: T, message?: string) => void
断言 value
是一个数字。
import { assert, test } from 'vitest';
const colors = 3;
test('assert.isNumber', () => {
assert.isNumber(colors, 'colors 是数字');
});
isNotNumber
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是一个数字。
import { assert, test } from 'vitest';
const colors = '3 colors';
test('assert.isNotNumber', () => {
assert.isNotNumber(colors, 'colors 不是数字,而是一个字符串');
});
isFinite
- 类型:
<T>(value: T, message?: string) => void
断言 value
是一个有限数字(即不是 NaN
或 Infinity
)。
import { assert, test } from 'vitest';
const colors = 3;
test('assert.isFinite', () => {
assert.isFinite(colors, 'colors 是一个数字,不是 NaN 或 Infinity');
});
isBoolean
- 类型:
<T>(value: T, message?: string) => void
断言 value
是一个布尔值。
import { assert, test } from 'vitest';
const isReady = true;
test('assert.isBoolean', () => {
assert.isBoolean(isReady, 'isReady 是布尔值');
});
isNotBoolean
- 类型:
<T>(value: T, message?: string) => void
断言 value
不是一个布尔值。
import { assert, test } from 'vitest';
const isReady = 'sure';
test('assert.isNotBoolean', () => {
assert.isNotBoolean(isReady, 'isReady 不是一个布尔值,而是字符串');
});
typeOf
- 类型:
<T>(value: T, name: string, message?: string) => void
断言 value
的类型是 name
,类型由 Object.prototype.toString
确定。
import { assert, test } from 'vitest';
test('assert.typeOf', () => {
assert.typeOf({ color: 'red' }, 'object', '我们有一个对象');
assert.typeOf(['red', 'green'], 'array', '我们有一个数组');
assert.typeOf('red', 'string', '我们有一个字符串');
assert.typeOf(/red/, 'regexp', '我们有一个正则表达式');
assert.typeOf(null, 'null', '我们有一个 null');
assert.typeOf(undefined, 'undefined', '我们有一个 undefined');
});
notTypeOf
- 类型:
<T>(value: T, name: string, message?: string) => void
断言 value
的类型不是 name
,类型由 Object.prototype.toString
确定。
import { assert, test } from 'vitest';
test('assert.notTypeOf', () => {
assert.notTypeOf('red', 'number', '"red" 不是数字');
});
instanceOf
- 类型:
<T>(value: T, constructor: Function, message?: string) => void
断言 value
是 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 是 Person 的一个实例');
assert.instanceOf(coffee, Tea, 'coffee 是 Tea 的一个实例');
});
notInstanceOf
- 类型:
<T>(value: T, constructor: Function, message?: string) => void
断言 value
不是 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 不是 Tea 的实例');
});
include
- 类型:
(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
断言 haystack
包含 needle
。 可用于断言数组包含某个值,字符串包含一个子字符串,或者对象包含一组属性。
import { assert, test } from 'vitest';
test('assert.include', () => {
assert.include([1, 2, 3], 2, '数组中包含该值');
assert.include('foobar', 'foo', '字符串包含该子字符串');
assert.include(
{ foo: 'bar', hello: 'universe' },
{ foo: 'bar' },
'对象包含该属性'
);
});
notInclude
- 类型:
(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
断言 haystack
不包含 needle
。 可用于断言数组不包含某个值,字符串不包含一个子字符串,或者对象不包含一组属性。
import { assert, test } from 'vitest';
test('assert.notInclude', () => {
assert.notInclude([1, 2, 3], 4, '数组中不包含 4');
assert.notInclude('foobar', 'baz', 'foobar 不包含 baz');
assert.notInclude(
{ foo: 'bar', hello: 'universe' },
{ foo: 'baz' },
'对象不包含该属性'
);
});
deepInclude
- 类型:
(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
断言 haystack
包含 needle
。 可用于断言数组包含某个值,或者对象包含一组属性。 使用深度比较。
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
- 类型:
(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
断言 haystack
不包含 needle
。 可用于断言数组不包含某个值,或者对象不包含一组属性。 使用深度比较。
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
- 类型:
(haystack: any, needle: any, message?: string) => void
断言 haystack
包含 needle
。 可用于断言对象包含某些属性。 允许使用点号和方括号表示法来引用嵌套属性。 属性名称中的 '[]'
和 '.'
可以使用双反斜杠进行转义。
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
- 类型:
(haystack: any, needle: any, message?: string) => void
断言 haystack
不包含 needle
。 可用于断言对象不包含某些属性。 允许使用点号和方括号表示法来引用嵌套属性。 属性名称中的 '[]'
和 '.'
可以使用双反斜杠进行转义。
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
- 类型:
(haystack: any, needle: any, message?: string) => void
断言 haystack
包含 needle
。 可以用来断言对象中包含一组属性,并进行深度相等性检查。 允许使用点号和方括号表示法来引用嵌套属性。 属性名称中的 '[]'
和 '.'
可以使用双反斜杠进行转义。
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
- 类型:
(haystack: any, needle: any, message?: string) => void
断言 haystack
不包含 needle
。 可以用来断言对象中缺少一组属性,并进行深度相等性检查。 允许使用点号和方括号表示法来引用嵌套属性。 属性名称中的 '[]'
和 '.'
可以使用双反斜杠进行转义。
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
- 类型:
(haystack: any, needle: any, message?: string) => void
断言 haystack
包含 needle
。 可以用来断言对象自身包含一组属性,忽略继承的属性。
import { assert, test } from 'vitest';
test('assert.ownInclude', () => {
assert.ownInclude({ a: 1 }, { a: 1 });
});
notOwnInclude
- 类型:
(haystack: any, needle: any, message?: string) => void
断言 haystack
不包含 needle
。 可以用来断言对象自身不包含一组属性,忽略继承的属性。
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
- 类型:
(haystack: any, needle: any, message?: string) => void
断言对象 haystack
包含具有特定属性值的子集 needle
,忽略继承的属性并进行深度相等性检查。
import { assert, test } from 'vitest';
test('assert.deepOwnInclude', () => {
assert.deepOwnInclude({ a: { b: 2 } }, { a: { b: 2 } });
});
notDeepOwnInclude
- 类型:
(haystack: any, needle: any, message?: string) => void
断言对象 haystack
不包含具有特定属性值的子集 needle
,忽略继承的属性并进行深度相等性检查。
import { assert, test } from 'vitest';
test('assert.notDeepOwnInclude', () => {
assert.notDeepOwnInclude({ a: { b: 2 } }, { a: { c: 3 } });
});
match
- 类型:
(value: string, regexp: RegExp, message?: string) => void
断言字符串 value
匹配正则表达式 regexp
。
import { assert, test } from 'vitest';
test('assert.match', () => {
assert.match('foobar', /^foo/, 'regexp matches');
});
notMatch
- 类型:
(value: string, regexp: RegExp, message?: string) => void
断言字符串 value
不匹配正则表达式 regexp
。
import { assert, test } from 'vitest';
test('assert.notMatch', () => {
assert.notMatch('foobar', /^foo/, 'regexp does not match');
});
property
- 类型:
<T>(object: T, property: string, message?: string) => void
断言对象 object
包含名为 property
的属性(直接属性或继承属性)。
import { assert, test } from 'vitest';
test('assert.property', () => {
assert.property({ tea: { green: 'matcha' } }, 'tea');
assert.property({ tea: { green: 'matcha' } }, 'toString');
});
notProperty
- 类型:
<T>(object: T, property: string, message?: string) => void
断言对象 object
不包含名为 property
的属性(直接属性或继承属性)。
import { assert, test } from 'vitest';
test('assert.notProperty', () => {
assert.notProperty({ tea: { green: 'matcha' } }, 'coffee');
});
propertyVal
- 类型:
<T, V>(object: T, property: string, value: V, message?: string) => void
断言对象 object
拥有名为 property
的属性(直接属性或继承属性),且该属性的值与 value
严格相等 (===)。
import { assert, test } from 'vitest';
test('assert.notPropertyVal', () => {
assert.propertyVal({ tea: 'is good' }, 'tea', 'is good');
});
notPropertyVal
- 类型:
<T, V>(object: T, property: string, value: V, message?: string) => void
断言对象 object
不拥有名为 property
的属性(直接属性或继承属性),或者该属性的值与 value
不严格相等 (===)。
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
- 类型:
<T, V>(object: T, property: string, value: V, message?: string) => void
断言对象 object
拥有名为 property
的属性(直接属性或继承属性),且该属性的值与 value
深度相等。
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
assert.deepPropertyVal({ tea: { green: 'matcha' } }, 'tea', {
green: 'matcha',
});
});
notDeepPropertyVal
- 类型:
<T, V>(object: T, property: string, value: V, message?: string) => void
断言对象 object
不拥有名为 property
的属性(直接属性或继承属性),或者该属性的值与 value
不深度相等。
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
- 类型:
<T>(object: T, property: string, message?: string) => void
断言对象 object
拥有名为 property
的属性(直接属性或继承属性),property
可以使用点号和方括号表示法进行嵌套引用。
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
assert.nestedProperty({ tea: { green: 'matcha' } }, 'tea.green');
});
notNestedProperty
- 类型:
<T>(object: T, property: string, message?: string) => void
断言对象 object
不拥有名为 property
的属性(直接属性或继承属性),property
可以使用点号和方括号表示法进行嵌套引用。
import { assert, test } from 'vitest';
test('assert.deepPropertyVal', () => {
assert.notNestedProperty({ tea: { green: 'matcha' } }, 'tea.oolong');
});
nestedPropertyVal
- 类型:
<T>(object: T, property: string, value: any, message?: string) => void
断言对象 object
拥有名为 property
的属性,且该属性的值与 value
严格相等 (===)。property
可以使用点号和方括号表示法进行嵌套引用。
import { assert, test } from 'vitest';
test('assert.nestedPropertyVal', () => {
assert.nestedPropertyVal({ tea: { green: 'matcha' } }, 'tea.green', 'matcha');
});
notNestedPropertyVal
- 类型:
<T>(object: T, property: string, value: any, message?: string) => void
断言对象 object
不拥有名为 property
的属性,或者该属性的值与 value
不严格相等 (===)。property
可以使用点号和方括号表示法进行嵌套引用。
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
- 类型:
<T>(object: T, property: string, value: any, message?: string) => void
断言对象 object
拥有名为 property
的属性,且该属性的值与 value
深度相等。property
可以使用点号和方括号表示法进行嵌套引用。
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
- 类型:
<T>(object: T, property: string, value: any, message?: string) => void
断言对象 object
不拥有名为 property
的属性,或者该属性的值与 value
不深度相等。property
可以使用点号和方括号表示法进行嵌套引用。
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
- 类型:
<T extends { readonly length?: number | undefined } | { readonly size?: number | undefined }>(object: T, length: number, message?: string) => void
断言对象 object
的 length
或 size
属性的值等于 length
。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言对象 object
至少包含 keys
中指定的一个键。 keys
可以是键的数组,也可以是一个对象,该对象的键会被视为预期的键集合。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言对象 object
包含且仅包含 keys
中指定的所有键。 keys
可以是键的数组,也可以是一个对象,该对象的键会被视为预期的键集合。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言对象 object
包含 keys
中指定的所有键,但可能包含更多未指定的键。 keys
可以是键的数组,也可以是一个对象,该对象的键会被视为预期的键集合。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
不包含提供的任何键。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
并非包含提供的所有 keys
(键)。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
包含提供的 keys
(键)中的至少一个。 由于 Set 和 Map 支持对象作为键,你可以使用此断言来执行深度比较。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
必须包含提供的所有 keys
(键)。 由于 Set 和 Map 支持对象作为键,你可以使用此断言来执行深度比较。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
包含提供的所有 keys
(键)。 由于 Set 和 Map 支持对象作为键,你可以使用此断言来执行深度比较。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
不包含提供的任何键。 由于 Set 和 Map 支持对象作为键,你可以使用此断言来执行深度比较。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
断言 object
不包含提供的所有 keys
(键)。 由于 Set 和 Map 支持对象作为键,你可以使用此断言来执行深度比较。 可以直接提供一个对象,其键将被用作期望的键集合,而不是提供键数组。
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
- 类型:
(fn: () => void, errMsgMatcher?: RegExp | string, ignored?: any, message?: string) => void
(fn: () => void, errorLike?: ErrorConstructor | Error | null, errMsgMatcher?: RegExp | string | null, message?: string) => void
- 别名:
throw
Throw
如果 errorLike
是一个 Error 构造函数,则断言 fn
将抛出一个错误,且该错误是 errorLike
的实例。如果 errorLike
是一个 Error 实例,则断言抛出的错误与 errorLike
是同一个实例。如果提供了 errMsgMatcher
,它还会断言抛出的错误的消息与 errMsgMatcher
匹配。
import { assert, test } from 'vitest';
test('assert.throws', () => {
assert.throws(fn, 'Error thrown must have this msg');
assert.throws(fn, /Error thrown must have a msg that matches this/);
assert.throws(fn, ReferenceError);
assert.throws(fn, errorInstance);
assert.throws(
fn,
ReferenceError,
'Error thrown must be a ReferenceError and have this msg'
);
assert.throws(
fn,
errorInstance,
'Error thrown must be the same errorInstance and have this msg'
);
assert.throws(
fn,
ReferenceError,
/Error thrown must be a ReferenceError and match this/
);
assert.throws(
fn,
errorInstance,
/Error thrown must be the same errorInstance and match this/
);
});
doesNotThrow
- 类型:
(fn: () => void, errMsgMatcher?: RegExp | string, ignored?: any, message?: string) => void
- 类型:
(fn: () => void, errorLike?: ErrorConstructor | Error | null, errMsgMatcher?: RegExp | string | null, message?: string) => void
如果 errorLike
是一个 Error 构造函数,则断言 fn
不会抛出一个错误,且该错误是 errorLike
的实例。如果 errorLike
是一个 Error 实例,则断言抛出的错误与 errorLike
不是同一个实例。如果提供了 errMsgMatcher
,它还会断言抛出的错误的消息与 errMsgMatcher
不匹配。
import { assert, test } from 'vitest';
test('assert.doesNotThrow', () => {
assert.doesNotThrow(fn, 'Any Error thrown must not have this message');
assert.doesNotThrow(fn, /Any Error thrown must not match this/);
assert.doesNotThrow(fn, Error);
assert.doesNotThrow(fn, errorInstance);
assert.doesNotThrow(fn, Error, 'Error must not have this message');
assert.doesNotThrow(fn, errorInstance, 'Error must not have this message');
assert.doesNotThrow(fn, Error, /Error must not match this/);
assert.doesNotThrow(fn, errorInstance, /Error must not match this/);
});
operator
- 类型:
(val1: OperatorComparable, operator: Operator, val2: OperatorComparable, message?: string) => void
使用 operator
比较 val1
和 val2
。
import { assert, test } from 'vitest';
test('assert.operator', () => {
assert.operator(1, '<', 2, 'everything is ok');
});
closeTo
- 类型:
(actual: number, expected: number, delta: number, message?: string) => void
- 别名:
approximately
断言 actual
等于 expected
,且在 expected
+/- delta
的范围内。
import { assert, test } from 'vitest';
test('assert.closeTo', () => {
assert.closeTo(1.5, 1, 0.5, 'numbers are close');
});
sameMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
具有相同的成员,顺序不限。 使用严格相等(===)进行检查。
import { assert, test } from 'vitest';
test('assert.sameMembers', () => {
assert.sameMembers([1, 2, 3], [2, 1, 3], 'same members');
});
notSameMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
不具有相同的成员,顺序不限。 使用严格相等(===)进行检查。
import { assert, test } from 'vitest';
test('assert.sameMembers', () => {
assert.notSameMembers([1, 2, 3], [5, 1, 3], 'not same members');
});
sameDeepMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
具有相同的成员,顺序不限,使用深度相等性进行检查。
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'
);
});
notSameDeepMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
不具有相同的成员,顺序不限,使用深度相等性进行检查。
import { assert, test } from 'vitest';
test('assert.notSameDeepMembers', () => {
assert.notSameDeepMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'same deep members'
);
});
sameOrderedMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
具有相同的成员,顺序相同,使用严格相等(===)进行检查。
import { assert, test } from 'vitest';
test('assert.sameOrderedMembers', () => {
assert.sameOrderedMembers([1, 2, 3], [1, 2, 3], 'same ordered members');
});
notSameOrderedMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
不具有相同的成员,顺序相同,使用严格相等(===)进行检查。
import { assert, test } from 'vitest';
test('assert.notSameOrderedMembers', () => {
assert.notSameOrderedMembers(
[1, 2, 3],
[2, 1, 3],
'not same ordered members'
);
});
sameDeepOrderedMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
具有相同的成员,顺序相同,使用深度相等性进行检查。
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'
);
});
notSameDeepOrderedMembers
- 类型:
<T>(set1: T[], set2: T[], message?: string) => void
断言 set1
和 set2
不包含相同顺序的相同成员,使用深度相等性检查。
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
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
是 superset
的子集(忽略顺序)。使用严格相等性检查 (===)。重复项将被忽略。
import { assert, test } from 'vitest';
test('assert.includeMembers', () => {
assert.includeMembers([1, 2, 3], [2, 1, 2], 'include members');
});
notIncludeMembers
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
不是 superset
的子集(忽略顺序)。使用严格相等性检查 (===)。重复项将被忽略。
import { assert, test } from 'vitest';
test('assert.notIncludeMembers', () => {
assert.notIncludeMembers([1, 2, 3], [5, 1], 'not include members');
});
includeDeepMembers
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
是 superset
的子集(忽略顺序)。使用深度相等性检查。重复项将被忽略。
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
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
不是 superset
的子集(忽略顺序)。使用深度相等性检查。重复项将被忽略。
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
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
以相同的顺序包含在 superset
中,且从 superset
的第一个元素开始匹配。使用严格相等性检查 (===)。
import { assert, test } from 'vitest';
test('assert.includeOrderedMembers', () => {
assert.includeOrderedMembers([1, 2, 3], [1, 2], 'include ordered members');
});
notIncludeOrderedMembers
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
没有以相同的顺序包含在 superset
中,或不是从 superset
的第一个元素开始匹配。使用严格相等性检查 (===)。
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
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
以相同的顺序包含在 superset
中,且从 superset
的第一个元素开始匹配。使用深度相等性检查。
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
- 类型:
<T>(superset: T[], subset: T[], message?: string) => void
断言 subset
没有以相同的顺序包含在 superset
中,或不是从 superset
的第一个元素开始匹配。使用深度相等性检查。
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
- 类型:
<T>(inList: T, list: T[], message?: string) => void
断言 inList
(非对象、非数组值) 出现在扁平数组 list
中。
import { assert, test } from 'vitest';
test('assert.oneOf', () => {
assert.oneOf(1, [2, 1], 'Not found in list');
});
changes
- 类型:
<T>(modifier: Function, object: T, property: string, message?: string) => void
断言执行 modifier
函数后,object
对象的 property
属性值发生了改变。
import { assert, test } from 'vitest';
test('assert.changes', () => {
const obj = { val: 10 };
function fn() {
obj.val = 22;
}
assert.changes(fn, obj, 'val');
});
changesBy
- 类型:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
断言执行 modifier
函数后,object
对象的 property
属性值改变了 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
- 类型:
<T>(modifier: Function, object: T, property: string, message?: string) => void
断言执行 modifier
函数后,object
对象的 property
属性值没有发生改变。
import { assert, test } from 'vitest';
test('assert.doesNotChange', () => {
const obj = { val: 10 };
function fn() {
obj.val += 2;
}
assert.doesNotChange(fn, obj, 'val', 2);
});
changesButNotBy
- 类型:
<T>(modifier: Function, object: T, property: string, change:number, message?: string) => void
断言执行 modifier
函数后,object
对象的 property
属性值的改变不等于 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
- 类型:
<T>(modifier: Function, object: T, property: string, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值增加了。
import { assert, test } from 'vitest';
test('assert.increases', () => {
const obj = { val: 10 };
function fn() {
obj.val = 13;
}
assert.increases(fn, obj, 'val');
});
increasesBy
- 类型:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值增加了 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
- 类型:
<T>(modifier: Function, object: T, property: string, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值没有增加。
import { assert, test } from 'vitest';
test('assert.doesNotIncrease', () => {
const obj = { val: 10 };
function fn() {
obj.val = 8;
}
assert.doesNotIncrease(fn, obj, 'val');
});
increasesButNotBy
- 类型:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值的增加不等于 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
- 类型:
<T>(modifier: Function, object: T, property: string, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值减少了。
import { assert, test } from 'vitest';
test('assert.decreases', () => {
const obj = { val: 10 };
function fn() {
obj.val = 5;
}
assert.decreases(fn, obj, 'val');
});
decreasesBy
- 类型:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值减少了 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
- 类型:
<T>(modifier: Function, object: T, property: string, message?: string) => void
断言执行 modifier
函数后,数字类型的 object
对象的 property
属性值没有减少。
import { assert, test } from 'vitest';
test('assert.doesNotDecrease', () => {
const obj = { val: 10 };
function fn() {
obj.val = 15;
}
assert.doesNotDecrease(fn, obj, 'val');
});
doesNotDecreaseBy
- 类型:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
断言执行 modifier
后,数值型 object
的 property
属性值或 modifier
的返回值,不会减少 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
- 类型:
<T>(modifier: Function, object: T, property: string, change: number, message?: string) => void
断言执行 modifier
后,数值型 object
的 property
属性值或 modifier
的返回值,会减少但不会减少 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
- 类型:
<T>(object: T, message?: string) => void
断言 object
是假值,如果 object
是真值则抛出错误。添加此方法是为了让 chai 能够直接替代 Node 的 assert 类。
import { assert, test } from 'vitest';
test('assert.ifError', () => {
const err = new Error('I am a custom error');
assert.ifError(err); // 抛出 err!
});
isExtensible
- 类型:
<T>(object: T, message?: string) => void
- 别名:
extensible
断言 object
是可扩展的(可以添加新属性)。
import { assert, test } from 'vitest';
test('assert.isExtensible', () => {
assert.isExtensible({});
});
isNotExtensible
- 类型:
<T>(object: T, message?: string) => void
- 别名:
notExtensible
断言 object
不可扩展(不能添加新属性)。
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
- 类型:
<T>(object: T, message?: string) => void
- 别名:
sealed
断言 object
已密封(不能添加新属性,且现有属性不能被删除)。
import { assert, test } from 'vitest';
test('assert.isSealed', () => {
const sealedObject = Object.seal({});
const frozenObject = Object.seal({});
assert.isSealed(sealedObject);
assert.isSealed(frozenObject);
});
isNotSealed
- 类型:
<T>(object: T, message?: string) => void
- 别名:
notSealed
断言 object
未密封(可以添加新属性,且现有属性可以被删除)。
import { assert, test } from 'vitest';
test('assert.isNotSealed', () => {
assert.isNotSealed({});
});
isFrozen
- 类型:
<T>(object: T, message?: string) => void
- 别名:
frozen
断言 object
已冻结(不能添加新属性,且现有属性不能被修改)。
import { assert, test } from 'vitest';
test('assert.isFrozen', () => {
const frozenObject = Object.freeze({});
assert.frozen(frozenObject);
});
isNotFrozen
- 类型:
<T>(object: T, message?: string) => void
- 别名:
notFrozen
断言 object
未冻结(可以添加新属性,且现有属性可以被修改)。
import { assert, test } from 'vitest';
test('assert.isNotFrozen', () => {
assert.isNotFrozen({});
});
isEmpty
- 类型:
<T>(target: T, message?: string) => void
- 别名:
empty
断言 target
为空。对于数组和字符串,它检查 length
属性。对于 Map 和 Set 实例,它检查 size
属性。对于非函数对象,它获取自身可枚举字符串键的数量。
import { assert, test } from 'vitest';
test('assert.isEmpty', () => {
assert.isEmpty([]);
assert.isEmpty('');
assert.isEmpty(new Map());
assert.isEmpty({});
});
isNotEmpty
- 类型:
<T>(object: T, message?: string) => void
- 别名:
notEmpty
断言 target
包含值。对于数组和字符串,它检查 length
属性。对于 Map 和 Set 实例,它检查 size
属性。对于非函数对象,它获取自身可枚举字符串键的数量。
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 });
});