Assert

The Assert plugin of Japa is built on top of Chai.js assert package. However, it is not an exact copy of chai, and therefore we recommend you to consult the API documented in this guide.

Suppose you have not configured the plugin during the initial setup. Then, you can install it from the npm registry as follows.

npm i -D @japa/assert@1.4.1

And register it as a plugin within the bin/test.js file.

import { assert } from '@japa/assert'
import { configure, processCliArgs } from '@japa/runner'
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.js'],
plugins: [assert()]
}
})
const { assert } = require('@japa/assert')
const { configure, processCliArgs } = require('@japa/runner')
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.js'],
plugins: [assert()]
}
})

Once done. You can access the assert property from the Test context as follows.

test('add two numbers', ({ assert }) => {
assert.equal(2 + 2, 4)
})

Open API testing

Using this plugin, you can test HTTP responses against one or more open API schemas. Just make sure to register the path to schema files when using the plugin.

const config = {
openApi: {
schemas: [join(__dirname, '..', './api-schema.json')]
}
}
configure({
plugins: [
assert(config) // 👈 register config
]
})

Once done, you can test response objects from axios, superagent, supertest, request, and light-my-request libraries using the following method.

test('get /users', async ({ assert }) => {
const response = await supertest(baseUrl).get('/')
assert.isValidApiResponse(response)
})

The response is validated as follows:

  • An error is raised if the request path is not documented in the API schema file.
  • An error is raised if the response status code is not documented in the API schema file.
  • An error is raised if the body properties are not the same as expected by the schema. Use the required array to validate required response properties.
  • The response status code is used to find the body schema for validation.
  • Response headers are also validated against the expected headers mentioned inside the API schema file.

assert

Assert an expression to be truthy. Then, optionally define the error message.

assert(isTrue(foo))
assert(foo === 'bar')
assert(age > 18, 'Not allowed to enter the club')

equal

Assert two values are equal but not strictly. The comparison is the same as foo == bar.

assert.equal(3, 3) // passes
assert.equal(3, '3') // passes
assert.equal(Symbol.for('foo'), Symbol.for('foo')) // passes
NameType
actualany
expectedany
message?string

notEqual

Assert two values are not equal. The comparison is the same as foo != bar.

assert.notEqual(3, 2) // passes
assert.notEqual(3, '2') // passes
assert.notEqual(Symbol.for('foo'), Symbol.for('bar')) // passes
NameType
actualany
expectedany
message?string

strictEqual

Assert two values are strictly equal. The comparison is same as foo === bar.

  • See equal for non-strict equality.
  • See deepEqual for comparing objects and arrays.
assert.strictEqual(3, 3) // passes
assert.strictEqual(3, '3') // fails
assert.strictEqual(Symbol.for('foo'), Symbol.for('foo')) // passes
NameType
actualany
expectedany
message?string

notStrictEqual

Assert two values are not strictly equal. The comparison is same as foo !== bar.

assert.notStrictEqual(3, 2) // passes
assert.notStrictEqual(3, '2') // fails
assert.notStrictEqual(Symbol.for('foo'), Symbol.for('bar')) // passes
NameType
actualany
expectedany
message?string

deepEqual

Assert two values are deeply equal. The order of items in an array should be the same for the assertion to pass.

assert.deepEqual({ a: 1, b: 2 }, { a: 1, b: 2 }) // passes
assert.deepEqual({ b: 2, a: 1 }, { a: 1, b: 2 }) // passes
assert.deepEqual([1, 2], [1, 2]) // passes
assert.deepEqual([1, 2], [2, 1]) // fails
assert.deepEqual(/a/, /a/) // passes
assert.deepEqual(
new Date('2020 01 22'),
new Date('2020 01 22')
) // passes
NameType
actualany
expectedany
message?string

notDeepEqual

Assert two values are not deeply equal.

assert.notDeepEqual({ a: 1, b: 2 }, { a: 1, b: '2' }) // passes
assert.notDeepEqual([1, 2], [2, 1]) // passes
assert.notDeepEqual(
new Date('2020 01 22'),
new Date('2020 01 23')
) // passes
NameType
actualany
expectedany
message?string

rejects

Assert the function to reject the promise. Optionally, you can assert the rejection is from a particular class or has a specific message.

await assert.rejects(() => {
throw new Error('')
}) // passes
await assert.rejects(() => {
async () => throw new HttpError('Resource not found')
}, HttpError) // passes
await assert.rejects(() => {
async () => throw new HttpError('Resource not found')
}, 'Resource not found') // passes
NameType
fn() => void
message?string
NameType
fn() => void | Promise<void>
errTypeRegExp | ErrorConstructor
message?string
NameType
fn() => void | Promise<void>
constructorErrorConstructor
regExpstring | RegExp
message?string

doesNotRejects

Assert the function does not reject the promise. Optionally, you can assert the rejection is not from a specific class or have a particular message.

await assert.doesNotRejects(async () => {
throw new Error('foo')
}, HttpError) // passes: Error !== HttpError
await assert.doesNotRejects(
async () => {
throw new HttpError('Resource not found')
},
'Server not available'
) // passes: Resource not found !== Server not available
await assert.doesNotRejects(async () => {
return 'foo'
}) // passes
NameType
fn() => void
message?string
NameType
fn() => void | Promise<void>
errTypeRegExp | ErrorConstructor
message?string
NameType
fn() => void | Promise<void>
constructorErrorConstructor
regExpstring | RegExp
message?string

throws

Expect the function to throw an exception. Optionally, you can assert for the exception class or message.

  • See rejects for async function calls.
function foo() { throw new Error('blow up') }
assert.throws(foo) // passes
assert.throws(foo, Error) // passes
assert.throws(foo, 'blow up') // passes
assert.throws(foo, 'failed') // fails
NameType
fn() => void
message?string
NameType
fn() => void
errTypeRegExp | ErrorConstructor
message?string
NameType
fn() => void
constructorErrorConstructor
regExpstring | RegExp
message?string

doesNotThrows

Expect the function not to throw an exception. Optionally, you can assert the exception is not from a particular class or has a specific message.

function foo() { throw new Error('blow up') }
assert.doesNotThrows(foo) // fails
assert.doesNotThrows(foo, 'failed') // passes
assert.doesNotThrows(() => {}) // passes
NameType
fn() => void
message?string
NameType
fn() => void
regExpRegExp
NameType
fn() => void
constructorErrorConstructor
message?string
NameType
fn() => void
constructorErrorConstructor
regExpstring | RegExp
message?string

lengthOf

Assert the length of an array, map, or set to match the expected value

assert.lengthOf([1, 2, 3], 3)
assert.lengthOf(new Map([[1],[2]]), 2)
assert.lengthOf('hello world', 11)
NameType
objectArray
lengthnumber
message?string

empty/isEmpty

Assert value to be empty.

assert.empty([]) // passes
assert.empty('') // passes
assert.isEmpty([]) // passes
assert.isEmpty('') // passes
NameType
objectany
message?string

notEmpty/isNotEmpty

Assert value to not be empty.

assert.notEmpty([1, 2]) // passes
assert.notEmpty({ foo: 'bar' }) // passes
assert.notEmpty('hello') // passes
NameType
objectany
message?string

exists

Asserts the value is not null or undefined.

assert.exists(false) // passes
assert.exists(0) // passes
assert.exists('') // passes
assert.exists(null) // fails
assert.exists(undefined) // fails
NameType
valueany
message?string

notExists

Asserts the value is null or undefined.

assert.notExists(null) // passes
assert.notExists(undefined) // passes
assert.notExists('') // fails
assert.notExists(false) // fails
assert.notExists(0) // fails
NameType
valueany
message?string

include

Assert the collection includes an item. Works for strings, arrays, and objects.

assert.include(
{ id: 1, name: 'virk' },
{ name: 'virk' }
) // passes
assert.include([1, 2, 3], 2) // passes
assert.include('hello world', 'hello') // passes
NameType
haystackany
needlePartial<haystack>
message?string

notInclude

Assert the collection NOT to include an item. Works for strings, arrays and objects.

assert.include(
{ id: 1, name: 'virk' },
{ name: 'virk' }
) // passes
assert.include([1, 2, 3], 2) // passes
assert.include('hello world', 'hello') // passes
NameType
haystackany
needlePartial<haystack>
message?string

deepInclude

Assert the collection includes an item. Works for strings, arrays, and objects.

assert.deepInclude(
{ foo: { a: 1 }, bar: { b: 2 } },
{ foo: { a: 1 } }
) // passes
assert.deepInclude([1, [2], 3], [2]) // passes
NameType
haystackany
needlePartial<haystack>
message?string

notDeepInclude

Assert the collection NOT to include an item. Works for strings, arrays, and objects.

assert.notDeepInclude(
{ foo: { a: 1 }, bar: { b: 2 } },
{ foo: { a: 4 } }
) // passes
assert.deepInclude([1, [2], 3], [20]) // passes
NameType
haystackany
needlePartial<haystack>
message?string

instanceOf

Assert value to be an instance of the expected class.

assert.instanceOf(new User(), User) // passes
assert.instanceOf(new User(), Function) // fails
class User extends BaseUser {}
assert.instanceOf(new User(), BaseUser) // passes
NameType
valueany
constructorFunction
message?string

notInstanceOf

Assert value NOT to be an instance of the expected class.

assert.notInstanceOf(new User(), Function) // passes
assert.notInstanceOf(new User(), User) // fails
NameType
valueany
constructorFunction
message?string

ok/isOk

Assert the value is truthy.

assert.ok({ hello: 'world' }) // passes
assert.ok(null) // fails
assert.isOk({ hello: 'world' }) // passes
assert.isOk(null) // fails
NameType
valueany
message?string

notOk/isNotOk

Assert the value is falsy.

assert.notOk({ hello: 'world' }) // fails
assert.notOk(null) // passes
assert.isNotOk({ hello: 'world' }) // fails
assert.isNotOk(null) // passes
NameType
valueany
message?string

properties

Assert the object has all of the expected properties.

assert.properties(
{ username: 'virk', age: 22, id: 1 },
['id', 'age']
) // passes
assert.properties(
{ username: 'virk', id: 1 },
['id', 'age']
) // fails: "age" is missing
NameType
objectany
keysstring[]
message?string

notAllProperties

Assert the object not to have all of the mentioned properties.

assert.notAllProperties(
{ id: 1, name: 'foo' },
['id', 'name', 'email']
) // passes
NameType
objectany
keys`string[]
message?string

onlyProperties

Assert the object has only the expected properties. The assertion will fail when the object contains properties not mentioned in the keys array.

assert.onlyProperties(
{ username: 'virk', age: 22, id: 1 },
['id', 'name', 'age']
) // passes
assert.onlyProperties(
{ username: 'virk', age: 22, id: 1 },
['id', 'name']
) // fails
NameType
objectany
keysstring[]
message?string

notAnyProperties

Assert the object not to have any of the mentioned properties.

assert.notAnyProperties(
{ id: 1, name: 'foo' },
['email', 'age']
) // passes
assert.notAnyProperties(
{ id: 1, name: 'foo' },
['email', 'id']
) // fails
NameType
objectany
keys`string[]
message?string

property

Assert an object to contain a property.

assert.property(
{ id: 1, username: 'virk' },
'id'
) // passes
NameType
objectany
propertystring
message?string

notProperty

Assert an object NOT to contain a property.

assert.notProperty(
{ id: 1, username: 'virk' },
'email'
) // passes
NameType
objectany
propertystring
message?string

propertyVal

Assert an object property to match the expected value.

assert.propertyVal(
{ id: 1, username: 'virk' },
'id',
1
) // passes
assert.propertyVal(
{ user: { id: 1 } },
'user',
{ id: 1 }
) // fails
NameType
objectany
propertystring
valueany
message?string

notPropertyVal

Assert an object property NOT to match the expected value.

assert.notPropertyVal(
{ id: 1, username: 'virk' },
'id',
22
) // passes
NameType
objectany
propertystring
valueany
message?string

deepPropertyVal

Assert an object property to match the expected value deeply.

assert.deepPropertyVal(
{ user: { id: 1 } },
'user',
{ id: 1 }
) // passes
NameType
objectany
propertystring
valueany
message?string

notDeepPropertyVal

Assert an object property NOT deeply to match the expected value

assert.notDeepPropertyVal(
{ user: { id: 1 } },
'user',
{ email: 'foo@bar.com' }
) // passes
NameType
actualany
propertystring
valueany
message?string

includeMembers

Assert the expected array is a subset of a given collection. The values comparison is the same as the assert.equal method.

assert.includeMembers([1, 2, 4, 5], [1, 2]) // passes
assert.includeMembers([1, 2, 4, 5], [1, 3]) // fails
NameType
supersetany[]
subsetany[]
message?string

notIncludeMembers

Assert the expected array is NOT a subset of a given collection. The values comparison is the same as the assert.notEqual method.

assert.notIncludeMembers([1, 2, 4, 5], [1, 3]) // passes
assert.notIncludeMembers([1, 2, 4, 5], [1, 2]) // fails
NameType
supersetany[]
subsetany[]
message?string

includeDeepMembers

Assert the expected array is a subset of a given array. The values comparison is the same as the assert.deepEqual method.

assert.includeDeepMembers(
[{ id: 1 }, { id: 2 }],
[{ id: 2 }]
) // passes
assert.includeDeepMembers(
[{ id: 1 }, { id: 2 }],
[{ id: 3 }]
) // fails
NameType
supersetany[]
subsetany[]
message?string

notIncludeDeepMembers

Assert the expected array is NOT a subset of a given array. The values comparison is the same as the assert.notDeepEqual method.

assert.notIncludeDeepMembers(
[{ id: 1 }, { id: 2 }],
[{ id: 3 }]
) // passes
assert.notIncludeDeepMembers(
[{ id: 1 }, { id: 2 }],
[{ id: 2 }]
) // fails
NameType
supersetany[]
subsetany[]
message?string

includeOrderedMembers

Assert the expected array is a subset of a given array and in the same order. The values comparison is the same as the assert.equal method.

assert.includeOrderedMembers(
[1, 2, 4, 5],
[1, 2, 4]
) // passes
assert.includeOrderedMembers(
[1, 2, 4, 5],
[1, 4, 2]
) // fails
assert.includeOrderedMembers(
[1, 2, 4, 5],
[1, 2, 5]
) // fails
NameType
supersetany[]
subsetany[]
message?string

notIncludeOrderedMembers

Assert the expected array is either not a subset of a given array or is not in the same order. The values comparison is the same as the assert.notEqual method.

assert.notIncludeOrderedMembers(
[1, 2, 4, 5],
[1, 4, 2]
) // passes
assert.notIncludeOrderedMembers(
[1, 2, 4, 5],
[1, 2, 5]
) // passes
assert.notIncludeOrderedMembers(
[1, 2, 4, 5],
[1, 2, 4]
) // fails
NameType
supersetany[]
subsetany[]
message?string

includeDeepOrderedMembers

Assert the expected array is a subset of a given array and in the same order. The values comparison is the same as the assert.deepEqual method.

assert.includeDeepOrderedMembers(
[{ id: 1 }, { id: 2 }, { id: 4 }],
[{ id: 1 }, { id: 2 }]
) // passes
assert.includeDeepOrderedMembers(
[{ id: 1 }, { id: 2 }, { id: 4 }],
[{ id: 1 }, { id: 4 }]
) // fails
assert.includeDeepOrderedMembers(
[{ id: 1 }, { id: 2 }, { id: 4 }],
[{ id: 1 }, { id: 4 }, { id: 2 }]
) // fails
NameType
supersetany[]
subsetany[]
message?string

notIncludeDeepOrderedMembers

Assert the expected array is either not a subset of a given array or is not in the same order. The values comparison is the same as the assert.notDeepEqual method.

assert.notIncludeDeepOrderedMembers(
[{ id: 1 }, { id: 2 }, { id: 4 }],
[{ id: 1 }, { id: 4 }]
) // passes
assert.notIncludeDeepOrderedMembers(
[{ id: 1 }, { id: 2 }, { id: 4 }],
[{ id: 1 }, { id: 4 }, { id: 2 }]
) // passes
assert.notIncludeDeepOrderedMembers(
[{ id: 1 }, { id: 2 }, { id: 4 }],
[{ id: 1 }, { id: 2 }]
) // fails
NameType
supersetany[]
subsetany[]
message?string

sameMembers

Assert two arrays to have the same members. The values comparison is the same as the assert.equal method.

assert.sameMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.sameMembers(
[1, { id: 1 }, 3],
[1, { id: 1 }, 3]
) // fails
NameType
set1any[]
set2any[]
message?string

notSameMembers

Assert two arrays NOT to have the same members. The values comparison is the same as the assert.notEqual method.

assert.notSameMembers(
[1, { id: 1 }, 3],
[1, { id: 1 }, 3]
) // passes
assert.notSameMembers(
[1, 2, 3],
[1, 2, 3]
) // fails
NameType
set1any[]
set2any[]
message?string

sameDeepMembers

Assert two arrays to have the same members.

assert.sameDeepMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.sameDeepMembers(
[1, { id: 1 }, 3],
[1, { id: 1 }, 3]
) // passes
NameType
set1any[]
set2any[]
message?string

notSameDeepMembers

Assert two arrays NOT to have the same members.

assert.notSameDeepMembers(
[1, { id: 1 }, 3],
[1, { id: 2 }, 3]
) // passes
NameType
set1any[]
set2any[]
message?string

sameOrderedMembers

Expect two arrays to have the same members and in the same order. The values comparison is the same as the assert.equal method.

assert.sameOrderedMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.sameOrderedMembers(
[1, 3, 2],
[1, 2, 3]
) // fails
NameType
set1any[]
set2any[]
message?string

notSameOrderedMembers

Expect two arrays to either have different members or be in a different order. The values comparison is the same as the assert.notEqual method.

assert.notSameOrderedMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.notSameOrderedMembers(
[1, 3, 2],
[1, 2, 3]
) // fails
NameType
set1any[]
set2any[]
message?string

sameDeepOrderedMembers

Expect two arrays to have the same members and in the same order. The values comparison is the same as the assert.deepEqual method.

assert.sameDeepOrderedMembers(
[1, { id: 1 }, { name: 'virk' }],
[1, { id: 1 }, { name: 'virk' }]
) // passes
assert.sameDeepOrderedMembers(
[1, { id: 1 }, { name: 'virk' }],
[1, { name: 'virk' }, { id: 1 }]
) // fails
NameType
set1any[]
set2any[]
message?string

notSameDeepOrderedMembers

Expect two arrays to either have different members or be in a different order. The values comparison is the same as the assert.notDeepEqual method.

assert.notSameDeepOrderedMembers(
[1, { id: 1 }, { name: 'virk' }],
[1, { name: 'virk' }, { id: 1 }]
) // passes
assert.notSameDeepOrderedMembers(
[1, { id: 1 }, { name: 'virk' }],
[1, { id: 1 }, { name: 'virk' }]
) // fails
NameType
set1any[]
set2any[]
message?string

containsSubset

Assert an array or an object to contain a subset of the expected value. Useful for testing API responses.

assert.containsSubset(
{ id: 1, created_at: Date },
{ id: 1 }
) // passes
assert.containsSubset(
[
{ id: 1, created_at: Date },
{ id: 2, created_at: Date }
],
[{ id: 1 }, { id: 2 }]
) // passes
NameType
haystackany
needleany
message?string

notContainsSubset

Assert an array or an object not to contain a subset of the expected value.

assert.notContainsSubset(
{ id: 1, created_at: Date },
{ email: 'foo@bar.com' }
) // passes
NameType
haystackany
needleany
message?string

oneOf

Assert the value is available in the provided list. This method only works with literal values.

assert.oneOf('foo', ['foo', 'bar', 'baz']) // passes
assert.oneOf('foo', ['bar', 'baz']) // fails
NameType
valueany
collectionany[]
message?string

sealed/isSealed

Assert the object is sealed.

assert.sealed(Object.seal({})) // passes
assert.sealed({}) // fails
assert.isSealed(Object.seal({})) // passes
assert.isSealed({}) // fails
NameType
objectany
message?string

notSealed/isNotSealed

Assert the object is not sealed.

assert.notSealed({}) // passes
assert.notSealed(Object.seal({})) // fails
assert.isNotSealed({}) // passes
assert.isNotSealed(Object.seal({})) // fails
NameType
objectany
message?string

isAbove

Assert if the actual value is above the expected value. Supports numbers, dates and luxon datetime object.

assert.isAbove(5, 2) // passes
assert.isAbove(new Date('2020 12 20'), new Date('2020 12 18')) // passes
NameType
valueToCheckDate
valueToBeAboveDate
message?string
NameType
valueToChecknumber
valueToBeAbovenumber
message?string

isBelow

Assert if the actual value is below expected value. numbers, dates and luxon datetime object.

assert.isBelow(2, 5) // passes
assert.isBelow(new Date('2020 12 20'), new Date('2020 12 24')) // passes
NameType
valueToCheckDate
valueToBeBelowDate
message?string
NameType
valueToChecknumber
valueToBeBelownumber
message?string

isAtLeast

Assert if the actual value is above or the same as the expected value. Supports numbers, dates and luxon datetime object.

assert.isAtLeast(2, 2) // passes
assert.isAtLeast(new Date('2020 12 20'), new Date('2020 12 20')) // passes
NameType
valueToCheckDate
valueToBeAtLeastDate
message?string
NameType
valueToChecknumber
valueToBeAtLeastnumber
message?string

isAtMost

Assert if the actual value is below or the same as the expected value. numbers, dates and luxon datetime object.

assert.isAtMost(2, 2) // passes
assert.isAtMost(new Date('2020 12 20'), new Date('2020 12 20')) // passes
NameType
valueToCheckDate
valueToBeAtMostDate
message?string
NameType
valueToChecknumber
valueToBeAtMostnumber
message?string

match

Assert the value to match the given regular expression.

assert.match('foobar', /^foo/) // passes
NameType
valuestring
regexpRegExp
message?string

notMatch

Assert the value NOT to match the given regular expression.

assert.notMatch('foobar', /^foo/) // fails
NameType
expectedstring
regexpRegExp
message?string

typeOf

Assert the typeof value that matches the expected type.

assert.typeOf({ foo: 'bar' }, 'object') // passes
assert.typeOf(['admin'], 'array') // passes
assert.typeOf(new Date(), 'date') // passes
NameType
valueany
typestring
message?string

notTypeOf

Assert the typeof value is not the same as the expected type.

assert.notTypeOf({ foo: 'bar' }, 'array') // passes
assert.notTypeOf(['admin'], 'string') // passes
NameType
valueany
typestring
message?string

frozen/isFrozen

Assert the object is frozen.

assert.frozen(Object.freeze({})) // passes
assert.frozen({}) // fails
assert.isFrozen(Object.freeze({})) // passes
assert.isFrozen({}) // fails
NameType
objectany
message?string

notFrozen/isNotFrozen

Assert the object is not frozen.

assert.notFrozen({}) // passes
assert.notFrozen(Object.freeze({})) // fails
assert.isNotFrozen({}) // passes
assert.isNotFrozen(Object.freeze({})) // fails
NameType
objectany
message?string

isArray

Assert the value to be a valid array

assert.isArray([]) // passes
assert.isArray({}) // fails
NameType
valueany
message?string

isNotArray

Assert the value not to be an array

assert.isNotArray([]) // fails
assert.isNotArray({}) // passes
NameType
valueany
message?string

isBoolean

Assert the value is a boolean.

assert.isBoolean(true) // passes
assert.isBoolean(false) // passes
assert.isBoolean(1) // fails
NameType
valueany
message?string

isNotBoolean

Assert the value is anything, but not a boolean.

assert.isNotBoolean(1) // passes
assert.isNotBoolean(false) // fails
assert.isNotBoolean(true) // fails
NameType
valueany
message?string

isDefined

Asserts the value is anything, but not undefined.

assert.isDefined(undefined) // fails
assert.isDefined(0) // passes
assert.isDefined(false) // passes
assert.isDefined('') // passes
assert.isDefined(null) // passes
NameType
valueany
message?string

isUndefined

Asserts the value is explicitly "undefined."

assert.isUndefined(undefined) // passes
assert.isUndefined(false) // fails
assert.isUndefined(0) // fails
assert.isUndefined('') // fails
assert.isUndefined(null) // fails
NameType
valueany
message?string

isFalse

Assert the value is boolean (false)

assert.isFalse(false) // passes
assert.isFalse(true) // fails
assert.isFalse(0) // fails
assert.isFalse(null) // fails
NameType
valueany
message?string

isNotFalse

Assert the value is anything but not false.

assert.isNotFalse(false) // fails
assert.isNotFalse(true) // passes
assert.isNotFalse(null) // passes
assert.isNotFalse(undefined) // passes
NameType
valueany
message?string

isFinite

Assert the value to be a number and no NaN or Infinity

assert.isFinite(1) // passes
assert.isFinite(Infinity) // fails
assert.isFinite(NaN) // fails
NameType
valueany
message?string

isFunction

Assert the value is a function.

assert.isFunction(function foo () {}) // passes
assert.isFunction(() => {}) // passes
assert.isFunction(class Foo {}) // passes
NameType
valueany
message?string

isNotFunction

Assert the value is not a function

assert.isNotFunction({}) // passes
assert.isNotFunction(null) // passes
assert.isNotFunction(() => {}) // fails
NameType
valueany
message?string

isNaN

Assert the value is NaN

assert.isNaN(NaN) // passes
assert.isNaN(Number('hello')) // passes
assert.isNaN(true) // fails
assert.isNaN(false) // fails
assert.isNaN(null) // fails
NameType
valueany
message?string

isNotNaN

Assert the value is anything, but not NaN.

assert.isNotNaN(NaN) // fails
assert.isNotNaN(Number('hello')) // fails
assert.isNotNaN(true) // passes
assert.isNotNaN(false) // passes
assert.isNotNaN(null) // passes
NameType
valueany
message?string

isNull

Assert the value is null

assert.isNull(null) // passes
assert.isNull(true) // fails
assert.isNull(false) // fails
assert.isNull('foo') // fails
NameType
valueany
message?string

isNotNull

Assert the value is anything but not null

assert.isNotNull(null) // fails
assert.isNotNull(true) // passes
assert.isNotNull(false) // passes
assert.isNotNull('foo') // passes
NameType
valueany
message?string

isNumber

Assert the value to be a valid number.

assert.isNumber(1) // passes
assert.isNumber(new Number('1')) // passes
assert.isNumber('1') // fails
NameType
valueany
message?string

isNotNumber

Assert the value not to be a valid number.

assert.isNotNumber('1') // passes
assert.isNotNumber(1) // fails
NameType
valueany
message?string

isObject

Assert the value to a valid object literal.

assert.isObject({}) // passes
assert.isObject(new SomeClass()) // passes
assert.isObject(null) // fails
assert.isObject([]) // fails
NameType
objectany
message?string

isNotObject

Assert the value not to be an object literal.

assert.isNotObject(null) // passes
assert.isNotObject([]) // passes
assert.isNotObject({}) // fails
assert.isNotObject(new SomeClass()) // fails
NameType
valueany
message?string

isString

Assert the value to be a literal string.

assert.isString('') // passes
assert.isString(new String(true)) // passes
assert.isString(1) // fails
NameType
valueany
message?string

isNotString

Assert the value not to be a string literal.

assert.isNotString(1) // passes
assert.isNotString('') // fails
assert.isNotString(new String(true)) // fails
NameType
valueany
message?string

isTrue

Assert the value is a boolean (true).

assert.isTrue(true) // passes
assert.isTrue(false) // fails
assert.isTrue(1) // fails
assert.isTrue('foo') // fails
NameType
valueany
message?string

isNotTrue

Assert the value is anything, but not true.

assert.isNotTrue(true) // fails
assert.isNotTrue(false) // passes
assert.isNotTrue(1) // passes
assert.isNotTrue('foo') // passes
NameType
valueany
message?string

closeTo

Assert the value is closer to the expected value + delta.

assert.closeTo(10, 6, 8) // passes
assert.closeTo(10, 6, 4) // passes
assert.closeTo(10, 20, 10) // passes

fail

Throw an error. Optionally accepts actual and expected values for the default error message.

The actual and expected values are not compared. However, they are available as properties on the AssertionError.

assert.fail() // fail
assert.fail('Error message for the failure')
assert.fail(1, 2, 'expected 1 to equal 2')
assert.fail(1, 2, 'expected 1 to be greater than 2', '>')
NameType
message?string
NameType
actualany
expectedany
message?string
operator?string

plan

Plan assertions for the test. The test will be marked as failed when the actual assertions count does not match the planned assertions count.

assert.plan(2)
NameType
assertionsToExpectnumber