assert
chai's assert functions, recorded as soft assertions outside a test.
Declaration
assert(value: any, message?: string): void
relay.assert(value: any, message?: string): voidOverview
assert is chai's assert, with one difference: outside a test, each call is a soft assertion. Relay records it as a row in the Assertions section of the Tests tab, passed or failed, and the script carries on even when it fails. Use it for quick checks that don't need a test of their own.
assert.equal(relay.response.code, 200);
assert.isBelow(relay.response.responseTime, 1000, "Responds within a second");
assert.isArray(relay.response.json().items);Inside a test callback, assert behaves as in chai: a failed assertion throws and fails the test.
Topics
Truthiness
assert(value, message)
assert(value: any, message?: string)Checks that value is truthy.
isOk(value, message)
assert.isOk(value: any, message?: string)Checks that value is truthy.
isTrue(value, message)
assert.isTrue(value: any, message?: string)Checks that value is true.
isFalse(value, message)
assert.isFalse(value: any, message?: string)Checks that value is false.
exists(value, message)
assert.exists(value: any, message?: string)Checks that value is neither null nor undefined.
isNull(value, message)
assert.isNull(value: any, message?: string)Checks that value is null.
isNotNull(value, message)
assert.isNotNull(value: any, message?: string)Checks that value isn't null.
isDefined(value, message)
assert.isDefined(value: any, message?: string)Checks that value isn't undefined.
isUndefined(value, message)
assert.isUndefined(value: any, message?: string)Checks that value is undefined.
Equality
equal(actual, expected, message)
assert.equal(actual: any, expected: any, message?: string)Checks that actual == expected.
notEqual(actual, expected, message)
assert.notEqual(actual: any, expected: any, message?: string)Checks that actual != expected.
strictEqual(actual, expected, message)
assert.strictEqual(actual: any, expected: any, message?: string)Checks that actual === expected.
deepEqual(actual, expected, message)
assert.deepEqual(actual: any, expected: any, message?: string)Checks that actual and expected have equal contents, compared recursively.
Numbers
isAbove(value, above, message)
assert.isAbove(value: number, above: number, message?: string)Checks that value is greater than above.
isBelow(value, below, message)
assert.isBelow(value: number, below: number, message?: string)Checks that value is less than below.
isAtLeast(value, min, message)
assert.isAtLeast(value: number, min: number, message?: string)Checks that value is greater than or equal to min.
isAtMost(value, max, message)
assert.isAtMost(value: number, max: number, message?: string)Checks that value is less than or equal to max.
Types
typeOf(value, type, message)
assert.typeOf(value: any, type: string, message?: string)Checks that value's type is type, such as "string".
isArray(value, message)
assert.isArray(value: any, message?: string)Checks that value is an array.
isString(value, message)
assert.isString(value: any, message?: string)Checks that value is a string.
isNumber(value, message)
assert.isNumber(value: any, message?: string)Checks that value is a number.
isObject(value, message)
assert.isObject(value: any, message?: string)Checks that value is an object.
Contents
include(haystack, needle, message)
assert.include(haystack: string | any[] | object, needle: any, message?: string)Checks that a string, array or object includes needle.
match(value, regexp, message)
assert.match(value: string, regexp: RegExp, message?: string)Checks that value matches regexp.
property(object, property, message)
assert.property(object: object, property: string, message?: string)Checks that object has the named property.
lengthOf(value, length, message)
assert.lengthOf(value: any, length: number, message?: string)Checks that value's length is length.
Failing
fail(message)
assert.fail(message?: string)Fails with message.
Discussion
Names of soft assertions
A soft assertion's row is named after its message when you pass one. Otherwise it's named after the line of the script that made it, such as assert.equal(relay.response.code, 200). The row's link shows that line in the script.
Asserting in async code
Only the synchronous part of a test callback counts as inside the test. After an await in an async test, assert records a soft assertion instead of failing the test. Use expect in async tests.
Errors other than failed assertions, such as a TypeError from a missing value, aren't recorded as assertions; they stop the script as usual.
Every other function of chai's assert works the same way, as do the relay.assert and pm.assert names.
See Also
- test: A function that runs a named check and records whether it passed.
- expect: A function that starts a chai or Jest-style assertion about a value.
- Testing responses: Check responses automatically with tests and assertions.