|
5 | 5 | * MIT Licensed
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -import {config} from './config.js'; |
9 |
| -import {AssertionError} from 'assertion-error'; |
10 |
| -import * as util from './utils/index.js'; |
| 8 | +import { config } from "./config.js"; |
| 9 | +import { AssertionError } from "assertion-error"; |
| 10 | +import * as util from "./utils/index.js"; |
11 | 11 |
|
12 | 12 | /**
|
13 |
| - * Assertion Constructor |
14 |
| - * |
15 | 13 | * Creates object for chaining.
|
16 |
| - * |
17 | 14 | * `Assertion` objects contain metadata in the form of flags. Three flags can
|
18 | 15 | * be assigned during instantiation by passing arguments to this constructor:
|
19 | 16 | *
|
@@ -42,139 +39,161 @@ import * as util from './utils/index.js';
|
42 | 39 | *
|
43 | 40 | * - `eql`: This flag contains the deepEqual function to be used by the assertion.
|
44 | 41 | *
|
45 |
| - * @param {unknown} obj target of the assertion |
46 |
| - * @param {string} msg (optional) custom error message |
47 |
| - * @param {Function} ssfi (optional) starting point for removing stack frames |
48 |
| - * @param {boolean} lockSsfi (optional) whether or not the ssfi flag is locked |
| 42 | + * @param {unknown} ?obj target of the assertion |
| 43 | + * @param {string} ?msg (optional) custom error message |
| 44 | + * @param {Function} ?ssfi (optional) starting point for removing stack frames |
| 45 | + * @param {boolean} ?lockSsfi (optional) whether or not the ssfi flag is locked |
49 | 46 | * @returns {unknown}
|
50 |
| - * @private |
51 | 47 | */
|
52 |
| -export function Assertion(obj, msg, ssfi, lockSsfi) { |
53 |
| - util.flag(this, 'ssfi', ssfi || Assertion); |
54 |
| - util.flag(this, 'lockSsfi', lockSsfi); |
55 |
| - util.flag(this, 'object', obj); |
56 |
| - util.flag(this, 'message', msg); |
57 |
| - util.flag(this, 'eql', config.deepEqual || util.eql); |
58 |
| - |
59 |
| - return util.proxify(this); |
60 |
| -} |
| 48 | +export class Assertion { |
| 49 | + constructor(obj, msg, ssfi, lockSsfi) { |
| 50 | + util.flag(this, "ssfi", ssfi || Assertion); |
| 51 | + util.flag(this, "lockSsfi", lockSsfi); |
| 52 | + util.flag(this, "object", obj); |
| 53 | + util.flag(this, "message", msg); |
| 54 | + util.flag(this, "eql", config.deepEqual || util.eql); |
| 55 | + |
| 56 | + return util.proxify(this); |
| 57 | + } |
61 | 58 |
|
62 |
| -Object.defineProperty(Assertion, 'includeStack', { |
63 |
| - get: function () { |
| 59 | + /** @returns {boolean} */ |
| 60 | + static get includeStack() { |
64 | 61 | console.warn(
|
65 |
| - 'Assertion.includeStack is deprecated, use chai.config.includeStack instead.' |
| 62 | + "Assertion.includeStack is deprecated, use chai.config.includeStack instead.", |
66 | 63 | );
|
67 | 64 | return config.includeStack;
|
68 |
| - }, |
69 |
| - set: function (value) { |
| 65 | + } |
| 66 | + |
| 67 | + /** @param {boolean} value */ |
| 68 | + static set includeStack(value) { |
70 | 69 | console.warn(
|
71 |
| - 'Assertion.includeStack is deprecated, use chai.config.includeStack instead.' |
| 70 | + "Assertion.includeStack is deprecated, use chai.config.includeStack instead.", |
72 | 71 | );
|
73 | 72 | config.includeStack = value;
|
74 | 73 | }
|
75 |
| -}); |
76 | 74 |
|
77 |
| -Object.defineProperty(Assertion, 'showDiff', { |
78 |
| - get: function () { |
| 75 | + /** @returns {boolean} */ |
| 76 | + static get showDiff() { |
79 | 77 | console.warn(
|
80 |
| - 'Assertion.showDiff is deprecated, use chai.config.showDiff instead.' |
| 78 | + "Assertion.showDiff is deprecated, use chai.config.showDiff instead.", |
81 | 79 | );
|
82 | 80 | return config.showDiff;
|
83 |
| - }, |
84 |
| - set: function (value) { |
| 81 | + } |
| 82 | + |
| 83 | + /** @param {boolean} value */ |
| 84 | + static set showDiff(value) { |
85 | 85 | console.warn(
|
86 |
| - 'Assertion.showDiff is deprecated, use chai.config.showDiff instead.' |
| 86 | + "Assertion.showDiff is deprecated, use chai.config.showDiff instead.", |
87 | 87 | );
|
88 | 88 | config.showDiff = value;
|
89 | 89 | }
|
90 |
| -}); |
91 |
| - |
92 |
| -Assertion.addProperty = function (name, fn) { |
93 |
| - util.addProperty(this.prototype, name, fn); |
94 |
| -}; |
95 | 90 |
|
96 |
| -Assertion.addMethod = function (name, fn) { |
97 |
| - util.addMethod(this.prototype, name, fn); |
98 |
| -}; |
| 91 | + /** |
| 92 | + * @param {string} name |
| 93 | + * @param {Function} fn |
| 94 | + */ |
| 95 | + static addProperty(name, fn) { |
| 96 | + util.addProperty(this.prototype, name, fn); |
| 97 | + } |
99 | 98 |
|
100 |
| -Assertion.addChainableMethod = function (name, fn, chainingBehavior) { |
101 |
| - util.addChainableMethod(this.prototype, name, fn, chainingBehavior); |
102 |
| -}; |
| 99 | + /** |
| 100 | + * @param {string} name |
| 101 | + * @param {Function} fn |
| 102 | + */ |
| 103 | + static addMethod(name, fn) { |
| 104 | + util.addMethod(this.prototype, name, fn); |
| 105 | + } |
103 | 106 |
|
104 |
| -Assertion.overwriteProperty = function (name, fn) { |
105 |
| - util.overwriteProperty(this.prototype, name, fn); |
106 |
| -}; |
| 107 | + /** |
| 108 | + * @param {string} name |
| 109 | + * @param {Function} fn |
| 110 | + * @param {Function} chainingBehavior |
| 111 | + */ |
| 112 | + static addChainableMethod(name, fn, chainingBehavior) { |
| 113 | + util.addChainableMethod(this.prototype, name, fn, chainingBehavior); |
| 114 | + } |
107 | 115 |
|
108 |
| -Assertion.overwriteMethod = function (name, fn) { |
109 |
| - util.overwriteMethod(this.prototype, name, fn); |
110 |
| -}; |
| 116 | + /** |
| 117 | + * @param {string} name |
| 118 | + * @param {Function} fn |
| 119 | + */ |
| 120 | + static overwriteProperty(name, fn) { |
| 121 | + util.overwriteProperty(this.prototype, name, fn); |
| 122 | + } |
111 | 123 |
|
112 |
| -Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) { |
113 |
| - util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior); |
114 |
| -}; |
| 124 | + /** |
| 125 | + * @param {string} name |
| 126 | + * @param {Function} fn |
| 127 | + */ |
| 128 | + static overwriteMethod(name, fn) { |
| 129 | + util.overwriteMethod(this.prototype, name, fn); |
| 130 | + } |
115 | 131 |
|
116 |
| -/** |
117 |
| - * ### .assert(expression, message, negateMessage, expected, actual, showDiff) |
118 |
| - * |
119 |
| - * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass. |
120 |
| - * |
121 |
| - * @name assert |
122 |
| - * @param {unknown} expression to be tested |
123 |
| - * @param {string | Function} message or function that returns message to display if expression fails |
124 |
| - * @param {string | Function} negatedMessage or function that returns negatedMessage to display if negated expression fails |
125 |
| - * @param {unknown} expected value (remember to check for negation) |
126 |
| - * @param {unknown} actual (optional) will default to `this.obj` |
127 |
| - * @param {boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails |
128 |
| - * @private |
129 |
| - */ |
| 132 | + /** |
| 133 | + * @param {string} name |
| 134 | + * @param {Function} fn |
| 135 | + * @param {Function} chainingBehavior |
| 136 | + */ |
| 137 | + static overwriteChainableMethod(name, fn, chainingBehavior) { |
| 138 | + util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior); |
| 139 | + } |
130 | 140 |
|
131 |
| -Assertion.prototype.assert = function ( |
132 |
| - expr, |
133 |
| - msg, |
134 |
| - negateMsg, |
135 |
| - expected, |
136 |
| - _actual, |
137 |
| - showDiff |
138 |
| -) { |
139 |
| - let ok = util.test(this, arguments); |
140 |
| - if (false !== showDiff) showDiff = true; |
141 |
| - if (undefined === expected && undefined === _actual) showDiff = false; |
142 |
| - if (true !== config.showDiff) showDiff = false; |
143 |
| - |
144 |
| - if (!ok) { |
145 |
| - msg = util.getMessage(this, arguments); |
146 |
| - let actual = util.getActual(this, arguments); |
147 |
| - let assertionErrorObjectProperties = { |
148 |
| - actual: actual, |
149 |
| - expected: expected, |
150 |
| - showDiff: showDiff |
151 |
| - }; |
152 |
| - |
153 |
| - let operator = util.getOperator(this, arguments); |
154 |
| - if (operator) { |
155 |
| - assertionErrorObjectProperties.operator = operator; |
| 141 | + /** |
| 142 | + * ### .assert(expression, message, negateMessage, expected, actual, showDiff) |
| 143 | + * |
| 144 | + * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass. |
| 145 | + * |
| 146 | + * @name assert |
| 147 | + * @param {unknown} _expr to be tested |
| 148 | + * @param {string | Function} msg or function that returns message to display if expression fails |
| 149 | + * @param {string | Function} _negateMsg or function that returns negatedMessage to display if negated expression fails |
| 150 | + * @param {unknown} expected value (remember to check for negation) |
| 151 | + * @param {unknown} _actual (optional) will default to `this.obj` |
| 152 | + * @param {boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails |
| 153 | + */ |
| 154 | + assert(_expr, msg, _negateMsg, expected, _actual, showDiff) { |
| 155 | + var ok = util.test(this, arguments); |
| 156 | + if (false !== showDiff) showDiff = true; |
| 157 | + if (undefined === expected && undefined === _actual) showDiff = false; |
| 158 | + if (true !== config.showDiff) showDiff = false; |
| 159 | + |
| 160 | + if (!ok) { |
| 161 | + msg = util.getMessage(this, arguments); |
| 162 | + var actual = util.getActual(this, arguments); |
| 163 | + var assertionErrorObjectProperties = { |
| 164 | + actual: actual, |
| 165 | + expected: expected, |
| 166 | + showDiff: showDiff, |
| 167 | + }; |
| 168 | + |
| 169 | + var operator = util.getOperator(this, arguments); |
| 170 | + if (operator) { |
| 171 | + assertionErrorObjectProperties.operator = operator; |
| 172 | + } |
| 173 | + |
| 174 | + throw new AssertionError( |
| 175 | + msg, |
| 176 | + assertionErrorObjectProperties, |
| 177 | + config.includeStack ? this.assert : util.flag(this, "ssfi"), |
| 178 | + ); |
156 | 179 | }
|
| 180 | + } |
157 | 181 |
|
158 |
| - throw new AssertionError( |
159 |
| - msg, |
160 |
| - assertionErrorObjectProperties, |
161 |
| - config.includeStack ? this.assert : util.flag(this, 'ssfi') |
162 |
| - ); |
| 182 | + /** |
| 183 | + * Quick reference to stored `actual` value for plugin developers. |
| 184 | + * |
| 185 | + * @returns {unknown} |
| 186 | + */ |
| 187 | + get _obj() { |
| 188 | + return util.flag(this, "object"); |
163 | 189 | }
|
164 |
| -}; |
165 | 190 |
|
166 |
| -/** |
167 |
| - * ### ._obj |
168 |
| - * |
169 |
| - * Quick reference to stored `actual` value for plugin developers. |
170 |
| - * |
171 |
| - * @private |
172 |
| - */ |
173 |
| -Object.defineProperty(Assertion.prototype, '_obj', { |
174 |
| - get: function () { |
175 |
| - return util.flag(this, 'object'); |
176 |
| - }, |
177 |
| - set: function (val) { |
178 |
| - util.flag(this, 'object', val); |
| 191 | + /** |
| 192 | + * Quick reference to stored `actual` value for plugin developers. |
| 193 | + * |
| 194 | + * @param {unknown} val |
| 195 | + */ |
| 196 | + set _obj(val) { |
| 197 | + util.flag(this, "object", val); |
179 | 198 | }
|
180 |
| -}); |
| 199 | +} |
0 commit comments