These assertions test Error
objects and their properties. If the subject looks like an Error
but is not an instance of Error
, these assertions will be unavailable.
✏️ Aliases:
{unknown} to be an Error {unknown} to be a Error
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/);