title | description | ogImage |
---|---|---|
Assert |
Assertion plugin built on top of Chai.js assert package. |
assert-plugin.jpeg |
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.
:::languageSwitcher
// title: ESM
// highlight-start
import { assert } from '@japa/assert'
// highlight-end
import { configure, processCliArgs } from '@japa/runner'
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.js'],
// highlight-start
plugins: [assert()]
// highlight-end
}
})
// title: CommonJS
// highlight-start
const { assert } = require('@japa/assert')
// highlight-end
const { configure, processCliArgs } = require('@japa/runner')
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.js'],
// highlight-start
plugins: [assert()]
// highlight-end
}
})
:::
Once done. You can access the assert
property from the Test context as follows.
test('add two numbers', ({ assert }) => {
assert.equal(2 + 2, 4)
})
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 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')
Assert two values are equal but not strictly. The comparison is the same as foo == bar
.
- See strictEqual for strict equality.
- See deepEqual for comparing objects and arrays.
assert.equal(3, 3) // passes
assert.equal(3, '3') // passes
assert.equal(Symbol.for('foo'), Symbol.for('foo')) // passes
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
Assert two values are not equal. The comparison is the same as foo != bar
.
- See notStrictEqual for strict inequality.
- See notDeepEqual for comparing objects and arrays.
assert.notEqual(3, 2) // passes
assert.notEqual(3, '2') // passes
assert.notEqual(Symbol.for('foo'), Symbol.for('bar')) // passes
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
Assert two values are strictly equal. The comparison is same as foo === bar
.
assert.strictEqual(3, 3) // passes
assert.strictEqual(3, '3') // fails
assert.strictEqual(Symbol.for('foo'), Symbol.for('foo')) // passes
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
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
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
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
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
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
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
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
Name | Type |
---|---|
fn |
() => void |
message? |
string |
Name | Type |
---|---|
fn |
() => void | Promise <void > |
errType |
RegExp | ErrorConstructor |
message? |
string |
Name | Type |
---|---|
fn |
() => void | Promise <void > |
constructor |
ErrorConstructor |
regExp |
string | RegExp |
message? |
string |
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
Name | Type |
---|---|
fn |
() => void |
message? |
string |
Name | Type |
---|---|
fn |
() => void | Promise <void > |
errType |
RegExp | ErrorConstructor |
message? |
string |
Name | Type |
---|---|
fn |
() => void | Promise <void > |
constructor |
ErrorConstructor |
regExp |
string | RegExp |
message? |
string |
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
Name | Type |
---|---|
fn |
() => void |
message? |
string |
Name | Type |
---|---|
fn |
() => void |
errType |
RegExp | ErrorConstructor |
message? |
string |
Name | Type |
---|---|
fn |
() => void |
constructor |
ErrorConstructor |
regExp |
string | RegExp |
message? |
string |
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.
- See doesNotRejects for async function calls.
function foo() { throw new Error('blow up') }
assert.doesNotThrows(foo) // fails
assert.doesNotThrows(foo, 'failed') // passes
assert.doesNotThrows(() => {}) // passes
Name | Type |
---|---|
fn |
() => void |
message? |
string |
Name | Type |
---|---|
fn |
() => void |
regExp |
RegExp |
Name | Type |
---|---|
fn |
() => void |
constructor |
ErrorConstructor |
message? |
string |
Name | Type |
---|---|
fn |
() => void |
constructor |
ErrorConstructor |
regExp |
string | RegExp |
message? |
string |
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)
Name | Type |
---|---|
object |
Array |
length |
number |
message? |
string |
Assert value to be empty.
assert.empty([]) // passes
assert.empty('') // passes
assert.isEmpty([]) // passes
assert.isEmpty('') // passes
Name | Type |
---|---|
object |
any |
message? |
string |
Assert value to not be empty.
assert.notEmpty([1, 2]) // passes
assert.notEmpty({ foo: 'bar' }) // passes
assert.notEmpty('hello') // passes
Name | Type |
---|---|
object |
any |
message? |
string |
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
Name | Type |
---|---|
value |
any |
message? |
string |
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
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the collection includes an item. Works for strings, arrays, and objects.
- See deepInclude for deep comparsion.
assert.include(
{ id: 1, name: 'virk' },
{ name: 'virk' }
) // passes
assert.include([1, 2, 3], 2) // passes
assert.include('hello world', 'hello') // passes
Name | Type |
---|---|
haystack |
any |
needle |
Partial<haystack> |
message? |
string |
Assert the collection NOT to include an item. Works for strings, arrays and objects.
- See notDeepInclude for nested object properties.
assert.include(
{ id: 1, name: 'virk' },
{ name: 'virk' }
) // passes
assert.include([1, 2, 3], 2) // passes
assert.include('hello world', 'hello') // passes
Name | Type |
---|---|
haystack |
any |
needle |
Partial<haystack> |
message? |
string |
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
Name | Type |
---|---|
haystack |
any |
needle |
Partial<haystack> |
message? |
string |
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
Name | Type |
---|---|
haystack |
any |
needle |
Partial<haystack> |
message? |
string |
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
Name | Type |
---|---|
value |
any |
constructor |
Function |
message? |
string |
Assert value NOT to be an instance of the expected class.
assert.notInstanceOf(new User(), Function) // passes
assert.notInstanceOf(new User(), User) // fails
Name | Type |
---|---|
value |
any |
constructor |
Function |
message? |
string |
Assert the value is truthy.
assert.ok({ hello: 'world' }) // passes
assert.ok(null) // fails
assert.isOk({ hello: 'world' }) // passes
assert.isOk(null) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is falsy.
assert.notOk({ hello: 'world' }) // fails
assert.notOk(null) // passes
assert.isNotOk({ hello: 'world' }) // fails
assert.isNotOk(null) // passes
Name | Type |
---|---|
value |
any |
message? |
string |
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
Name | Type |
---|---|
object |
any |
keys |
string[] |
message? |
string |
Assert the object not to have all of the mentioned properties.
assert.notAllProperties(
{ id: 1, name: 'foo' },
['id', 'name', 'email']
) // passes
Name | Type |
---|---|
object |
any |
keys |
`string[] |
message? |
string |
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
Name | Type |
---|---|
object |
any |
keys |
string[] |
message? |
string |
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
Name | Type |
---|---|
object |
any |
keys |
`string[] |
message? |
string |
Assert an object to contain a property.
assert.property(
{ id: 1, username: 'virk' },
'id'
) // passes
Name | Type |
---|---|
object |
any |
property |
string |
message? |
string |
Assert an object NOT to contain a property.
assert.notProperty(
{ id: 1, username: 'virk' },
'email'
) // passes
Name | Type |
---|---|
object |
any |
property |
string |
message? |
string |
Assert an object property to match the expected value.
- Use deepPropertyVal for deep comparing the value.
assert.propertyVal(
{ id: 1, username: 'virk' },
'id',
1
) // passes
assert.propertyVal(
{ user: { id: 1 } },
'user',
{ id: 1 }
) // fails
Name | Type |
---|---|
object |
any |
property |
string |
value |
any |
message? |
string |
Assert an object property NOT to match the expected value.
- Use notDeepPropertyVal for deep comparing the value.
assert.notPropertyVal(
{ id: 1, username: 'virk' },
'id',
22
) // passes
Name | Type |
---|---|
object |
any |
property |
string |
value |
any |
message? |
string |
Assert an object property to match the expected value deeply.
assert.deepPropertyVal(
{ user: { id: 1 } },
'user',
{ id: 1 }
) // passes
Name | Type |
---|---|
object |
any |
property |
string |
value |
any |
message? |
string |
Assert an object property NOT deeply to match the expected value
assert.notDeepPropertyVal(
{ user: { id: 1 } },
'user',
{ email: 'foo@bar.com' }
) // passes
Name | Type |
---|---|
actual |
any |
property |
string |
value |
any |
message? |
string |
Assert the expected array is a subset of a given collection. The values comparison is the same as the assert.equal
method.
- Use includeDeepMembers for deep comparsion.
assert.includeMembers([1, 2, 4, 5], [1, 2]) // passes
assert.includeMembers([1, 2, 4, 5], [1, 3]) // fails
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
Assert the expected array is NOT a subset of a given collection. The values comparison is the same as the assert.notEqual
method.
- Use notIncludeDeepMembers for deep comparsion.
assert.notIncludeMembers([1, 2, 4, 5], [1, 3]) // passes
assert.notIncludeMembers([1, 2, 4, 5], [1, 2]) // fails
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
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
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
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
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
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.
- Use includeDeepOrderedMembers for deep comparsion.
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
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
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.
- Use notIncludeDeepOrderedMembers for deep comparsion.
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
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
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
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
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
Name | Type |
---|---|
superset |
any[] |
subset |
any[] |
message? |
string |
Assert two arrays to have the same members. The values comparison
is the same as the assert.equal
method.
- Use sameDeepMembers for deep comparison.
assert.sameMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.sameMembers(
[1, { id: 1 }, 3],
[1, { id: 1 }, 3]
) // fails
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
Assert two arrays NOT to have the same members. The values comparison
is the same as the assert.notEqual
method.
- Use notSameDeepMembers for deep comparison.
assert.notSameMembers(
[1, { id: 1 }, 3],
[1, { id: 1 }, 3]
) // passes
assert.notSameMembers(
[1, 2, 3],
[1, 2, 3]
) // fails
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
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
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
Assert two arrays NOT to have the same members.
assert.notSameDeepMembers(
[1, { id: 1 }, 3],
[1, { id: 2 }, 3]
) // passes
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
Expect two arrays to have the same members and in the same order. The values comparison is the same as the assert.equal
method.
- Use sameDeepOrderedMembers for deep comparison.
assert.sameOrderedMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.sameOrderedMembers(
[1, 3, 2],
[1, 2, 3]
) // fails
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
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.
- Use notSameDeepOrderedMembers for deep comparison.
assert.notSameOrderedMembers(
[1, 2, 3],
[1, 2, 3]
) // passes
assert.notSameOrderedMembers(
[1, 3, 2],
[1, 2, 3]
) // fails
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
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
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
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
Name | Type |
---|---|
set1 |
any[] |
set2 |
any[] |
message? |
string |
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
Name | Type |
---|---|
haystack |
any |
needle |
any |
message? |
string |
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
Name | Type |
---|---|
haystack |
any |
needle |
any |
message? |
string |
Assert the value is available in the provided list. This method only works with literal values.
- See containsSubset matching objects and arrays.
assert.oneOf('foo', ['foo', 'bar', 'baz']) // passes
assert.oneOf('foo', ['bar', 'baz']) // fails
Name | Type |
---|---|
value |
any |
collection |
any[] |
message? |
string |
Assert the object is sealed.
assert.sealed(Object.seal({})) // passes
assert.sealed({}) // fails
assert.isSealed(Object.seal({})) // passes
assert.isSealed({}) // fails
Name | Type |
---|---|
object |
any |
message? |
string |
Assert the object is not sealed.
assert.notSealed({}) // passes
assert.notSealed(Object.seal({})) // fails
assert.isNotSealed({}) // passes
assert.isNotSealed(Object.seal({})) // fails
Name | Type |
---|---|
object |
any |
message? |
string |
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
Name | Type |
---|---|
valueToCheck |
Date |
valueToBeAbove |
Date |
message? |
string |
Name | Type |
---|---|
valueToCheck |
number |
valueToBeAbove |
number |
message? |
string |
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
Name | Type |
---|---|
valueToCheck |
Date |
valueToBeBelow |
Date |
message? |
string |
Name | Type |
---|---|
valueToCheck |
number |
valueToBeBelow |
number |
message? |
string |
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
Name | Type |
---|---|
valueToCheck |
Date |
valueToBeAtLeast |
Date |
message? |
string |
Name | Type |
---|---|
valueToCheck |
number |
valueToBeAtLeast |
number |
message? |
string |
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
Name | Type |
---|---|
valueToCheck |
Date |
valueToBeAtMost |
Date |
message? |
string |
Name | Type |
---|---|
valueToCheck |
number |
valueToBeAtMost |
number |
message? |
string |
Assert the value to match the given regular expression.
assert.match('foobar', /^foo/) // passes
Name | Type |
---|---|
value |
string |
regexp |
RegExp |
message? |
string |
Assert the value NOT to match the given regular expression.
assert.notMatch('foobar', /^foo/) // fails
Name | Type |
---|---|
expected |
string |
regexp |
RegExp |
message? |
string |
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
Name | Type |
---|---|
value |
any |
type |
string |
message? |
string |
Assert the typeof value is not the same as the expected type.
assert.notTypeOf({ foo: 'bar' }, 'array') // passes
assert.notTypeOf(['admin'], 'string') // passes
Name | Type |
---|---|
value |
any |
type |
string |
message? |
string |
Assert the object is frozen.
assert.frozen(Object.freeze({})) // passes
assert.frozen({}) // fails
assert.isFrozen(Object.freeze({})) // passes
assert.isFrozen({}) // fails
Name | Type |
---|---|
object |
any |
message? |
string |
Assert the object is not frozen.
assert.notFrozen({}) // passes
assert.notFrozen(Object.freeze({})) // fails
assert.isNotFrozen({}) // passes
assert.isNotFrozen(Object.freeze({})) // fails
Name | Type |
---|---|
object |
any |
message? |
string |
Assert the value to be a valid array
assert.isArray([]) // passes
assert.isArray({}) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value not to be an array
assert.isNotArray([]) // fails
assert.isNotArray({}) // passes
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is a boolean.
assert.isBoolean(true) // passes
assert.isBoolean(false) // passes
assert.isBoolean(1) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is anything, but not a boolean.
assert.isNotBoolean(1) // passes
assert.isNotBoolean(false) // fails
assert.isNotBoolean(true) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
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
Name | Type |
---|---|
value |
any |
message? |
string |
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
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is boolean (false)
assert.isFalse(false) // passes
assert.isFalse(true) // fails
assert.isFalse(0) // fails
assert.isFalse(null) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is anything but not false.
assert.isNotFalse(false) // fails
assert.isNotFalse(true) // passes
assert.isNotFalse(null) // passes
assert.isNotFalse(undefined) // passes
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value to be a number and no NaN or Infinity
assert.isFinite(1) // passes
assert.isFinite(Infinity) // fails
assert.isFinite(NaN) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is a function.
assert.isFunction(function foo () {}) // passes
assert.isFunction(() => {}) // passes
assert.isFunction(class Foo {}) // passes
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is not a function
assert.isNotFunction({}) // passes
assert.isNotFunction(null) // passes
assert.isNotFunction(() => {}) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
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
Name | Type |
---|---|
value |
any |
message? |
string |
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
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is null
assert.isNull(null) // passes
assert.isNull(true) // fails
assert.isNull(false) // fails
assert.isNull('foo') // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is anything but not null
assert.isNotNull(null) // fails
assert.isNotNull(true) // passes
assert.isNotNull(false) // passes
assert.isNotNull('foo') // passes
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value to be a valid number.
assert.isNumber(1) // passes
assert.isNumber(new Number('1')) // passes
assert.isNumber('1') // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value not to be a valid number.
assert.isNotNumber('1') // passes
assert.isNotNumber(1) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value to a valid object literal.
assert.isObject({}) // passes
assert.isObject(new SomeClass()) // passes
assert.isObject(null) // fails
assert.isObject([]) // fails
Name | Type |
---|---|
object |
any |
message? |
string |
Assert the value not to be an object literal.
assert.isNotObject(null) // passes
assert.isNotObject([]) // passes
assert.isNotObject({}) // fails
assert.isNotObject(new SomeClass()) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value to be a literal string.
assert.isString('') // passes
assert.isString(new String(true)) // passes
assert.isString(1) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value not to be a string literal.
assert.isNotString(1) // passes
assert.isNotString('') // fails
assert.isNotString(new String(true)) // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is a boolean (true).
assert.isTrue(true) // passes
assert.isTrue(false) // fails
assert.isTrue(1) // fails
assert.isTrue('foo') // fails
Name | Type |
---|---|
value |
any |
message? |
string |
Assert the value is anything, but not true.
assert.isNotTrue(true) // fails
assert.isNotTrue(false) // passes
assert.isNotTrue(1) // passes
assert.isNotTrue('foo') // passes
Name | Type |
---|---|
value |
any |
message? |
string |
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
Throw an error. Optionally accepts actual
and expected
values for the default error message.
:::note 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', '>')
Name | Type |
---|---|
message? |
string |
Name | Type |
---|---|
actual |
any |
expected |
any |
message? |
string |
operator? |
string |
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)
Name | Type |
---|---|
assertionsToExpect |
number |