These are the most basic expectations about the type of a subject.
Success:
expect('hello, world!', 'to be a string');
Failure:
expect(42, 'to be a string');
// AssertionError: Expected 42 to be a string
Negation:
expect(42, 'not to be a string');
Aliases:
to be a boolean
,to be a bool
,to be boolean
Success:
expect(true, 'to be a boolean');
expect(false, 'to be a boolean');
Failure:
expect(0, 'to be a boolean');
// AssertionError: Expected 0 to be a boolean
Negation:
expect(0, 'not to be a boolean');
Aliases:
to be a number
,to be finite
The definition of "number" is that of Zod v4's; only finite numbers are considered valid.
Success:
expect(3.14, 'to be a number');
expect(-42, 'to be a number');
expect(0, 'to be a number');
Failure:
expect(NaN, 'to be a number');
// AssertionError: Expected NaN to be a number
expect(Infinity, 'to be a number');
Negation:
expect(NaN, 'not to be a number');
expect(Infinity, 'not to be a number');
Success:
expect(9007199254741991n, 'to be a bigint');
Failure:
expect(42, 'to be a bigint');
// AssertionError: Expected 42 to be a bigint
Negation:
expect(42, 'not to be a bigint');
Success:
expect(Symbol('foo'), 'to be a symbol');
Failure:
expect('foo', 'to be a symbol');
// AssertionError: Expected 'foo' to be a symbol
Negation:
expect('foo', 'not to be a symbol');
Success:
expect(null, 'to be null');
Failure:
expect(undefined, 'to be null');
// AssertionError: Expected undefined to be null
Negation:
expect(undefined, 'not to be null');
Success:
expect(undefined, 'to be undefined');
Failure:
expect(null, 'to be undefined');
// AssertionError: Expected null to be undefined
Negation:
expect(null, 'not to be undefined');
Success:
expect('hello', 'to be a primitive');
expect(42, 'to be a primitive');
expect(true, 'to be a primitive');
expect(null, 'to be a primitive');
expect(undefined, 'to be a primitive');
expect(Symbol('test'), 'to be a primitive');
expect(123n, 'to be a primitive');
Failure:
expect({}, 'to be a primitive');
// AssertionError: Expected {} to be a primitive
expect([], 'to be a primitive');
Negation:
expect({}, 'not to be a primitive');
expect([], 'not to be a primitive');
These assertions test numeric values, ranges, and mathematical relationships.
👉 See to be a number
Success:
expect(Infinity, 'to be infinite');
expect(-Infinity, 'to be infinite');
Failure:
expect(42, 'to be infinite');
// AssertionError: Expected 42 to be infinite
Negation:
expect(42, 'not to be infinite');
Success:
expect(Infinity, 'to be Infinity');
Failure:
expect(-Infinity, 'to be Infinity');
// AssertionError: Expected -Infinity to be Infinity
Negation:
expect(-Infinity, 'not to be Infinity');
Success:
expect(-Infinity, 'to be -Infinity');
Failure:
expect(Infinity, 'to be -Infinity');
// AssertionError: Expected Infinity to be -Infinity
Negation:
expect(Infinity, 'not to be -Infinity');
Aliases:
to be positive
,to be a positive number
Success:
expect(42, 'to be positive');
expect(3.14, 'to be positive');
Failure:
expect(-5, 'to be positive');
// AssertionError: Expected -5 to be positive
expect(0, 'to be positive');
Negation:
expect(-5, 'not to be positive');
expect(0, 'not to be positive');
Aliases:
to be a positive integer
,to be a positive int
Success:
expect(42, 'to be a positive integer');
expect(1, 'to be a positive integer');
Failure:
expect(3.14, 'to be a positive integer');
// AssertionError: Expected 3.14 to be a positive integer
expect(-5, 'to be a positive integer');
Negation:
expect(3.14, 'not to be a positive integer');
Aliases:
to be negative
,to be a negative number
Success:
expect(-42, 'to be negative');
expect(-3.14, 'to be negative');
Failure:
expect(5, 'to be negative');
// AssertionError: Expected 5 to be negative
expect(0, 'to be negative');
Negation:
expect(5, 'not to be negative');
expect(0, 'not to be negative');
Aliases:
to be a negative integer
,to be a negative int
Success:
expect(-42, 'to be a negative integer');
expect(-1, 'to be a negative integer');
Failure:
expect(-3.14, 'to be a negative integer');
// AssertionError: Expected -3.14 to be a negative integer
expect(5, 'to be a negative integer');
Negation:
expect(-3.14, 'not to be a negative integer');
Success:
expect(NaN, 'to be NaN');
expect(Number.NaN, 'to be NaN');
Failure:
expect(42, 'to be NaN');
// AssertionError: Expected 42 to be NaN
Negation:
expect(42, 'not to be NaN');
Aliases:
to be an integer
,to be a safe integer
,to be an int
,to be a safe int
Success:
expect(42, 'to be an integer');
expect(-17, 'to be an integer');
expect(0, 'to be an integer');
Failure:
expect(3.14, 'to be an integer');
// AssertionError: Expected 3.14 to be an integer
Negation:
expect(3.14, 'not to be an integer');
Success:
expect(10, 'to be greater than', 5);
expect(3.14, 'to be greater than', 3);
Failure:
expect(5, 'to be greater than', 10);
// AssertionError: Expected 5 to be greater than 10
Negation:
expect(5, 'not to be greater than', 10);
Success:
expect(5, 'to be less than', 10);
expect(3, 'to be less than', 3.14);
Failure:
expect(10, 'to be less than', 5);
// AssertionError: Expected 10 to be less than 5
Negation:
expect(10, 'not to be less than', 5);
Aliases:
to be greater than or equal to <number>
,to be at least <number>
Success:
expect(10, 'to be greater than or equal to', 10);
expect(15, 'to be at least', 10);
Failure:
expect(5, 'to be greater than or equal to', 10);
// AssertionError: Expected 5 to be greater than or equal to 10
Negation:
expect(5, 'not to be greater than or equal to', 10);
Aliases:
to be less than or equal to <number>
,to be at most <number>
Success:
expect(10, 'to be less than or equal to', 10);
expect(5, 'to be at most', 10);
Failure:
expect(15, 'to be less than or equal to', 10);
// AssertionError: Expected 15 to be less than or equal to 10
Negation:
expect(15, 'not to be less than or equal to', 10);
Aliases:
to be within <number> and <number>
,to be between <number> and <number>
Success:
expect(5, 'to be within', 1, 10);
expect(7.5, 'to be between', 7, 8);
Failure:
expect(15, 'to be within', 1, 10);
// AssertionError: Expected 15 to be within 1 and 10
Negation:
expect(15, 'not to be within', 1, 10);
Success:
expect(1.0, 'to be close to', 1.1, 0.2);
expect(3.14159, 'to be close to', 3.14, 0.01);
Failure:
expect(1.0, 'to be close to', 2.0, 0.5);
// AssertionError: Expected 1.0 to be close to 2.0 within 0.5
Negation:
expect(1.0, 'not to be close to', 2.0, 0.5);
These assertions are the odd ducks. We don't have the mental stamina to categorize them properly.
Aliases:
to be truthy
,to exist
,to be ok
Success:
expect(1, 'to be truthy');
expect('hello', 'to be truthy');
expect(true, 'to exist');
expect({}, 'to be ok');
expect([], 'to exist');
Failure:
expect(0, 'to be truthy');
// AssertionError: Expected 0 to be truthy
expect('', 'to exist');
expect(false, 'to be ok');
expect(null, 'to exist');
expect(undefined, 'to be truthy');
Negation:
expect(0, 'not to be truthy');
expect('', 'not to exist');
expect(false, 'not to be ok');
Success:
expect(0, 'to be falsy');
expect('', 'to be falsy');
expect(false, 'to be falsy');
expect(null, 'to be falsy');
expect(undefined, 'to be falsy');
expect(NaN, 'to be falsy');
Failure:
expect(1, 'to be falsy');
// AssertionError: Expected 1 to be falsy
expect('hello', 'to be falsy');
expect(true, 'to be falsy');
Negation:
expect(1, 'not to be falsy');
expect('hello', 'not to be falsy');
expect(true, 'not to be falsy');
Success:
expect(0, 'to be defined');
expect('', 'to be defined');
expect(false, 'to be defined');
expect(null, 'to be defined');
Failure:
expect(undefined, 'to be defined');
// AssertionError: Expected undefined to be defined
Negation:
expect(undefined, 'not to be defined');
These assertions test strings, regular expressions, and pattern matching.
👉 See to be a string
Aliases:
to be a RegExp
,to be a regex
,to be a regexp
Success:
expect(/hello/, 'to be a RegExp');
expect(new RegExp('world'), 'to be a regex');
expect(/[a-z]+/i, 'to be a regexp');
Failure:
expect('hello', 'to be a RegExp');
// AssertionError: Expected 'hello' to be a RegExp
Negation:
expect('hello', 'not to be a RegExp');
Aliases:
to begin with <string>
,to start with <string>
Success:
expect('hello world', 'to begin with', 'hello');
expect('JavaScript', 'to start with', 'Java');
Failure:
expect('hello world', 'to begin with', 'world');
// AssertionError: Expected 'hello world' to begin with 'world'
Negation:
expect('hello world', 'not to begin with', 'world');
Success:
expect('hello world', 'to end with', 'world');
expect('test.js', 'to end with', '.js');
Failure:
expect('hello world', 'to end with', 'hello');
// AssertionError: Expected 'hello world' to end with 'hello'
Negation:
expect('hello world', 'not to end with', 'hello');
Success:
expect('hello123', 'to match', /\d+/);
expect('JavaScript', 'to match', /^Java/);
expect('test@example.com', 'to match', /@/);
Failure:
expect('hello', 'to match', /\d+/);
// AssertionError: Expected 'hello' to match /\d+/
Negation:
expect('hello', 'not to match', /\d+/);
Success:
expect('', 'to be empty');
Failure:
expect('hello', 'to be empty');
// AssertionError: Expected 'hello' to be empty
Negation:
expect('hello', 'not to be empty');
Success:
expect('hello', 'to be non-empty');
expect(' ', 'to be non-empty'); // Whitespace counts as non-empty
Failure:
expect('', 'to be non-empty');
// AssertionError: Expected '' to be non-empty
Negation:
PRO TIP: Use "to be empty" instead.
expect('', 'not to be non-empty');
Aliases:
includes <string>
,contains <string>
,to include <string>
,to contain <string>
Success:
expect('hello world', 'includes', 'world');
expect('JavaScript', 'contains', 'Script');
expect([1, 2, 3], 'to include', 2);
expect('test string', 'to contain', 'string');
Failure:
expect('hello', 'includes', 'world');
// AssertionError: Expected 'hello' to include 'world'
Negation:
expect('hello', 'not to include', 'world');
These assertions test collections like arrays, Maps, Sets, and their properties.
Aliases:
to be an array
,to be array
Success:
expect([], 'to be an array');
expect([1, 2, 3], 'to be an array');
expect(new Array(5), 'to be an array');
Failure:
expect('hello', 'to be an array');
// AssertionError: Expected 'hello' to be an array
Negation:
expect('hello', 'not to be an array');
Success:
expect([], 'to be empty');
Failure:
expect([1, 2, 3], 'to be empty');
// AssertionError: Expected [1, 2, 3] to be empty
Negation:
expect([1, 2, 3], 'not to be empty');
Success:
expect([1, 2, 3], 'to have length', 3);
expect('hello', 'to have length', 5);
Failure:
expect([1, 2], 'to have length', 3);
// AssertionError: Expected [1, 2] to have length 3
Negation:
expect([1, 2], 'not to have length', 3);
Success:
expect([1, 2, 3], 'to be non-empty');
expect('hello', 'to be non-empty');
Failure:
expect([], 'to be non-empty');
// AssertionError: Expected [] to be non-empty
Negation:
expect([], 'not to be non-empty');
Aliases:
to contain <any>
,to include <any>
Success:
expect([1, 2, 3], 'to contain', 2);
expect(['a', 'b', 'c'], 'to include', 'b');
Failure:
expect([1, 2, 3], 'to contain', 5);
// AssertionError: Expected [1, 2, 3] to contain 5
Negation:
expect([1, 2, 3], 'not to contain', 5);
Aliases:
to contain <any>
,to include <any>
Success:
const map = new Map([
['key1', 'value1'],
['key2', 'value2'],
]);
expect(map, 'to contain', 'key1');
expect(map, 'to include', 'key2');
Failure:
const map = new Map([['key1', 'value1']]);
expect(map, 'to contain', 'key3');
// AssertionError: Expected Map to contain 'key3'
Negation:
expect(map, 'not to contain', 'key3');
Success:
const map = new Map([
['a', 1],
['b', 2],
]);
expect(map, 'to have size', 2);
Failure:
const map = new Map([['a', 1]]);
expect(map, 'to have size', 2);
// AssertionError: Expected Map to have size 2
Negation:
expect(map, 'not to have size', 2);
Success:
expect(new Map(), 'to be empty');
Failure:
const map = new Map([['a', 1]]);
expect(map, 'to be empty');
// AssertionError: Expected Map to be empty
Negation:
expect(map, 'not to be empty');
Aliases:
to contain <any>
,to include <any>
Success:
const set = new Set([1, 2, 3]);
expect(set, 'to contain', 2);
expect(set, 'to include', 3);
Failure:
const set = new Set([1, 2, 3]);
expect(set, 'to contain', 5);
// AssertionError: Expected Set to contain 5
Negation:
expect(set, 'not to contain', 5);
Success:
const set = new Set([1, 2, 3]);
expect(set, 'to have size', 3);
Failure:
const set = new Set([1, 2]);
expect(set, 'to have size', 3);
// AssertionError: Expected Set to have size 3
Negation:
expect(set, 'not to have size', 3);
Success:
expect(new Set(), 'to be empty');
Failure:
const set = new Set([1, 2, 3]);
expect(set, 'to be empty');
// AssertionError: Expected Set to be empty
Negation:
expect(set, 'not to be empty');
Success:
expect(new Set(), 'to be a Set');
expect(new Set([1, 2, 3]), 'to be a Set');
Failure:
expect([1, 2, 3], 'to be a Set');
// AssertionError: Expected [1, 2, 3] to be a Set
Negation:
expect([1, 2, 3], 'not to be a Set');
Aliases:
to contain <any>
,to include <any>
Success:
const obj = {};
const wm = new WeakMap([[obj, 'value']]);
expect(wm, 'to contain', obj);
Failure:
const obj1 = {},
obj2 = {};
const wm = new WeakMap([[obj1, 'value']]);
expect(wm, 'to contain', obj2);
// AssertionError: Expected WeakMap to contain object
Negation:
expect(wm, 'not to contain', obj2);
Success:
expect(new WeakMap(), 'to be a WeakMap');
Failure:
expect(new Map(), 'to be a WeakMap');
// AssertionError: Expected Map to be a WeakMap
Negation:
expect(new Map(), 'not to be a WeakMap');
Aliases:
to contain <any>
,to include <any>
Success:
const obj = {};
const ws = new WeakSet([obj]);
expect(ws, 'to contain', obj);
Failure:
const obj1 = {},
obj2 = {};
const ws = new WeakSet([obj1]);
expect(ws, 'to contain', obj2);
// AssertionError: Expected WeakSet to contain object
Negation:
expect(ws, 'not to contain', obj2);
Success:
expect(new WeakSet(), 'to be a WeakSet');
Failure:
expect(new Set(), 'to be a WeakSet');
// AssertionError: Expected Set to be a WeakSet
Negation:
expect(new Set(), 'not to be a WeakSet');
These assertions test objects, their properties, and object-specific behaviors.
Success:
expect({}, 'to be an object');
expect({ a: 1 }, 'to be an object');
expect([], 'to be an object'); // Arrays are objects
expect(new Date(), 'to be an object');
Failure:
expect('hello', 'to be an object');
// AssertionError: Expected 'hello' to be an object
expect(null, 'to be an object');
Negation:
expect('hello', 'not to be an object');
expect(null, 'not to be an object');
Aliases:
to be a record
,to be a plain object
Success:
expect({}, 'to be a record');
expect({ a: 1, b: 2 }, 'to be a plain object');
Failure:
expect([], 'to be a record');
// AssertionError: Expected [] to be a record
expect(new Date(), 'to be a record');
Negation:
expect([], 'not to be a record');
Success:
expect({}, 'to be empty');
Failure:
expect({ a: 1 }, 'to be empty');
// AssertionError: Expected { a: 1 } to be empty
Negation:
expect({ a: 1 }, 'not to be empty');
Aliases:
to have keys <array>
,to have properties <array>
,to have props <array>
Success:
expect({ a: 1, b: 2 }, 'to have keys', ['a', 'b']);
expect({ name: 'John', age: 30 }, 'to have properties', ['name', 'age']);
Failure:
expect({ a: 1 }, 'to have keys', ['a', 'b']);
// AssertionError: Expected { a: 1 } to have keys ['a', 'b']
Negation:
expect({ a: 1 }, 'not to have keys', ['a', 'b']);
Success:
const obj = Object.create(null);
expect(obj, 'to have a null prototype');
Failure:
expect({}, 'to have a null prototype');
// AssertionError: Expected {} to have a null prototype
Negation:
expect({}, 'not to have a null prototype');
Success:
const obj = { a: 1, b: 2 };
expect('a', 'to be an enumerable property of', obj);
Failure:
const obj = { a: 1 };
Object.defineProperty(obj, 'b', { value: 2, enumerable: false });
expect('b', 'to be an enumerable property of', obj);
// AssertionError: Expected 'b' to be an enumerable property of object
Negation:
expect('b', 'not to be an enumerable property of', obj);
Success:
const obj = { a: 1 };
Object.seal(obj);
expect(obj, 'to be sealed');
Failure:
expect({}, 'to be sealed');
// AssertionError: Expected {} to be sealed
Negation:
expect({}, 'not to be sealed');
Success:
const obj = { a: 1 };
Object.freeze(obj);
expect(obj, 'to be frozen');
Failure:
expect({}, 'to be frozen');
// AssertionError: Expected {} to be frozen
Negation:
expect({}, 'not to be frozen');
Success:
expect({}, 'to be extensible');
Failure:
const obj = {};
Object.preventExtensions(obj);
expect(obj, 'to be extensible');
// AssertionError: Expected {} to be extensible
Negation:
expect(obj, 'not to be extensible');
Aliases:
to satisfy <object>
,to be like <object>
"To satisfy" is a wonky special loose "deep equal" assertion. It is similar to AVA's t.like()
or Jest's expect.objectContaining()
. It checks that the actual object contains at least the properties and values specified in the expected object. It ignores any additional properties.
In addition, any regular expression in a property value position will be used to match the corresponding actual value (which will be coerced into a string). This makes it easy to assert that a string property contains a substring, starts with a prefix, or matches some other pattern.
Success:
expect({ a: 1, b: 2, c: 3 }, 'to satisfy', { a: 1, b: 2 });
expect({ name: 'John', age: 30 }, 'to be like', { name: 'John' });
// Using regular expressions in property values
expect(
{
email: 'user@example.com',
phone: '+1-555-0123',
id: 12345,
},
'to satisfy',
{
email: /^user@/,
phone: /^\+1-555/,
id: /123/,
},
);
Failure:
expect({ a: 1 }, 'to satisfy', { a: 1, b: 2 });
// AssertionError: Expected { a: 1 } to satisfy { a: 1, b: 2 }
Negation:
expect({ a: 1 }, 'not to satisfy', { a: 1, b: 2 });
These assertions test functions, their behavior, and properties.
Success:
expect(function () {}, 'to be a function');
expect(() => {}, 'to be a function');
expect(Math.max, 'to be a function');
Failure:
expect('hello', 'to be a function');
// AssertionError: Expected 'hello' to be a function
Negation:
expect('hello', 'not to be a function');
Success:
expect(async function () {}, 'to be an async function');
expect(async () => {}, 'to be an async function');
Failure:
expect(function () {}, 'to be an async function');
// AssertionError: Expected function to be an async function
Negation:
expect(function () {}, 'not to be an async function');
Aliases:
to be a class
,to be a constructor
⚠️ Warning!
It is currently (as of September 2025) not possible to reliably distinguish between classes and regular functions in JavaScript. We can only tell if a function is constructable (i.e., can be called with
new
); we cannot determine if it is specifically a class constructor.Use with caution.
Success:
class MyClass {}
expect(MyClass, 'to be a class');
expect(Date, 'to be a constructor');
Failure:
const fn = function () {};
expect(fn, 'to be a class');
// AssertionError: Expected function to be a class
Negation:
expect(fn, 'not to be a class');
Success:
function add(a, b) {
return a + b;
}
expect(add, 'to have arity', 2);
const multiply = (x, y, z) => x * y * z;
expect(multiply, 'to have arity', 3);
Failure:
function greet(name) {
return `Hello, ${name}!`;
}
expect(greet, 'to have arity', 2);
// AssertionError: Expected function to have arity 2
Negation:
expect(greet, 'not to have arity', 2);
This assertion optionally accepts a parameter which must be a string
, RegExp
or object
:
string
, matches the message
prop of the thrown Error exactly; if a non-error was thrown, the value is coerced to a string and matched directly.RegExp
, tests the message
prop of the thrown Error; if a non-error was thrown, the value is coerced to a string and tested directly.object
, "to satisfy" semantics are used.Success:
expect(() => {
throw new Error('Something went wrong');
}, 'to throw');
expect(() => {
JSON.parse('invalid json');
}, 'to throw');
// String matching
expect(
() => {
throw new Error('Specific error message');
},
'to throw',
'Specific error message',
);
// RegExp matching
expect(
() => {
throw new Error('Error: Something failed');
},
'to throw',
/Something failed/,
);
// Object matching
expect(
() => {
throw new Error('Custom error');
},
'to throw',
{ message: 'Custom error' },
);
Failure:
expect(() => {
return 'all good';
}, 'to throw');
// AssertionError: Expected function to throw
expect(
() => {
throw new Error('Different message');
},
'to throw',
'Specific error message',
);
// AssertionError: Expected function to throw 'Specific error message'
Negation:
expect(() => {
return 'all good';
}, 'not to throw');
expect(
() => {
throw new Error('Different message');
},
'not to throw',
'Specific error message',
);
Aliases:
to throw a <constructor>
,to throw an <constructor>
Success:
expect(
() => {
throw new TypeError('Type error');
},
'to throw a',
TypeError,
);
expect(
() => {
throw new RangeError('Range error');
},
'to throw an',
Error,
); // RangeError extends Error
Failure:
expect(
() => {
throw new TypeError('Type error');
},
'to throw a',
RangeError,
);
// AssertionError: Expected function to throw a RangeError
Negation:
expect(
() => {
throw new TypeError('Type error');
},
'not to throw a',
RangeError,
);
Aliases:
to throw a <error> satisfying <object>
,to throw an <error> satisfying <object>
This assertion is a combination of "to throw a" and "to throw" (with an object parameter).
Success:
expect(
() => {
const err = new Error('Custom error');
err.code = 'CUSTOM_CODE';
throw err;
},
'to throw a',
Error,
'satisfying',
{ code: 'CUSTOM_CODE' },
);
Failure:
expect(
() => {
throw new Error('Simple error');
},
'to throw a',
Error,
'satisfying',
{ code: 'MISSING_CODE' },
);
// AssertionError: Expected thrown error to satisfy { code: 'MISSING_CODE' }
Negation:
expect(
() => {
throw new Error('Simple error');
},
'not to throw a',
Error,
'satisfying',
{ code: 'MISSING_CODE' },
);
These assertions test equality, identity, and value comparisons.
Aliases:
to be
,to equal
,equals
,is
,is equal to
,to strictly equal
Success:
expect(42, 'to be', 42);
expect('hello', 'to equal', 'hello');
expect(true, 'is', true);
expect(null, 'is equal to', null);
Failure:
expect(42, 'to be', '42');
// AssertionError: Expected 42 to be '42'
expect({}, 'to equal', {});
// AssertionError: Expected {} to equal {}
Negation:
expect(42, 'not to be', '42');
expect({}, 'not to equal', {});
Aliases:
to deep equal
,to deeply equal
Success:
expect({ a: 1, b: 2 }, 'to deep equal', { a: 1, b: 2 });
expect([1, 2, 3], 'to deeply equal', [1, 2, 3]);
expect({ nested: { value: 42 } }, 'to deep equal', { nested: { value: 42 } });
Failure:
expect({ a: 1 }, 'to deep equal', { a: 1, b: 2 });
// AssertionError: Expected { a: 1 } to deep equal { a: 1, b: 2 }
Negation:
expect({ a: 1 }, 'not to deep equal', { a: 1, b: 2 });
Success:
expect(2, 'to be one of', [1, 2, 3]);
expect('blue', 'to be one of', ['red', 'green', 'blue']);
Failure:
expect(5, 'to be one of', [1, 2, 3]);
// AssertionError: Expected 5 to be one of [1, 2, 3]
Negation:
expect(5, 'not to be one of', [1, 2, 3]);
Aliases:
to be an instance of <constructor>
,to be a <constructor>
Success:
expect(new Date(), 'to be an instance of', Date);
expect([], 'to be a', Array);
expect('hello', 'to be an instance of', String); // Note: primitive strings work too
Failure:
expect('hello', 'to be an instance of', Number);
// AssertionError: Expected 'hello' to be an instance of Number
Negation:
expect('hello', 'not to be an instance of', Number);
Aliases:
to be a <constructor>
,to be an <constructor>
Success:
expect(new Date(), 'to be a', Date);
expect(new Error(), 'to be an', Error);
expect([], 'to be an', Array);
Failure:
expect('hello', 'to be a', Number);
// AssertionError: Expected 'hello' to be a Number
Negation:
expect('hello', 'not to be a', Number);
These assertions test Error objects and their properties.
Success:
expect(new Error(), 'to be an Error');
expect(new TypeError(), 'to be an Error');
expect(new RangeError('Invalid range'), 'to be an Error');
Failure:
expect('error message', 'to be an Error');
// AssertionError: Expected 'error message' to be an Error
Negation:
expect('error message', 'not to be an Error');
Success:
const error = new Error('Something went wrong');
expect(error, 'to have message', 'Something went wrong');
const typeError = new TypeError('Invalid type');
expect(typeError, 'to have message', 'Invalid type');
Failure:
const error = new Error('Actual message');
expect(error, 'to have message', 'Expected message');
// AssertionError: Expected Error to have message 'Expected message'
Negation:
expect(error, 'not to have message', 'Expected message');
Success:
const error = new Error('File not found: /path/to/file.txt');
expect(error, 'to have message matching', /File not found/);
expect(error, 'to have message matching', /\.txt$/);
Failure:
const error = new Error('Something went wrong');
expect(error, 'to have message matching', /File not found/);
// AssertionError: Expected Error message to match /File not found/
Negation:
expect(error, 'not to have message matching', /File not found/);
These assertions test Date objects and time-related values.
Aliases:
to be a date
,to be a Date
Success:
expect(new Date(), 'to be a date');
expect(new Date('2024-01-01'), 'to be a Date');
expect(new Date(Date.now()), 'to be a date');
Failure:
expect('2024-01-01', 'to be a date');
// AssertionError: Expected '2024-01-01' to be a date
expect(1704067200000, 'to be a Date'); // Unix timestamp
Negation:
expect('2024-01-01', 'not to be a date');
expect(1704067200000, 'not to be a Date');
These assertions test Promises and asynchronous operations. Use expectAsync()
for these assertions.
Aliases:
to resolve
,to fulfill
Success:
await expectAsync(Promise.resolve(42), 'to resolve');
await expectAsync(Promise.resolve('success'), 'to fulfill');
// With async functions
await expectAsync(async () => 'result', 'to resolve');
Failure:
await expectAsync(Promise.reject('error'), 'to resolve');
// AssertionError: Expected Promise to resolve
Negation:
await expectAsync(Promise.reject('error'), 'not to resolve');
Success:
await expectAsync(Promise.reject('error'), 'to reject');
await expectAsync(Promise.reject(new Error('failed')), 'to reject');
// With async functions
await expectAsync(async () => {
throw new Error('async error');
}, 'to reject');
Failure:
await expectAsync(Promise.resolve(42), 'to reject');
// AssertionError: Expected Promise to reject
Negation:
await expectAsync(Promise.resolve(42), 'not to reject');
Aliases:
to reject with a <constructor>
,to reject with an <constructor>
Success:
await expectAsync(
Promise.reject(new TypeError('Type error')),
'to reject with a',
TypeError,
);
await expectAsync(
Promise.reject(new Error('Generic error')),
'to reject with an',
Error,
);
Failure:
await expectAsync(
Promise.reject(new TypeError('Type error')),
'to reject with a',
RangeError,
);
// AssertionError: Expected Promise to reject with a RangeError
Negation:
await expectAsync(
Promise.reject(new TypeError('Type error')),
'not to reject with a',
RangeError,
);
Success:
// String matching
await expectAsync(
Promise.reject(new Error('Specific error')),
'to reject with',
'Specific error',
);
// RegExp matching
await expectAsync(
Promise.reject(new Error('Error: Something failed')),
'to reject with',
/Something failed/,
);
// Object matching
await expectAsync(
Promise.reject({ message: 'Custom error', code: 500 }),
'to reject with',
{ message: 'Custom error' },
);
Failure:
await expectAsync(
Promise.reject(new Error('Different error')),
'to reject with',
'Specific error',
);
// AssertionError: Expected Promise to reject with 'Specific error'
Negation:
await expectAsync(
Promise.reject(new Error('Different error')),
'not to reject with',
'Specific error',
);
Aliases:
to fulfill with value satisfying <string | RegExp | object>
,to resolve to value satisfying <string | RegExp | object>
Success:
// String matching
await expectAsync(
Promise.resolve('Hello World'),
'to fulfill with value satisfying',
'Hello World',
);
// RegExp matching
await expectAsync(
Promise.resolve('Success: Operation completed'),
'to resolve to value satisfying',
/Success/,
);
// Object matching
await expectAsync(
Promise.resolve({ status: 'ok', data: [1, 2, 3] }),
'to fulfill with value satisfying',
{ status: 'ok' },
);
Failure:
await expectAsync(
Promise.resolve('Different value'),
'to fulfill with value satisfying',
'Expected value',
);
// AssertionError: Expected Promise to resolve to value satisfying 'Expected value'
Negation:
await expectAsync(
Promise.resolve('Different value'),
'not to fulfill with value satisfying',
'Expected value',
);