to error
- <function> to error [with] <any>
- <function> to error
Asserts that the function throws an error, or returns a promise that
is rejected.
function willBeRejected() {
return expect.promise(function (resolve, reject) {
reject(new Error('The reject message'));
});
}
function willThrow() {
throw new Error('The error message');
}
expect(willBeRejected, 'to error');
expect(willThrow, 'to error');
In case of a failing expectation you get the following output:
function willNotBeRejected() {
return expect.promise(function (resolve, reject) {
resolve('Hello world');
});
}
expect(willNotBeRejected, 'to error');
expected
function willNotBeRejected() {
return expect.promise(function (resolve, reject) {
resolve('Hello world');
});
}
to error
You can assert the error message is a given string if you provide a
string as the second parameter.
expect(willBeRejected, 'to error', 'The reject message');
expect(willBeRejected, 'to error', 'The error message');
expected
function willBeRejected() {
return expect.promise(function (resolve, reject) {
reject(new Error('The reject message'));
});
}
to error 'The error message'
expected Error('The reject message') to satisfy 'The error message'
By providing a regular expression as the second parameter you can
assert the error message matches the given regular expression.
expect(willBeRejected, 'to error', /reject message/);
In case of a failing expectation you get the following output:
expect(willBeRejected, 'to error', /error message/);
expected
function willBeRejected() {
return expect.promise(function (resolve, reject) {
reject(new Error('The reject message'));
});
}
to error /error message/
expected Error('The reject message') to satisfy /error message/
You can also negate the check, and verify that the function will not
error out. When negating the assertion, you cannot provide a message.
expect(willNotBeRejected, 'not to error');
In case of a failing expectation you get the following output:
expect(willBeRejected, 'not to error');
expected
function willBeRejected() {
return expect.promise(function (resolve, reject) {
reject(new Error('The reject message'));
});
}
not to error
returned promise rejected with: Error('The reject message')
You can pass in a function instead of the error message, and do more
assertions on the error.
function willBeRejectedAsync() {
return expect.promise(function (resolve, reject) {
setTimeout(function () {
reject(new Error('async error'));
}, 1);
});
}
return expect(willBeRejectedAsync, 'to error', function (e) {
return expect(e.message, 'to equal', 'async error');
});
You can even do async assertions in the function that you pass in.
var errorCount = 0;
function willBeRejectedAsync() {
return expect.promise(function (resolve, reject) {
setTimeout(function () {
var error = new Error('async error');
errorCount += 1;
error.errorCount = errorCount;
reject(error);
}, 1);
});
}
return expect(willBeRejectedAsync, 'to error', function (e) {
return expect(willBeRejectedAsync, 'to error', function (e2) {
return expect(e2.errorCount, 'to be greater than', e.errorCount);
});
});