You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.5 KiB

import {Predicate, Context} from './predicate';
export class ErrorPredicate extends Predicate<Error> {
constructor(context?: Context) {
super('error', context);
}
/**
* Test an error to have a specific name.
*
* @param expected Expected name of the Error.
*/
name(expected: string) {
return this.addValidator({
message: error => `Expected error to have name \`${expected}\`, got \`${error.name}\``,
validator: error => error.name === expected
});
}
/**
* Test an error to have a specific message.
*
* @param expected Expected message of the Error.
*/
message(expected: string) {
return this.addValidator({
message: error => `Expected error message to be \`${expected}\`, got \`${error.message}\``,
validator: error => error.message === expected
});
}
/**
* Test the error message to include a specific message.
*
* @param message Message that should be included in the error.
*/
messageIncludes(message: string) {
return this.addValidator({
message: error => `Expected error message to include \`${message}\`, got \`${error.message}\``,
validator: error => error.message.includes(message)
});
}
/**
* Test the error object to have specific keys.
*
* @param keys One or more keys which should be part of the error object.
*/
hasKeys(...keys: string[]) {
return this.addValidator({
message: () => `Expected error message to have keys \`${keys.join('`, `')}\``,
validator: error => keys.every(key => error.hasOwnProperty(key))
});
}
/**
* Test an error to be of a specific instance type.
*
* @param instance The expected instance type of the error.
*/
instanceOf(instance: any) {
return this.addValidator({
message: error => `Expected \`${error.name}\` to be of type \`${instance.name}\``,
validator: error => error instanceof instance
});
}
/**
* Test an Error to be a TypeError.
*/
get typeError() {
return this.instanceOf(TypeError);
}
/**
* Test an Error to be an EvalError.
*/
get evalError() {
return this.instanceOf(EvalError);
}
/**
* Test an Error to be a RangeError.
*/
get rangeError() {
return this.instanceOf(RangeError);
}
/**
* Test an Error to be a ReferenceError.
*/
get referenceError() {
return this.instanceOf(ReferenceError);
}
/**
* Test an Error to be a SyntaxError.
*/
get syntaxError() {
return this.instanceOf(SyntaxError);
}
/**
* Test an Error to be a URIError.
*/
get uriError() {
return this.instanceOf(URIError);
}
}