Skip to content
Vitest 1
Main Navigation 指南API配置高級
1.6.1
0.34.6

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

外觀

Sidebar Navigation

指南

為什麼使用 Vitest

開始使用

功能特性

工作區

命令列界面

測試過濾器

報告器

覆蓋率

快照

模擬(Mocking)

測試類型

Vitest UI

瀏覽器模式

原始碼測試

測試上下文

測試環境

擴展匹配器

IDE 整合支援

偵錯

與其他測試執行器的比較

遷移指南

常見錯誤

提升效能

API

測試 API 參考文件

模擬函數

Vi

expect

expectTypeOf

assert

assertType

配置

管理 Vitest 配置文件

配置 Vitest

本頁導覽

assert ​

Vitest 重新導出 chai 的 assert 方法,用於驗證不變式。

assert ​

  • 類型: (expression: any, message?: string) => asserts expression

斷言給定的 expression 為真值 (truthy),否則會觸發斷言失敗。

ts
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

強制斷言失敗。

ts
import { assert, test } from 'vitest';

test('assert.fail', () => {
  assert.fail('失敗時的錯誤訊息');
  assert.fail('foo', 'bar', 'foo is not bar', '===');
});

isOk ​

  • 類型: <T>(value: T, message?: string) => void
  • 別名 ok

斷言給定的 value 為真值 (truthy)。

ts
import { assert, test } from 'vitest';

test('assert.isOk', () => {
  assert.isOk('foo', '每個真值都是 ok');
  assert.isOk(false, '這個斷言會失敗,因為 false 不是真值');
});

isNotOk ​

  • 類型: <T>(value: T, message?: string) => void
  • 別名 notOk

斷言給定的 value 為假值 (falsy)。

ts
import { assert, test } from 'vitest';

test('assert.isNotOk', () => {
  assert.isNotOk('foo', '這個斷言會失敗,因為每個真值都不是 ok');
  assert.isNotOk(false, '這個斷言會通過,因為 false 是假值');
});

equal ​

  • 類型: <T>(actual: T, expected: T, message?: string) => void

斷言 actual 與 expected 非嚴格相等 (==)。

ts
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 非嚴格不等 (!=)。

ts
import { assert, test } from 'vitest';

test('assert.equal', () => {
  assert.notEqual(Math.sqrt(4), 3);
});

strictEqual ​

  • 類型: <T>(actual: T, expected: T, message?: string) => void

斷言 actual 與 expected 嚴格相等 (===)。

ts
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。

ts
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。

ts
import { assert, test } from 'vitest';

test('assert.notDeepEqual', () => {
  assert.notDeepEqual({ color: 'green' }, { color: 'red' });
});

isAbove ​

  • 類型: (valueToCheck: number, valueToBeAbove: number, message?: string) => void

斷言 valueToCheck 嚴格大於 (>) valueToBeAbove。

ts
import { assert, test } from 'vitest';

test('assert.isAbove', () => {
  assert.isAbove(5, 2, '5 嚴格大於 2');
});

isAtLeast ​

  • 類型: (valueToCheck: number, valueToBeAtLeast: number, message?: string) => void

斷言 valueToCheck 大於或等於 (>=) valueToBeAtLeast。

ts
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。

ts
import { assert, test } from 'vitest';

test('assert.isBelow', () => {
  assert.isBelow(3, 6, '3 嚴格小於 6');
});

isAtMost ​

  • 類型: (valueToCheck: number, valueToBeAtMost: number, message?: string) => void

斷言 valueToCheck 小於或等於 (<=) valueToBeAtMost。

ts
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。

ts
import { assert, test } from 'vitest';

const testPassed = true;

test('assert.isTrue', () => {
  assert.isTrue(testPassed);
});

isNotTrue ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 不是 true。

ts
import { assert, test } from 'vitest';

const testPassed = 'ok';

test('assert.isNotTrue', () => {
  assert.isNotTrue(testPassed);
});

isFalse ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是 false。

ts
import { assert, test } from 'vitest';

const testPassed = false;

test('assert.isFalse', () => {
  assert.isFalse(testPassed);
});

isNotFalse ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 不是 false。

ts
import { assert, test } from 'vitest';

const testPassed = 'no';

test('assert.isNotFalse', () => {
  assert.isNotFalse(testPassed);
});

isNull ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是 null。

ts
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。

ts
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 (Not a Number,非數字)。

ts
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 (Not a Number,非數字)。

ts
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。

ts
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。

ts
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。

ts
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。

ts
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 是一個函式 (function)。
ts
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 不是一個函式 (function)。

ts
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 揭示)。 此斷言不適用於子類化的物件。

ts
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 的結果判斷)。此斷言不包含繼承的物件。

ts
import { assert, test } from 'vitest';

const someThing = 'redCircle';

test('assert.isNotObject', () => {
  assert.isNotObject(someThing, 'someThing is not object but string');
});

isArray ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是一個陣列。

ts
import { assert, test } from 'vitest';

const color = ['red', 'green', 'yellow'];

test('assert.isArray', () => {
  assert.isArray(color, 'color is array');
});

isNotArray ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 不是一個陣列。

ts
import { assert, test } from 'vitest';

const color = 'red';

test('assert.isNotArray', () => {
  assert.isNotArray(color, 'color is not array but string');
});

isString ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是一個字串。

ts
import { assert, test } from 'vitest';

const color = 'red';

test('assert.isString', () => {
  assert.isString(color, 'color is string');
});

isNotString ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 不是一個字串。

ts
import { assert, test } from 'vitest';

const color = ['red', 'green', 'yellow'];

test('assert.isNotString', () => {
  assert.isNotString(color, 'color is not string but array');
});

isNumber ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是一個數字。

ts
import { assert, test } from 'vitest';

const colors = 3;

test('assert.isNumber', () => {
  assert.isNumber(colors, 'colors is number');
});

isNotNumber ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 不是一個數字。

ts
import { assert, test } from 'vitest';

const colors = '3 colors';

test('assert.isNotNumber', () => {
  assert.isNotNumber(colors, 'colors is not number but strings');
});

isFinite ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是一個有限數字(即不是 NaN 或 Infinity)。

ts
import { assert, test } from 'vitest';

const colors = 3;

test('assert.isFinite', () => {
  assert.isFinite(colors, 'colors is number not NaN or Infinity');
});

isBoolean ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 是一個布林值。

ts
import { assert, test } from 'vitest';

const isReady = true;

test('assert.isBoolean', () => {
  assert.isBoolean(isReady, 'isReady is a boolean');
});

isNotBoolean ​

  • 類型: <T>(value: T, message?: string) => void

斷言 value 不是一個布林值。

ts
import { assert, test } from 'vitest';

const isReady = 'sure';

test('assert.isBoolean', () => {
  assert.isBoolean(isReady, 'isReady is not a boolean but string');
});

typeOf ​

  • 類型: <T>(value: T, name: string, message?: string) => void

斷言 value 的類型是 name,由 Object.prototype.toString 確定。

ts
import { assert, test } from 'vitest';

test('assert.typeOf', () => {
  assert.typeOf({ color: 'red' }, 'object', 'we have an object');
  assert.typeOf(['red', 'green'], 'array', 'we have an array');
  assert.typeOf('red', 'string', 'we have a string');
  assert.typeOf(/red/, 'regexp', 'we have a regular expression');
  assert.typeOf(null, 'null', 'we have a null');
  assert.typeOf(undefined, 'undefined', 'we have an undefined');
});

notTypeOf ​

  • 類型: <T>(value: T, name: string, message?: string) => void

斷言 value 的類型不是 name,由 Object.prototype.toString 確定。

ts
import { assert, test } from 'vitest';

test('assert.notTypeOf', () => {
  assert.notTypeOf('red', 'number', '"red" is not a number');
});

instanceOf ​

  • 類型: <T>(value: T, constructor: Function, message?: string) => void

斷言 value 是 constructor 的一個實例。

ts
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 is an instance of Person');
  assert.instanceOf(coffee, Tea, 'coffee is an instance of Tea');
});

notInstanceOf ​

  • 類型: <T>(value: T, constructor: Function, message?: string) => void

斷言 value 不是 constructor 的一個實例。

ts
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 is not an instance of 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。 可以用於斷言陣列中包含一個值,字串中包含一個子字串,或物件中包含一組屬性。

ts
import { assert, test } from 'vitest';

test('assert.include', () => {
  assert.include([1, 2, 3], 2, 'array contains value');
  assert.include('foobar', 'foo', 'string contains substring');
  assert.include(
    { foo: 'bar', hello: 'universe' },
    { foo: 'bar' },
    'object contains property'
  );
});

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。 可以用於斷言陣列中不存在一個值,字串中不存在一個子字串,或物件中不存在一組屬性。

ts
import { assert, test } from 'vitest';

test('assert.notInclude', () => {
  assert.notInclude([1, 2, 3], 4, "array doesn't contain 4");
  assert.notInclude('foobar', 'baz', "foobar doesn't contain baz");
  assert.notInclude(
    { foo: 'bar', hello: 'universe' },
    { foo: 'baz' },
    "object doesn't contain property"
  );
});

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。 可以用於斷言陣列中包含一個值,或物件中包含一組屬性。 使用深度相等性檢查。

ts
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。 可以用於斷言陣列中不存在一個值,或物件中不存在一組屬性。 使用深度相等性檢查。

ts
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。 可以用於斷言物件中包含一組屬性。 允許使用點表示法和方括號表示法來引用巢狀屬性。 屬性名稱中的 '[]' 和 '.' 可以使用雙反斜線來跳脫(escape)。

ts
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。 可以用於斷言物件中不包含一組屬性。 允許使用點表示法和方括號表示法來引用巢狀屬性。 屬性名稱中的 '[]' 和 '.' 可以使用雙反斜線來跳脫(escape)。

ts
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 ​

  • 類型: (haystack: any, needle: any, message?: string) => void

斷言 haystack 包含 needle。 可以用於斷言物件中包含一組屬性,同時檢查深度相等性。 允許使用點表示法和方括號表示法來引用巢狀屬性。 屬性名稱中的 '[]' 和 '.' 可以使用雙反斜線來跳脫(escape)。

ts
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。 可以用於斷言物件中不存在一組屬性,同時檢查深度相等性。 允許使用點表示法和方括號表示法來引用巢狀屬性。 屬性名稱中的 '[]' 和 '.' 可以使用雙反斜線來跳脫(escape)。

ts
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。 可以用於斷言物件中包含一組屬性,同時忽略繼承的屬性。

ts
import { assert, test } from 'vitest';

test('assert.ownInclude', () => {
  assert.ownInclude({ a: 1 }, { a: 1 });
});

notOwnInclude ​

  • 類型: (haystack: any, needle: any, message?: string) => void

斷言 haystack 不包含 needle。 可以用於斷言物件中不存在一組屬性,同時忽略繼承的屬性。

ts
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。可用於斷言物件包含特定的屬性子集,同時忽略繼承屬性並進行深度相等性檢查。

ts
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。可用於斷言物件不包含特定的屬性子集,同時忽略繼承屬性並進行深度相等性檢查。

ts
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。

ts
import { assert, test } from 'vitest';

test('assert.match', () => {
  assert.match('foobar', /^foo/, 'regexp matches');
});

notMatch ​

  • 類型: (value: string, regexp: RegExp, message?: string) => void

斷言 value 不符合正規表達式 regexp。

ts
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 的屬性,無論該屬性是直接屬性還是繼承屬性。

ts
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 的屬性,無論該屬性是直接屬性還是繼承屬性。

ts
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。 使用嚴格相等性檢查 (===)。

ts
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。 使用嚴格相等性檢查 (===)。

ts
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。 使用深度相等性檢查。

ts
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。 使用深度相等性檢查。

ts
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 可以是使用點記號和方括號表示法的字串,用於巢狀引用。

ts
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 可以是使用點記號和方括號表示法的字串,用於巢狀引用。

ts
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 可以使用點記號和方括號表示法進行巢狀引用。使用嚴格相等性檢查 (===)。

ts
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 可以使用點記號和方括號表示法進行巢狀引用。使用嚴格相等性檢查 (===)。

ts
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 可以使用點記號和方括號表示法進行巢狀引用。使用深度相等性檢查。

ts
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 可以使用點記號和方括號表示法進行巢狀引用。使用深度相等性檢查。

ts
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 屬性具有預期的值。

ts
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 之一。 您也可以提供單個物件來代替鍵陣列,該物件的鍵將被視為預期的鍵集合。

ts
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。 您也可以提供單個物件來代替鍵陣列,該物件的鍵將被視為預期的鍵集合。

ts
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,但可能包含未列出的其他鍵。 您也可以提供單個物件來代替鍵陣列,該物件的鍵將被視為預期的鍵集合。

ts
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(鍵)。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
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(鍵)。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
import { assert, test } from 'vitest';

test('assert.doesNotHaveAllKeys', () => {
  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(映射)可以將物件作為鍵,因此您可以使用此斷言來執行深度比較。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
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(映射)可以將物件作為鍵,因此您可以使用此斷言來執行深度比較。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
import { assert, test } from 'vitest';

test('assert.hasAnyDeepKeys', () => {
  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(映射)可以將物件作為鍵,因此您可以使用此斷言來執行深度比較。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
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(映射)可以將物件作為鍵,因此您可以使用此斷言來執行深度比較。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
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(映射)可以將物件作為鍵,因此您可以使用此斷言來執行深度比較。您可以提供一個物件,而不是鍵陣列,物件的鍵將被視為預期的鍵集合。

ts
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

斷言 fn 將拋出一個錯誤。

  • 如果 errorLike 是一個 Error 建構函式,則斷言 fn 拋出的錯誤是 errorLike 的實例。
  • 如果 errorLike 是一個 Error 實例,則斷言 fn 拋出的錯誤與 errorLike 是同一個實例。
  • 如果提供了 errMsgMatcher,則還會斷言拋出的錯誤訊息與 errMsgMatcher 匹配。
ts
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

斷言 fn 不會拋出錯誤。

  • 如果 errorLike 是一個 Error 建構函式,則斷言 fn 不會拋出 errorLike 的實例。
  • 如果 errorLike 是一個 Error 實例,則斷言 fn 拋出的錯誤不是與 errorLike 是同一個實例。
  • 如果提供了 errMsgMatcher,則還會斷言拋出的錯誤訊息與 errMsgMatcher 不匹配。
ts
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。

ts
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,其差異在 +/- delta 範圍內。

ts
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 具有相同的成員,忽略成員的順序。 使用嚴格相等性檢查 (===)。

ts
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 不具有相同的成員,忽略成員的順序。 使用嚴格相等性檢查 (===)。

ts
import { assert, test } from 'vitest';

test('assert.notSameMembers', () => {
  assert.notSameMembers([1, 2, 3], [5, 1, 3], 'not same members'); // 不相同的成員
});

sameDeepMembers ​

  • 類型: <T>(set1: T[], set2: T[], message?: string) => void

斷言 set1 和 set2 具有相同的成員,忽略成員的順序。 使用深度相等性檢查。

ts
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 不具有相同的成員,忽略成員的順序。 使用深度相等性檢查。

ts
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' // 深度相同的成員
  );
});

sameOrderedMembers ​

  • 類型: <T>(set1: T[], set2: T[], message?: string) => void

斷言 set1 和 set2 具有相同的成員,且成員的順序也相同。 使用嚴格相等性檢查 (===)。

ts
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 不具有相同的成員,或成員的順序不相同。 使用嚴格相等性檢查 (===)。

ts
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 具有相同的成員,且成員的順序也相同。 使用深度相等性檢查。

ts
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 ​

  • Type: <T>(set1: T[], set2: T[], message?: string) => void

斷言 set1 和 set2 的成員順序或內容不完全相同。 使用深度相等性檢查。

ts
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 ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 的所有成員都包含在 superset 中(順序不限)。 使用嚴格相等性檢查 (===)。 重複的成員會被忽略。

ts
import { assert, test } from 'vitest';

test('assert.includeMembers', () => {
  assert.includeMembers([1, 2, 3], [2, 1, 2], 'include members');
});

notIncludeMembers ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 並未完全包含在 superset 中(順序不限)。 使用嚴格相等性檢查 (===)。 重複的成員會被忽略。

ts
import { assert, test } from 'vitest';

test('assert.notIncludeMembers', () => {
  assert.notIncludeMembers([1, 2, 3], [5, 1], 'not include members');
});

includeDeepMembers ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 的所有成員都包含在 superset 中(順序不限)。 使用深度相等性檢查。 重複的成員會被忽略。

ts
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 ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 並未完全包含在 superset 中(順序不限)。 使用深度相等性檢查。 重複的成員會被忽略。

ts
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 ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 以相同的順序包含在 superset 中,且從 superset 的第一個元素開始匹配。 使用嚴格相等性檢查 (===)。

ts
import { assert, test } from 'vitest';

test('assert.includeOrderedMembers', () => {
  assert.includeOrderedMembers([1, 2, 3], [1, 2], 'include ordered members');
});

notIncludeOrderedMembers ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 並未以相同的順序包含在 superset 中,且從 superset 的第一個元素開始匹配。 使用嚴格相等性檢查 (===)。

ts
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 ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 以相同的順序包含在 superset 中,且從 superset 的第一個元素開始匹配。 使用深度相等性檢查。

ts
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 ​

  • Type: <T>(superset: T[], subset: T[], message?: string) => void

斷言 subset 並未以相同的順序包含在 superset 中,且從 superset 的第一個元素開始匹配。 使用深度相等性檢查。

ts
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 ​

  • Type: <T>(inList: T, list: T[], message?: string) => void

斷言 inList (非物件、非陣列值) 出現在扁平陣列 list 中。

ts
import { assert, test } from 'vitest';

test('assert.oneOf', () => {
  assert.oneOf(1, [2, 1], 'Not found in list');
});

changes ​

  • Type: <T>(modifier: Function, object: T, property: string, message?: string) => void

斷言執行 modifier 後,object 的 property 值會發生改變。

ts
import { assert, test } from 'vitest';

test('assert.changes', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val = 22;
  }
  assert.changes(fn, obj, 'val');
});

changesBy ​

  • Type: <T>(modifier: Function, object: T, property: string, change: number, message?: string) => void

斷言執行 modifier 後,object 的 property 值會改變 change 的量。

ts
import { assert, test } from 'vitest';

test('assert.changesBy', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val += 2;
  }
  assert.changesBy(fn, obj, 'val', 2);
});

doesNotChange ​

  • Type: <T>(modifier: Function, object: T, property: string, message?: string) => void

斷言執行 modifier 後,object 的 property 值不會發生改變。

ts
import { assert, test } from 'vitest';

test('assert.doesNotChange', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val += 2;
  }
  assert.doesNotChange(fn, obj, 'val', 2);
});

changesButNotBy ​

  • Type: <T>(modifier: Function, object: T, property: string, change:number, message?: string) => void

斷言執行 modifier 後,object 的 property 值發生了改變,但改變的量不是 change。

ts
import { assert, test } from 'vitest';

test('assert.changesButNotBy', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val += 10;
  }
  assert.changesButNotBy(fn, obj, 'val', 5);
});

increases ​

  • Type: <T>(modifier: Function, object: T, property: string, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 會增加。

ts
import { assert, test } from 'vitest';

test('assert.increases', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val = 13;
  }
  assert.increases(fn, obj, 'val');
});

increasesBy ​

  • Type: <T>(modifier: Function, object: T, property: string, change: number, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 增加了 change 的值。

ts
import { assert, test } from 'vitest';

test('assert.increases', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val += 10;
  }
  assert.increases(fn, obj, 'val', 10);
});

doesNotIncrease ​

  • Type: <T>(modifier: Function, object: T, property: string, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 並未增加。

ts
import { assert, test } from 'vitest';

test('assert.doesNotIncrease', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val = 8;
  }
  assert.doesNotIncrease(fn, obj, 'val');
});

increasesButNotBy ​

  • Type: <T>(modifier: Function, object: T, property: string, change: number, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 增加了,但增加的量不是 change。

ts
import { assert, test } from 'vitest';

test('assert.increasesButNotBy', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val += 15;
  }
  assert.increasesButNotBy(fn, obj, 'val', 10);
});

decreases ​

  • Type: <T>(modifier: Function, object: T, property: string, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 會減少。

ts
import { assert, test } from 'vitest';

test('assert.decreases', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val = 5;
  }
  assert.decreases(fn, obj, 'val');
});

decreasesBy ​

  • Type: <T>(modifier: Function, object: T, property: string, change: number, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 減少了 change 的值。

ts
import { assert, test } from 'vitest';

test('assert.decreasesBy', () => {
  const obj = { val: 10 };
  function fn() {
    obj.val -= 5;
  }
  assert.decreasesBy(fn, obj, 'val', 5);
});

doesNotDecrease ​

  • Type: <T>(modifier: Function, object: T, property: string, message?: string) => void

斷言執行 modifier 後,object 的數值型 property 並未減少。

ts
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。

ts
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。

ts
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 不是 false 值。如果 object 是 true 值,則拋出錯誤。 添加此功能是為了使 chai 可以直接替代 Node 的 assert 類別。

ts
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 是可擴展的(可以新增屬性)。

ts
import { assert, test } from 'vitest';

test('assert.isExtensible', () => {
  assert.isExtensible({});
});

isNotExtensible ​

  • 類型: <T>(object: T, message?: string) => void
  • 別名: notExtensible

斷言 object 是不可擴展的(無法新增屬性)。

ts
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 是密封的(無法新增屬性,且其現有屬性無法刪除)。

ts
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 不是密封的(可以新增屬性,且可以刪除現有屬性)。

ts
import { assert, test } from 'vitest';

test('assert.isNotSealed', () => {
  assert.isNotSealed({});
});

isFrozen ​

  • 類型: <T>(object: T, message?: string) => void
  • 別名: frozen

斷言 object 是凍結的(無法新增屬性,且其現有屬性無法修改)。

ts
import { assert, test } from 'vitest';

test('assert.isFrozen', () => {
  const frozenObject = Object.freeze({});
  assert.frozen(frozenObject);
});

isNotFrozen ​

  • 類型: <T>(object: T, message?: string) => void
  • 別名: notFrozen

斷言 object 不是凍結的(可以新增屬性,且可以修改其現有屬性)。

ts
import { assert, test } from 'vitest';

test('assert.isNotFrozen', () => {
  assert.isNotFrozen({});
});

isEmpty ​

  • 類型: <T>(target: T, message?: string) => void
  • 別名: empty

斷言 target 不包含任何內容。 對於陣列和字串,會檢查其 length 屬性。 對於 Map 和 Set 實例,會檢查其 size 屬性。 對於非函數物件,會取得其自身可列舉字串鍵的數量。

ts
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 屬性。 對於非函數物件,會取得其自身可列舉字串鍵的數量。

ts
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 });
});
Pager
上一頁expectTypeOf
下一頁assertType

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors

https://v1.vitest.dev/api/assert

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors