UNPKG

3.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ErrorPredicate = void 0;
4const predicate_1 = require("./predicate");
5class ErrorPredicate extends predicate_1.Predicate {
6 /**
7 @hidden
8 */
9 constructor(options) {
10 super('error', options);
11 }
12 /**
13 Test an error to have a specific name.
14
15 @param expected - Expected name of the Error.
16 */
17 name(expected) {
18 return this.addValidator({
19 message: (error, label) => `Expected ${label} to have name \`${expected}\`, got \`${error.name}\``,
20 validator: error => error.name === expected
21 });
22 }
23 /**
24 Test an error to have a specific message.
25
26 @param expected - Expected message of the Error.
27 */
28 message(expected) {
29 return this.addValidator({
30 message: (error, label) => `Expected ${label} message to be \`${expected}\`, got \`${error.message}\``,
31 validator: error => error.message === expected
32 });
33 }
34 /**
35 Test the error message to include a specific message.
36
37 @param message - Message that should be included in the error.
38 */
39 messageIncludes(message) {
40 return this.addValidator({
41 message: (error, label) => `Expected ${label} message to include \`${message}\`, got \`${error.message}\``,
42 validator: error => error.message.includes(message)
43 });
44 }
45 /**
46 Test the error object to have specific keys.
47
48 @param keys - One or more keys which should be part of the error object.
49 */
50 hasKeys(...keys) {
51 return this.addValidator({
52 message: (_, label) => `Expected ${label} message to have keys \`${keys.join('`, `')}\``,
53 validator: error => keys.every(key => Object.prototype.hasOwnProperty.call(error, key))
54 });
55 }
56 /**
57 Test an error to be of a specific instance type.
58
59 @param instance - The expected instance type of the error.
60 */
61 instanceOf(instance) {
62 return this.addValidator({
63 message: (error, label) => `Expected ${label} \`${error.name}\` to be of type \`${instance.name}\``,
64 validator: error => error instanceof instance
65 });
66 }
67 /**
68 Test an Error to be a TypeError.
69 */
70 get typeError() {
71 return this.instanceOf(TypeError);
72 }
73 /**
74 Test an Error to be an EvalError.
75 */
76 get evalError() {
77 return this.instanceOf(EvalError);
78 }
79 /**
80 Test an Error to be a RangeError.
81 */
82 get rangeError() {
83 return this.instanceOf(RangeError);
84 }
85 /**
86 Test an Error to be a ReferenceError.
87 */
88 get referenceError() {
89 return this.instanceOf(ReferenceError);
90 }
91 /**
92 Test an Error to be a SyntaxError.
93 */
94 get syntaxError() {
95 return this.instanceOf(SyntaxError);
96 }
97 /**
98 Test an Error to be a URIError.
99 */
100 get uriError() {
101 return this.instanceOf(URIError);
102 }
103}
104exports.ErrorPredicate = ErrorPredicate;