assert
Vitest 重新匯出 assert
方法,方法來自 chai
,用於驗證不變項。
assert
- 類型:
(expression: any, message?: string) => asserts expression
斷言給定的 expression
為真,否則斷言失敗。
import { assert, test } from 'vitest'
test('assert', () => {
assert('foo' !== 'bar', 'foo should not be equal to 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('error message on failure')
assert.fail('foo', 'bar', 'foo is not bar', '===')
})
isOk
- 類型:
<T>(value: T, message?: string) => void
- 別名
ok
斷言給定的 value
為真。
import { assert, test } from 'vitest'
test('assert.isOk', () => {
assert.isOk('foo', 'every truthy is ok')
assert.isOk(false, 'this will fail since false is not truthy')
})
isNotOk
- 類型:
<T>(value: T, message?: string) => void
- 別名
notOk
斷言給定的 value
為假。
import { assert, test } from 'vitest'
test('assert.isNotOk', () => {
assert.isNotOk('foo', 'this will fail, every truthy is not ok')
assert.isNotOk(false, 'this will pass since false is falsy')
})
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.equal', () => {
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 is strictly greater than 2')
})
isAtLeast
- 類型:
(valueToCheck: number, valueToBeAtLeast: number, message?: string) => void
斷言 valueToCheck
大於或等於(> =)valueToBeAtLeast
。
import { assert, test } from 'vitest'
test('assert.isAtLeast', () => {
assert.isAtLeast(5, 2, '5 is greater or equal to 2')
assert.isAtLeast(3, 3, '3 is greater or equal to 3')
})
isBelow
- 類型:
(valueToCheck: number, valueToBeBelow: number, message?: string) => void
斷言 valueToCheck
嚴格小於(<)valueToBeBelow
。
import { assert, test } from 'vitest'
test('assert.isBelow', () => {
assert.isBelow(3, 6, '3 is strictly less than 6')
})
isAtMost
- 類型:
(valueToCheck: number, valueToBeAtMost: number, message?: string) => void
斷言 valueToCheck
小於或等於(< =)valueToBeAtMost
。
import { assert, test } from 'vitest'
test('assert.isAtMost', () => {
assert.isAtMost(3, 6, '3 is less than or equal to 6')
assert.isAtMost(4, 4, '4 is less than or equal to 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 is 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 is not null but object')
})
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" is 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 is Not NaN but 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 is neither null nor 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 is null so not exist')
assert.notExists(bar, 'bar is undefined so not exist')
})
isUndefined
- 類型:
<T>(value: T, message?: string) => void
斷言 value
為 undefined。
import { assert, test } from 'vitest'
const name = undefined
test('assert.isUndefined', () => {
assert.isUndefined(name, 'name is 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 is not 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 is function')
})
isNotFunction
- 類型:
<T>(value: T, message?: string) => void
- 別名:
isNotCallable
斷言 value
不為函式。
import { assert, test } from 'vitest'
const name = 'foo'
test('assert.isNotFunction', () => {
assert.isNotFunction(name, 'name is not function but string')
})
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 is object')
})
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 is not object but string')
})
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 is array')
})
isNotArray
- 類型:
<T>(value: T, message?: string) => void
斷言 value
不為陣列。
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
為字串。
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
不為字串。
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
是數字。
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
不是數字。
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)。
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
是布林值。
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
不是布林值。
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 決定。
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 決定。
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
的實例。
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
的實例。
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
。可用於斷言陣列中值的包含、字串中的子字串或物件中屬性子集的包含。
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
。可用於斷言陣列中值的遺漏、字串中的子字串遺漏或物件中屬性子集的遺漏。
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
。可用於斷言陣列中值的包含或物件中屬性子集的包含。使用深度相等性。
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.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
。可用於斷言物件中包含某個子集合的屬性,同時檢查深度相等性。允許使用點號和方括號表示法來參照巢狀屬性。屬性名稱中的「[]」和「.」可以使用雙反斜線進行跳脫。
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
。
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
中至少一個。您也可以提供一個單一物件,而不是一個鍵陣列,其鍵將用作預期的鍵集。
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
。您也可以提供一個單一物件,而不是一個 keys 陣列,其 keys 將被用作預期的 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
。由於 Sets 和 Maps 可以將物件作為 keys,因此您可以使用此斷言來執行深度比較。您也可以提供一個單一物件,而不是一個 keys 陣列,其 keys 將被用作預期的 keys 組。
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
。由於 Sets 和 Maps 可以將物件作為 keys,因此您可以使用此斷言來執行深度比較。您也可以提供一個單一物件,而不是一個 keys 陣列,其 keys 將被用作預期的 keys 組。
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
。由於 Sets 和 Maps 可以將物件作為 keys,因此您可以使用此斷言來執行深度比較。您也可以提供一個單一物件,而不是一個 keys 陣列,其 keys 將被用作預期的 keys 組。
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
。由於 Sets 和 Maps 可以將物件作為 keys,因此您可以使用此斷言來執行深度比較。您也可以提供一個單一物件,而不是一個 keys 陣列,其 keys 將被用作預期的 keys 組。
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
。由於 Sets 和 Maps 可以將物件作為 keys,因此您可以使用此斷言來執行深度比較。您也可以提供一個單一物件,而不是一個 keys 陣列,其 keys 將被用作預期的 keys 組。
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
,範圍在 +/- 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.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
在相同的順序中具有相同的成員。使用嚴格相等檢查 (===)。
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
會變更 property
的 object
。
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
會將 property
的 object
變更為 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
不會變更 property
的 object
。
import { assert, test } from 'vitest'
test('assert.doesNotChange', () => {
const obj = { val: 10 }
function fn() { obj.val += 2 };
assert.doesNotChange(fn, obj, 'val', 2)
})
changesButNotBy
- 類型:
<T>(modifier: Function, object: T, property: string, change:number, message?: string) => void
斷言 modifier
不會將 property
的 object
或 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.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
或 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
不是 false 值,則斷言,如果它是 true 值,則擲回。新增此功能,讓 chai 可取代 Node 的 assert 類別。
import { assert, test } from 'vitest'
test('assert.ifError', () => {
const err = new Error('I am a custom error')
assert.ifError(err) // Rethrows 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
斷言物件已凍結(無法新增新的屬性,也無法修改現有的屬性)。
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
不包含任何值。對於陣列和字串,它會檢查長度屬性。對於 Map 和 Set 執行個體,它會檢查大小屬性。對於非函式物件,它會取得其自身可列舉字串金鑰的數量。
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
包含值。對於陣列和字串,它會檢查長度屬性。對於 Map 和 Set 執行個體,它會檢查大小屬性。對於非函式物件,它會取得其自身可列舉字串金鑰的數量。
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 })
})