chai 5.2.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/eslint.config.js CHANGED
@@ -14,6 +14,7 @@ export default [
14
14
  }
15
15
  },
16
16
  rules: {
17
+ "no-var": "error",
17
18
  "jsdoc/require-param-description": "off",
18
19
  "jsdoc/require-returns-description": "off",
19
20
  "jsdoc/tag-lines": ["error", "any", { startLines: 1 }],
@@ -9,172 +9,196 @@ import {config} from './config.js';
9
9
  import {AssertionError} from 'assertion-error';
10
10
  import * as util from './utils/index.js';
11
11
 
12
- /**
13
- * Assertion Constructor
14
- *
15
- * Creates object for chaining.
16
- *
17
- * `Assertion` objects contain metadata in the form of flags. Three flags can
18
- * be assigned during instantiation by passing arguments to this constructor:
19
- *
20
- * - `object`: This flag contains the target of the assertion. For example, in
21
- * the assertion `expect(numKittens).to.equal(7);`, the `object` flag will
22
- * contain `numKittens` so that the `equal` assertion can reference it when
23
- * needed.
24
- *
25
- * - `message`: This flag contains an optional custom error message to be
26
- * prepended to the error message that's generated by the assertion when it
27
- * fails.
28
- *
29
- * - `ssfi`: This flag stands for "start stack function indicator". It
30
- * contains a function reference that serves as the starting point for
31
- * removing frames from the stack trace of the error that's created by the
32
- * assertion when it fails. The goal is to provide a cleaner stack trace to
33
- * end users by removing Chai's internal functions. Note that it only works
34
- * in environments that support `Error.captureStackTrace`, and only when
35
- * `Chai.config.includeStack` hasn't been set to `false`.
36
- *
37
- * - `lockSsfi`: This flag controls whether or not the given `ssfi` flag
38
- * should retain its current value, even as assertions are chained off of
39
- * this object. This is usually set to `true` when creating a new assertion
40
- * from within another assertion. It's also temporarily set to `true` before
41
- * an overwritten assertion gets called by the overwriting assertion.
42
- *
43
- * - `eql`: This flag contains the deepEqual function to be used by the assertion.
44
- *
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
49
- * @returns {unknown}
50
- * @private
51
- */
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
- }
12
+ export class Assertion {
13
+ /** @type {{}} */
14
+ __flags = {};
15
+
16
+ /**
17
+ * Creates object for chaining.
18
+ * `Assertion` objects contain metadata in the form of flags. Three flags can
19
+ * be assigned during instantiation by passing arguments to this constructor:
20
+ *
21
+ * - `object`: This flag contains the target of the assertion. For example, in
22
+ * the assertion `expect(numKittens).to.equal(7);`, the `object` flag will
23
+ * contain `numKittens` so that the `equal` assertion can reference it when
24
+ * needed.
25
+ *
26
+ * - `message`: This flag contains an optional custom error message to be
27
+ * prepended to the error message that's generated by the assertion when it
28
+ * fails.
29
+ *
30
+ * - `ssfi`: This flag stands for "start stack function indicator". It
31
+ * contains a function reference that serves as the starting point for
32
+ * removing frames from the stack trace of the error that's created by the
33
+ * assertion when it fails. The goal is to provide a cleaner stack trace to
34
+ * end users by removing Chai's internal functions. Note that it only works
35
+ * in environments that support `Error.captureStackTrace`, and only when
36
+ * `Chai.config.includeStack` hasn't been set to `false`.
37
+ *
38
+ * - `lockSsfi`: This flag controls whether or not the given `ssfi` flag
39
+ * should retain its current value, even as assertions are chained off of
40
+ * this object. This is usually set to `true` when creating a new assertion
41
+ * from within another assertion. It's also temporarily set to `true` before
42
+ * an overwritten assertion gets called by the overwriting assertion.
43
+ *
44
+ * - `eql`: This flag contains the deepEqual function to be used by the assertion.
45
+ *
46
+ * @param {unknown} obj target of the assertion
47
+ * @param {string} [msg] (optional) custom error message
48
+ * @param {Function} [ssfi] (optional) starting point for removing stack frames
49
+ * @param {boolean} [lockSsfi] (optional) whether or not the ssfi flag is locked
50
+ */
51
+ constructor(obj, msg, ssfi, lockSsfi) {
52
+ util.flag(this, 'ssfi', ssfi || Assertion);
53
+ util.flag(this, 'lockSsfi', lockSsfi);
54
+ util.flag(this, 'object', obj);
55
+ util.flag(this, 'message', msg);
56
+ util.flag(this, 'eql', config.deepEqual || util.eql);
57
+
58
+ return util.proxify(this);
59
+ }
61
60
 
62
- Object.defineProperty(Assertion, 'includeStack', {
63
- get: function () {
61
+ /** @returns {boolean} */
62
+ static get includeStack() {
64
63
  console.warn(
65
64
  'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
66
65
  );
67
66
  return config.includeStack;
68
- },
69
- set: function (value) {
67
+ }
68
+
69
+ /** @param {boolean} value */
70
+ static set includeStack(value) {
70
71
  console.warn(
71
72
  'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
72
73
  );
73
74
  config.includeStack = value;
74
75
  }
75
- });
76
76
 
77
- Object.defineProperty(Assertion, 'showDiff', {
78
- get: function () {
77
+ /** @returns {boolean} */
78
+ static get showDiff() {
79
79
  console.warn(
80
80
  'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
81
81
  );
82
82
  return config.showDiff;
83
- },
84
- set: function (value) {
83
+ }
84
+
85
+ /** @param {boolean} value */
86
+ static set showDiff(value) {
85
87
  console.warn(
86
88
  'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
87
89
  );
88
90
  config.showDiff = value;
89
91
  }
90
- });
91
-
92
- Assertion.addProperty = function (name, fn) {
93
- util.addProperty(this.prototype, name, fn);
94
- };
95
-
96
- Assertion.addMethod = function (name, fn) {
97
- util.addMethod(this.prototype, name, fn);
98
- };
99
-
100
- Assertion.addChainableMethod = function (name, fn, chainingBehavior) {
101
- util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
102
- };
103
-
104
- Assertion.overwriteProperty = function (name, fn) {
105
- util.overwriteProperty(this.prototype, name, fn);
106
- };
107
-
108
- Assertion.overwriteMethod = function (name, fn) {
109
- util.overwriteMethod(this.prototype, name, fn);
110
- };
111
-
112
- Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) {
113
- util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior);
114
- };
115
-
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
- */
130
92
 
131
- Assertion.prototype.assert = function (
132
- expr,
133
- msg,
134
- negateMsg,
135
- expected,
136
- _actual,
137
- showDiff
138
- ) {
139
- var 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
- var actual = util.getActual(this, arguments);
147
- var assertionErrorObjectProperties = {
148
- actual: actual,
149
- expected: expected,
150
- showDiff: showDiff
151
- };
152
-
153
- var operator = util.getOperator(this, arguments);
154
- if (operator) {
155
- assertionErrorObjectProperties.operator = operator;
156
- }
93
+ /**
94
+ * @param {string} name
95
+ * @param {Function} fn
96
+ */
97
+ static addProperty(name, fn) {
98
+ util.addProperty(this.prototype, name, fn);
99
+ }
157
100
 
158
- throw new AssertionError(
159
- msg,
160
- assertionErrorObjectProperties,
161
- config.includeStack ? this.assert : util.flag(this, 'ssfi')
162
- );
101
+ /**
102
+ * @param {string} name
103
+ * @param {Function} fn
104
+ */
105
+ static addMethod(name, fn) {
106
+ util.addMethod(this.prototype, name, fn);
163
107
  }
164
- };
165
-
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 () {
108
+
109
+ /**
110
+ * @param {string} name
111
+ * @param {Function} fn
112
+ * @param {Function} chainingBehavior
113
+ */
114
+ static addChainableMethod(name, fn, chainingBehavior) {
115
+ util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
116
+ }
117
+
118
+ /**
119
+ * @param {string} name
120
+ * @param {Function} fn
121
+ */
122
+ static overwriteProperty(name, fn) {
123
+ util.overwriteProperty(this.prototype, name, fn);
124
+ }
125
+
126
+ /**
127
+ * @param {string} name
128
+ * @param {Function} fn
129
+ */
130
+ static overwriteMethod(name, fn) {
131
+ util.overwriteMethod(this.prototype, name, fn);
132
+ }
133
+
134
+ /**
135
+ * @param {string} name
136
+ * @param {Function} fn
137
+ * @param {Function} chainingBehavior
138
+ */
139
+ static overwriteChainableMethod(name, fn, chainingBehavior) {
140
+ util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior);
141
+ }
142
+
143
+ /**
144
+ * ### .assert(expression, message, negateMessage, expected, actual, showDiff)
145
+ *
146
+ * Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
147
+ *
148
+ * @name assert
149
+ * @param {unknown} _expr to be tested
150
+ * @param {string | Function} msg or function that returns message to display if expression fails
151
+ * @param {string | Function} _negateMsg or function that returns negatedMessage to display if negated expression fails
152
+ * @param {unknown} expected value (remember to check for negation)
153
+ * @param {unknown} _actual (optional) will default to `this.obj`
154
+ * @param {boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails
155
+ * @returns {void}
156
+ */
157
+ assert(_expr, msg, _negateMsg, expected, _actual, showDiff) {
158
+ const ok = util.test(this, arguments);
159
+ if (false !== showDiff) showDiff = true;
160
+ if (undefined === expected && undefined === _actual) showDiff = false;
161
+ if (true !== config.showDiff) showDiff = false;
162
+
163
+ if (!ok) {
164
+ msg = util.getMessage(this, arguments);
165
+ const actual = util.getActual(this, arguments);
166
+ /** @type {Record<PropertyKey, unknown>} */
167
+ const assertionErrorObjectProperties = {
168
+ actual: actual,
169
+ expected: expected,
170
+ showDiff: showDiff
171
+ };
172
+
173
+ const operator = util.getOperator(this, arguments);
174
+ if (operator) {
175
+ assertionErrorObjectProperties.operator = operator;
176
+ }
177
+
178
+ throw new AssertionError(
179
+ msg,
180
+ assertionErrorObjectProperties,
181
+ // @ts-expect-error Not sure what to do about these types yet
182
+ config.includeStack ? this.assert : util.flag(this, 'ssfi')
183
+ );
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Quick reference to stored `actual` value for plugin developers.
189
+ *
190
+ * @returns {unknown}
191
+ */
192
+ get _obj() {
175
193
  return util.flag(this, 'object');
176
- },
177
- set: function (val) {
194
+ }
195
+
196
+ /**
197
+ * Quick reference to stored `actual` value for plugin developers.
198
+ *
199
+ * @param {unknown} val
200
+ */
201
+ set _obj(val) {
178
202
  util.flag(this, 'object', val);
179
203
  }
180
- });
204
+ }