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
為真值。
import { assert, test } from 'vitest';
test('assert.isOk', () => {
assert.isOk('foo', '任何真值都是有效的');
assert.isOk(false, '這會失敗,因為 false 不是真值');
});
isNotOk
- 類型:
<T>(value: T, message?: string) => void
- 別名
notOk
斷言給定的 value
為假值。
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, '錯誤是 null');
});
isNotNull
- 類型:
<T>(value: T, message?: string) => void
斷言 value
不是 null
。
import { assert, test } from 'vitest';
const error = { message: 'error was occurred' };
test('assert.isNotNull', () => {
assert.isNotNull(error, '錯誤不是 null,而是物件');
});
isNaN
- 類型:
<T>(value: T, message?: string) => void
斷言 value
是 NaN
。
import { assert, test } from 'vitest';
const calculation = 1 * 'vitest';
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
類型的物件(由 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/, '正則表達式匹配');
});
notMatch
- 類型:
(value: string, regexp: RegExp, message?: string) => void
斷言 value
不匹配正則表達式 regexp
。
import { assert, test } from 'vitest';
test('assert.notMatch', () => {
assert.notMatch('foobar', /^foo/, '正則表達式不匹配');
});
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.propertyVal', () => {
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.notDeepPropertyVal', () => {
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', {
black: 'matcha',
});
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', {
green: 'oolong',
});
assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'coffee', {
green: 'matcha',
});
});
nestedProperty
- 類型:
<T>(object: T, property: string, message?: string) => void
斷言 object
具有由 property
命名的直接或繼承屬性,property
可以是使用點和方括號表示法進行巢狀引用的字串。
import { assert, test } from 'vitest';
test('assert.nestedProperty', () => {
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.notNestedProperty', () => {
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.deepNestedPropertyVal', () => {
assert.deepNestedPropertyVal(
{ tea: { green: 'matcha' } },
'tea.green',
'konacha'
);
assert.deepNestedPropertyVal(
{ 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
。
import { assert, test } from 'vitest';
test('assert.lengthOf', () => {
assert.lengthOf([1, 2, 3], 3, '陣列長度為 3');
assert.lengthOf('foobar', 6, '字串長度為 6');
assert.lengthOf(new Set([1, 2, 3]), 3, 'set 大小為 3');
assert.lengthOf(
new Map([
['a', 1],
['b', 2],
['c', 3],
]),
3,
'map 大小為 3'
);
});
hasAnyKeys
- 類型:
<T>(object: T, keys: Array<Object | string> | { [key: string]: any }, message?: string) => void
斷言 object
至少具有所提供的 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
,且僅有這些鍵。您也可以提供單一物件而不是鍵陣列,其鍵將用作預期的鍵集。
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
,但可能還有未列出的其他鍵。您也可以提供單一物件而不是鍵陣列,其鍵將用作預期的鍵集。
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
不具有所提供的任何 keys
。您也可以提供單一物件而不是鍵陣列,其鍵將用作預期的鍵集。
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.doesNotHaveAllKeys', () => {
assert.doesNotHaveAllKeys({ foo: 1, bar: 2, baz: 3 }, [
'one',
'two',
'example',
]);
assert.doesNotHaveAllKeys(
{ foo: 1, bar: 2, baz: 3 },
{ one: 1, two: 2, example: 'foo' }
);
assert.doesNotHaveAllKeys(
new Map([
[{ foo: 1 }, 'bar'],
['key', 'value'],
]),
[{ one: 'two' }, 'example']
);
assert.doesNotHaveAllKeys(new Set([{ foo: 'bar' }, 'anotherKey']), [
{ one: 'two' },
'example',
]);
});
hasAnyDeepKeys
- 類型:
<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
不具有所提供的任何 keys
。由於 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, '拋出的錯誤必須有此訊息');
assert.throws(fn, /拋出的錯誤必須有匹配此訊息的訊息/);
assert.throws(fn, ReferenceError);
assert.throws(fn, errorInstance);
assert.throws(
fn,
ReferenceError,
'拋出的錯誤必須是 ReferenceError 並具有此訊息'
);
assert.throws(
fn,
errorInstance,
'拋出的錯誤必須是相同的 errorInstance 並具有此訊息'
);
assert.throws(
fn,
ReferenceError,
/拋出的錯誤必須是 ReferenceError 並匹配此訊息/
);
assert.throws(
fn,
errorInstance,
/拋出的錯誤必須是相同的 errorInstance 並匹配此訊息/
);
});
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, '任何拋出的錯誤都不能有此訊息');
assert.doesNotThrow(fn, /任何拋出的錯誤都不能匹配此訊息/);
assert.doesNotThrow(fn, Error);
assert.doesNotThrow(fn, errorInstance);
assert.doesNotThrow(fn, Error, '錯誤不能有此訊息');
assert.doesNotThrow(fn, errorInstance, '錯誤不能有此訊息');
assert.doesNotThrow(fn, Error, /錯誤不能匹配此訊息/);
assert.doesNotThrow(fn, errorInstance, /錯誤不能匹配此訊息/);
});
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, '一切正常');
});
closeTo
- 類型:
(actual: number, expected: number, delta: number, message?: string) => void
- 別名:
approximately
斷言 actual
等於 expected
,在 +/- delta
範圍內。
import { assert, test } from 'vitest';
test('assert.closeTo', () => {
assert.closeTo(1.5, 1, 0.5, '數字接近');
});
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], '相同成員');
});
notSameMembers
- 類型:
<T>(set1: T[], set2: T[], message?: string) => void
斷言 set1
和 set2
不具有相同的成員,順序不拘。使用嚴格相等檢查 (===)。
import { assert, test } from 'vitest';
test('assert.notSameMembers', () => {
assert.notSameMembers([1, 2, 3], [5, 1, 3], '成員不同');
});
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 }],
'相同深度成員'
);
});
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 }],
'深度成員不同'
);
});
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], '相同順序成員');
});
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],
'順序不同的成員'
);
});
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 }],
'相同深度順序成員'
);
});
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 }],
'深度順序不同的成員'
);
assert.notSameDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }, { c: 3 }],
'深度順序不同的成員'
);
});
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], '包含成員');
});
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], '不包含成員');
});
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 }],
'包含深度成員'
);
});
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 }],
'不包含深度成員'
);
});
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], '包含有序成員');
});
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],
'不包含有序成員'
);
assert.notIncludeOrderedMembers(
[1, 2, 3],
[2, 3],
'不包含有序成員'
);
});
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 }],
'包含深度有序成員'
);
});
notIncludeDeepOrderedMembers
- 類型:
<T>(superset: T[], subset: T[], message?: string) => void
斷言 subset
不包含在 superset
中,且順序與 superset
的第一個元素開始的順序相同。使用深度相等檢查。
import { assert, test } from 'vitest';
test('assert.notIncludeDeepOrderedMembers', () => {
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ a: 1 }, { f: 5 }],
'不包含深度有序成員'
);
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { a: 1 }],
'不包含深度有序成員'
);
assert.notIncludeDeepOrderedMembers(
[{ a: 1 }, { b: 2 }, { c: 3 }],
[{ b: 2 }, { c: 3 }],
'不包含深度有序成員'
);
});
oneOf
- 類型:
<T>(inList: T, list: T[], message?: string) => void
斷言非物件、非陣列值 inList
出現在平面陣列 list
中。
import { assert, test } from 'vitest';
test('assert.oneOf', () => {
assert.oneOf(1, [2, 1], '未在列表中找到');
});
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
值或 modifier
的回傳值更改 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
值或 modifier
的回傳值增加了 change
。
import { assert, test } from 'vitest';
test('assert.increasesBy', () => {
const obj = { val: 10 };
function fn() {
obj.val += 10;
}
assert.increasesBy(fn, obj, 'val', 10);
});
doesNotIncrease
- 類型:
<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
值或 modifier
的回傳值增加 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
值或 modifier
的回傳值減少了 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
是否為假值;如果為真值,則拋出錯誤。此功能旨在讓 Chai 能夠作為 Node 的 assert
類別的直接替代品。
import { assert, test } from 'vitest';
test('assert.ifError', () => {
const err = new Error('我是一個自訂錯誤');
assert.ifError(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.freeze({});
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 });
});