chai 5.1.2 → 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/.prettierrc.json +10 -0
- package/README.md +1 -1
- package/chai.js +892 -542
- package/eslint.config.js +15 -0
- package/lib/chai/assertion.js +181 -141
- package/lib/chai/config.js +0 -2
- package/lib/chai/core/assertions.js +760 -538
- package/lib/chai/interface/assert.js +437 -260
- package/lib/chai/interface/expect.js +11 -7
- package/lib/chai/interface/should.js +27 -21
- package/lib/chai/utils/addChainableMethod.js +69 -70
- package/lib/chai/utils/addLengthGuard.js +18 -5
- package/lib/chai/utils/addMethod.js +4 -5
- package/lib/chai/utils/addProperty.js +27 -28
- package/lib/chai/utils/expectTypes.js +18 -10
- package/lib/chai/utils/flag.js +4 -3
- package/lib/chai/utils/getMessage.js +18 -12
- package/lib/chai/utils/getOperator.js +7 -7
- package/lib/chai/utils/getProperties.js +2 -2
- package/lib/chai/utils/index.js +8 -2
- package/lib/chai/utils/inspect.js +3 -3
- package/lib/chai/utils/isNaN.js +1 -20
- package/lib/chai/utils/isProxyEnabled.js +4 -2
- package/lib/chai/utils/objDisplay.js +7 -6
- package/lib/chai/utils/overwriteChainableMethod.js +15 -14
- package/lib/chai/utils/overwriteMethod.js +8 -9
- package/lib/chai/utils/overwriteProperty.js +38 -39
- package/lib/chai/utils/proxify.js +40 -29
- package/lib/chai/utils/test.js +2 -2
- package/lib/chai/utils/transferFlags.js +9 -4
- package/lib/chai/utils/type-detect.js +1 -1
- package/lib/chai.js +2 -1
- package/package.json +15 -7
- package/tsconfig.json +18 -0
- package/lib/chai/utils/getEnumerableProperties.js +0 -25
- package/register-assert.cjs +0 -3
- package/register-expect.cjs +0 -3
- package/register-should.cjs +0 -3
package/eslint.config.js
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
import jsdoc from "eslint-plugin-jsdoc";
|
|
2
|
+
import eslintjs from "@eslint/js";
|
|
3
|
+
|
|
4
|
+
const {configs: eslintConfigs} = eslintjs;
|
|
2
5
|
|
|
3
6
|
export default [
|
|
4
7
|
jsdoc.configs["flat/recommended"],
|
|
8
|
+
eslintConfigs["recommended"],
|
|
5
9
|
{
|
|
10
|
+
languageOptions: {
|
|
11
|
+
// if we ever use more globals than this, pull in the `globals` package
|
|
12
|
+
globals: {
|
|
13
|
+
console: false
|
|
14
|
+
}
|
|
15
|
+
},
|
|
6
16
|
rules: {
|
|
17
|
+
"no-var": "error",
|
|
7
18
|
"jsdoc/require-param-description": "off",
|
|
8
19
|
"jsdoc/require-returns-description": "off",
|
|
9
20
|
"jsdoc/tag-lines": ["error", "any", { startLines: 1 }],
|
|
21
|
+
"no-unused-vars": ["error", {
|
|
22
|
+
argsIgnorePattern: "^_",
|
|
23
|
+
caughtErrorsIgnorePattern: "^_"
|
|
24
|
+
}]
|
|
10
25
|
},
|
|
11
26
|
},
|
|
12
27
|
];
|
package/lib/chai/assertion.js
CHANGED
|
@@ -9,156 +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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
63
|
-
get
|
|
64
|
-
console.warn(
|
|
61
|
+
/** @returns {boolean} */
|
|
62
|
+
static get includeStack() {
|
|
63
|
+
console.warn(
|
|
64
|
+
'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
|
|
65
|
+
);
|
|
65
66
|
return config.includeStack;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @param {boolean} value */
|
|
70
|
+
static set includeStack(value) {
|
|
71
|
+
console.warn(
|
|
72
|
+
'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
|
|
73
|
+
);
|
|
69
74
|
config.includeStack = value;
|
|
70
75
|
}
|
|
71
|
-
});
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
get
|
|
75
|
-
console.warn(
|
|
77
|
+
/** @returns {boolean} */
|
|
78
|
+
static get showDiff() {
|
|
79
|
+
console.warn(
|
|
80
|
+
'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
|
|
81
|
+
);
|
|
76
82
|
return config.showDiff;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @param {boolean} value */
|
|
86
|
+
static set showDiff(value) {
|
|
87
|
+
console.warn(
|
|
88
|
+
'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
|
|
89
|
+
);
|
|
80
90
|
config.showDiff = value;
|
|
81
91
|
}
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
Assertion.addProperty = function (name, fn) {
|
|
85
|
-
util.addProperty(this.prototype, name, fn);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
Assertion.addMethod = function (name, fn) {
|
|
89
|
-
util.addMethod(this.prototype, name, fn);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
Assertion.addChainableMethod = function (name, fn, chainingBehavior) {
|
|
93
|
-
util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
Assertion.overwriteProperty = function (name, fn) {
|
|
97
|
-
util.overwriteProperty(this.prototype, name, fn);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
Assertion.overwriteMethod = function (name, fn) {
|
|
101
|
-
util.overwriteMethod(this.prototype, name, fn);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
Assertion.overwriteChainableMethod = function (name, fn, chainingBehavior) {
|
|
105
|
-
util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* ### .assert(expression, message, negateMessage, expected, actual, showDiff)
|
|
110
|
-
*
|
|
111
|
-
* Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
|
|
112
|
-
*
|
|
113
|
-
* @name assert
|
|
114
|
-
* @param {unknown} expression to be tested
|
|
115
|
-
* @param {string | Function} message or function that returns message to display if expression fails
|
|
116
|
-
* @param {string | Function} negatedMessage or function that returns negatedMessage to display if negated expression fails
|
|
117
|
-
* @param {unknown} expected value (remember to check for negation)
|
|
118
|
-
* @param {unknown} actual (optional) will default to `this.obj`
|
|
119
|
-
* @param {boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails
|
|
120
|
-
* @private
|
|
121
|
-
*/
|
|
122
92
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
msg = util.getMessage(this, arguments);
|
|
131
|
-
var actual = util.getActual(this, arguments);
|
|
132
|
-
var assertionErrorObjectProperties = {
|
|
133
|
-
actual: actual
|
|
134
|
-
, expected: expected
|
|
135
|
-
, showDiff: showDiff
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
var operator = util.getOperator(this, arguments);
|
|
139
|
-
if (operator) {
|
|
140
|
-
assertionErrorObjectProperties.operator = operator;
|
|
141
|
-
}
|
|
93
|
+
/**
|
|
94
|
+
* @param {string} name
|
|
95
|
+
* @param {Function} fn
|
|
96
|
+
*/
|
|
97
|
+
static addProperty(name, fn) {
|
|
98
|
+
util.addProperty(this.prototype, name, fn);
|
|
99
|
+
}
|
|
142
100
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} name
|
|
103
|
+
* @param {Function} fn
|
|
104
|
+
*/
|
|
105
|
+
static addMethod(name, fn) {
|
|
106
|
+
util.addMethod(this.prototype, name, fn);
|
|
147
107
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
+
);
|
|
163
184
|
}
|
|
164
|
-
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Quick reference to stored `actual` value for plugin developers.
|
|
189
|
+
*
|
|
190
|
+
* @returns {unknown}
|
|
191
|
+
*/
|
|
192
|
+
get _obj() {
|
|
193
|
+
return util.flag(this, 'object');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Quick reference to stored `actual` value for plugin developers.
|
|
198
|
+
*
|
|
199
|
+
* @param {unknown} val
|
|
200
|
+
*/
|
|
201
|
+
set _obj(val) {
|
|
202
|
+
util.flag(this, 'object', val);
|
|
203
|
+
}
|
|
204
|
+
}
|