chai 5.0.3 → 5.1.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.
Files changed (38) hide show
  1. package/README.md +3 -3
  2. package/chai.js +94 -220
  3. package/eslint.config.js +12 -0
  4. package/lib/chai/assertion.js +30 -31
  5. package/lib/chai/config.js +18 -24
  6. package/lib/chai/core/assertions.js +253 -223
  7. package/lib/chai/interface/assert.js +574 -670
  8. package/lib/chai/interface/expect.js +12 -7
  9. package/lib/chai/interface/should.js +43 -40
  10. package/lib/chai/utils/addChainableMethod.js +6 -11
  11. package/lib/chai/utils/addLengthGuard.js +3 -3
  12. package/lib/chai/utils/addMethod.js +5 -6
  13. package/lib/chai/utils/addProperty.js +5 -6
  14. package/lib/chai/utils/compareByInspect.js +4 -9
  15. package/lib/chai/utils/expectTypes.js +7 -8
  16. package/lib/chai/utils/flag.js +5 -5
  17. package/lib/chai/utils/getActual.js +3 -3
  18. package/lib/chai/utils/getEnumerableProperties.js +2 -3
  19. package/lib/chai/utils/getMessage.js +4 -8
  20. package/lib/chai/utils/getOperator.js +8 -4
  21. package/lib/chai/utils/getOwnEnumerableProperties.js +2 -7
  22. package/lib/chai/utils/getOwnEnumerablePropertySymbols.js +2 -3
  23. package/lib/chai/utils/getProperties.js +5 -3
  24. package/lib/chai/utils/index.js +42 -109
  25. package/lib/chai/utils/inspect.js +5 -4
  26. package/lib/chai/utils/isNaN.js +3 -3
  27. package/lib/chai/utils/isProxyEnabled.js +1 -1
  28. package/lib/chai/utils/objDisplay.js +2 -7
  29. package/lib/chai/utils/overwriteChainableMethod.js +7 -8
  30. package/lib/chai/utils/overwriteMethod.js +10 -11
  31. package/lib/chai/utils/overwriteProperty.js +10 -12
  32. package/lib/chai/utils/proxify.js +9 -9
  33. package/lib/chai/utils/test.js +3 -7
  34. package/lib/chai/utils/transferFlags.js +4 -6
  35. package/lib/chai/utils/type-detect.js +4 -0
  36. package/lib/chai.js +9 -31
  37. package/package.json +6 -3
  38. package/web-test-runner.config.js +4 -1
@@ -4,171 +4,104 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- /*!
8
- * Dependencies that are used for multiple exports are required here only once
9
- */
10
-
7
+ // Dependencies that are used for multiple exports are required here only once
11
8
  import * as checkError from 'check-error';
12
9
 
13
- /*!
14
- * test utility
15
- */
16
-
10
+ // test utility
17
11
  export {test} from './test.js';
18
12
 
19
- /*!
20
- * type utility
21
- */
22
-
13
+ // type utility
23
14
  export {type} from './type-detect.js';
24
15
 
25
- /*!
26
- * expectTypes utility
27
- */
16
+ // expectTypes utility
28
17
  export {expectTypes} from './expectTypes.js';
29
18
 
30
- /*!
31
- * message utility
32
- */
33
-
19
+ // message utility
34
20
  export {getMessage} from './getMessage.js';
35
21
 
36
- /*!
37
- * actual utility
38
- */
39
-
22
+ // actual utility
40
23
  export {getActual} from './getActual.js';
41
24
 
42
- /*!
43
- * Inspect util
44
- */
45
-
25
+ // Inspect util
46
26
  export {inspect} from './inspect.js';
47
27
 
48
- /*!
49
- * Object Display util
50
- */
51
-
28
+ // Object Display util
52
29
  export {objDisplay} from './objDisplay.js';
53
30
 
54
- /*!
55
- * Flag utility
56
- */
57
-
31
+ // Flag utility
58
32
  export {flag} from './flag.js';
59
33
 
60
- /*!
61
- * Flag transferring utility
62
- */
63
-
34
+ // Flag transferring utility
64
35
  export {transferFlags} from './transferFlags.js';
65
36
 
66
- /*!
67
- * Deep equal utility
68
- */
69
-
37
+ // Deep equal utility
70
38
  export {default as eql} from 'deep-eql';
71
39
 
72
- /*!
73
- * Deep path info
74
- */
75
-
40
+ // Deep path info
76
41
  export {getPathInfo, hasProperty} from 'pathval';
77
42
 
78
- /*!
43
+ /**
79
44
  * Function name
45
+ *
46
+ * @param {Function} fn
47
+ * @returns {string}
80
48
  */
81
-
82
49
  export function getName(fn) {
83
50
  return fn.name
84
51
  }
85
52
 
86
- /*!
87
- * add Property
88
- */
89
-
53
+ // add Property
90
54
  export {addProperty} from './addProperty.js';
91
55
 
92
- /*!
93
- * add Method
94
- */
95
-
56
+ // add Method
96
57
  export {addMethod} from './addMethod.js';
97
58
 
98
- /*!
99
- * overwrite Property
100
- */
101
-
59
+ // overwrite Property
102
60
  export {overwriteProperty} from './overwriteProperty.js';
103
61
 
104
- /*!
105
- * overwrite Method
106
- */
107
-
62
+ // overwrite Method
108
63
  export {overwriteMethod} from './overwriteMethod.js';
109
64
 
110
- /*!
111
- * Add a chainable method
112
- */
113
-
65
+ // Add a chainable method
114
66
  export {addChainableMethod} from './addChainableMethod.js';
115
67
 
116
- /*!
117
- * Overwrite chainable method
118
- */
119
-
68
+ // Overwrite chainable method
120
69
  export {overwriteChainableMethod} from './overwriteChainableMethod.js';
121
70
 
122
- /*!
123
- * Compare by inspect method
124
- */
125
-
71
+ // Compare by inspect method
126
72
  export {compareByInspect} from './compareByInspect.js';
127
73
 
128
- /*!
129
- * Get own enumerable property symbols method
130
- */
131
-
74
+ // Get own enumerable property symbols method
132
75
  export {getOwnEnumerablePropertySymbols} from './getOwnEnumerablePropertySymbols.js';
133
76
 
134
- /*!
135
- * Get own enumerable properties method
136
- */
137
-
77
+ // Get own enumerable properties method
138
78
  export {getOwnEnumerableProperties} from './getOwnEnumerableProperties.js';
139
79
 
140
- /*!
141
- * Checks error against a given set of criteria
142
- */
143
-
80
+ // Checks error against a given set of criteria
144
81
  export {checkError};
145
82
 
146
- /*!
147
- * Proxify util
148
- */
149
-
83
+ // Proxify util
150
84
  export {proxify} from './proxify.js';
151
85
 
152
- /*!
153
- * addLengthGuard util
154
- */
155
-
86
+ // addLengthGuard util
156
87
  export {addLengthGuard} from './addLengthGuard.js';
157
88
 
158
- /*!
159
- * isProxyEnabled helper
160
- */
161
-
89
+ // isProxyEnabled helper
162
90
  export {isProxyEnabled} from './isProxyEnabled.js';
163
91
 
164
- /*!
165
- * isNaN method
166
- */
167
-
92
+ // isNaN method
168
93
  export {isNaN} from './isNaN.js';
169
94
 
170
- /*!
171
- * getOperator method
172
- */
173
-
95
+ // getOperator method
174
96
  export {getOperator} from './getOperator.js';
97
+
98
+ /**
99
+ * Determines if an object is a `RegExp`
100
+ * This is used since `instanceof` will not work in virtual contexts
101
+ *
102
+ * @param {*} obj Object to test
103
+ * @returns {boolean}
104
+ */
105
+ export function isRegExp(obj) {
106
+ return Object.prototype.toString.call(obj) === '[object RegExp]';
107
+ }
@@ -10,12 +10,13 @@ import {config} from '../config.js';
10
10
  * Echoes the value of a value. Tries to print the value out
11
11
  * in the best way possible given the different types.
12
12
  *
13
- * @param {Object} obj The object to print out.
14
- * @param {Boolean} showHidden Flag that shows hidden (not enumerable)
13
+ * @param {object} obj The object to print out.
14
+ * @param {boolean} showHidden Flag that shows hidden (not enumerable)
15
15
  * properties of objects. Default is false.
16
- * @param {Number} depth Depth in which to descend in object. Default is 2.
17
- * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
16
+ * @param {number} depth Depth in which to descend in object. Default is 2.
17
+ * @param {boolean} colors Flag to turn on ANSI escape codes to color the
18
18
  * output. Default is false (no coloring).
19
+ * @returns {string}
19
20
  * @namespace Utils
20
21
  * @name inspect
21
22
  */
@@ -11,11 +11,11 @@
11
11
  *
12
12
  * utils.isNaN(NaN); // true
13
13
  *
14
- * @param {Value} The value which has to be checked if it is NaN
14
+ * @param {unknown} value The value which has to be checked if it is NaN
15
+ * @returns {boolean}
15
16
  * @name isNaN
16
- * @api private
17
+ * @private
17
18
  */
18
-
19
19
  function _isNaN(value) {
20
20
  // Refer http://www.ecma-international.org/ecma-262/6.0/#sec-isnan-number
21
21
  // section's NOTE.
@@ -15,8 +15,8 @@ import {config} from '../config.js';
15
15
  *
16
16
  * @namespace Utils
17
17
  * @name isProxyEnabled
18
+ * @returns {boolean}
18
19
  */
19
-
20
20
  export function isProxyEnabled() {
21
21
  return config.useProxy &&
22
22
  typeof Proxy !== 'undefined' &&
@@ -4,10 +4,6 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- /*!
8
- * Module dependencies
9
- */
10
-
11
7
  import {inspect} from './inspect.js';
12
8
  import {config} from '../config.js';
13
9
 
@@ -18,13 +14,12 @@ import {config} from '../config.js';
18
14
  * criteria to be inspected in-line for error
19
15
  * messages or should be truncated.
20
16
  *
21
- * @param {Mixed} javascript object to inspect
17
+ * @param {unknown} obj javascript object to inspect
22
18
  * @returns {string} stringified object
23
19
  * @name objDisplay
24
20
  * @namespace Utils
25
- * @api public
21
+ * @public
26
22
  */
27
-
28
23
  export function objDisplay(obj) {
29
24
  var str = inspect(obj)
30
25
  , type = Object.prototype.toString.call(obj);
@@ -16,10 +16,10 @@ import {transferFlags} from './transferFlags.js';
16
16
  * name.
17
17
  *
18
18
  * utils.overwriteChainableMethod(chai.Assertion.prototype, 'lengthOf',
19
- * function (_super) {
20
- * }
21
- * , function (_super) {
22
- * }
19
+ * function (_super) {
20
+ * }
21
+ * , function (_super) {
22
+ * }
23
23
  * );
24
24
  *
25
25
  * Can also be accessed directly from `chai.Assertion`.
@@ -31,15 +31,14 @@ import {transferFlags} from './transferFlags.js';
31
31
  * expect(myFoo).to.have.lengthOf(3);
32
32
  * expect(myFoo).to.have.lengthOf.above(3);
33
33
  *
34
- * @param {Object} ctx object whose method / property is to be overwritten
35
- * @param {String} name of method / property to overwrite
34
+ * @param {object} ctx object whose method / property is to be overwritten
35
+ * @param {string} name of method / property to overwrite
36
36
  * @param {Function} method function that returns a function to be used for name
37
37
  * @param {Function} chainingBehavior function that returns a function to be used for property
38
38
  * @namespace Utils
39
39
  * @name overwriteChainableMethod
40
- * @api public
40
+ * @public
41
41
  */
42
-
43
42
  export function overwriteChainableMethod(ctx, name, method, chainingBehavior) {
44
43
  var chainableBehavior = ctx.__methods[name];
45
44
 
@@ -18,14 +18,14 @@ import {transferFlags} from './transferFlags.js';
18
18
  * to be used for name.
19
19
  *
20
20
  * utils.overwriteMethod(chai.Assertion.prototype, 'equal', function (_super) {
21
- * return function (str) {
22
- * var obj = utils.flag(this, 'object');
23
- * if (obj instanceof Foo) {
24
- * new chai.Assertion(obj.value).to.equal(str);
25
- * } else {
26
- * _super.apply(this, arguments);
21
+ * return function (str) {
22
+ * var obj = utils.flag(this, 'object');
23
+ * if (obj instanceof Foo) {
24
+ * new chai.Assertion(obj.value).to.equal(str);
25
+ * } else {
26
+ * _super.apply(this, arguments);
27
+ * }
27
28
  * }
28
- * }
29
29
  * });
30
30
  *
31
31
  * Can also be accessed directly from `chai.Assertion`.
@@ -36,14 +36,13 @@ import {transferFlags} from './transferFlags.js';
36
36
  *
37
37
  * expect(myFoo).to.equal('bar');
38
38
  *
39
- * @param {Object} ctx object whose method is to be overwritten
40
- * @param {String} name of method to overwrite
39
+ * @param {object} ctx object whose method is to be overwritten
40
+ * @param {string} name of method to overwrite
41
41
  * @param {Function} method function that returns a function to be used for name
42
42
  * @namespace Utils
43
43
  * @name overwriteMethod
44
- * @api public
44
+ * @public
45
45
  */
46
-
47
46
  export function overwriteMethod(ctx, name, method) {
48
47
  var _method = ctx[name]
49
48
  , _super = function () {
@@ -16,17 +16,16 @@ import {transferFlags} from './transferFlags.js';
16
16
  * access to previous value. Must return function to use as getter.
17
17
  *
18
18
  * utils.overwriteProperty(chai.Assertion.prototype, 'ok', function (_super) {
19
- * return function () {
20
- * var obj = utils.flag(this, 'object');
21
- * if (obj instanceof Foo) {
22
- * new chai.Assertion(obj.name).to.equal('bar');
23
- * } else {
24
- * _super.call(this);
19
+ * return function () {
20
+ * var obj = utils.flag(this, 'object');
21
+ * if (obj instanceof Foo) {
22
+ * new chai.Assertion(obj.name).to.equal('bar');
23
+ * } else {
24
+ * _super.call(this);
25
+ * }
25
26
  * }
26
- * }
27
27
  * });
28
28
  *
29
- *
30
29
  * Can also be accessed directly from `chai.Assertion`.
31
30
  *
32
31
  * chai.Assertion.overwriteProperty('foo', fn);
@@ -35,14 +34,13 @@ import {transferFlags} from './transferFlags.js';
35
34
  *
36
35
  * expect(myFoo).to.be.ok;
37
36
  *
38
- * @param {Object} ctx object whose property is to be overwritten
39
- * @param {String} name of property to overwrite
37
+ * @param {object} ctx object whose property is to be overwritten
38
+ * @param {string} name of property to overwrite
40
39
  * @param {Function} getter function that returns a getter function to be used for name
41
40
  * @namespace Utils
42
41
  * @name overwriteProperty
43
- * @api public
42
+ * @public
44
43
  */
45
-
46
44
  export function overwriteProperty(ctx, name, getter) {
47
45
  var _get = Object.getOwnPropertyDescriptor(ctx, name)
48
46
  , _super = function () {};
@@ -9,6 +9,8 @@ import {isProxyEnabled} from './isProxyEnabled.js';
9
9
  * MIT Licensed
10
10
  */
11
11
 
12
+ const builtins = ['__flags', '__methods', '_obj', 'assert'];
13
+
12
14
  /**
13
15
  * ### .proxify(object)
14
16
  *
@@ -22,14 +24,12 @@ import {isProxyEnabled} from './isProxyEnabled.js';
22
24
  * If proxies are unsupported or disabled via the user's Chai config, then
23
25
  * return object without modification.
24
26
  *
25
- * @param {Object} obj
26
- * @param {String} nonChainableMethodName
27
+ * @param {object} obj
28
+ * @param {string} nonChainableMethodName
29
+ * @returns {unknown}
27
30
  * @namespace Utils
28
31
  * @name proxify
29
32
  */
30
-
31
- const builtins = ['__flags', '__methods', '_obj', 'assert'];
32
-
33
33
  export function proxify(obj ,nonChainableMethodName) {
34
34
  if (!isProxyEnabled()) return obj;
35
35
 
@@ -103,13 +103,13 @@ export function proxify(obj ,nonChainableMethodName) {
103
103
  /**
104
104
  * # stringDistanceCapped(strA, strB, cap)
105
105
  * Return the Levenshtein distance between two strings, but no more than cap.
106
+ *
106
107
  * @param {string} strA
107
108
  * @param {string} strB
108
- * @param {number} number
109
- * @return {number} min(string distance between strA and strB, cap)
110
- * @api private
109
+ * @param {number} cap
110
+ * @returns {number} min(string distance between strA and strB, cap)
111
+ * @private
111
112
  */
112
-
113
113
  function stringDistanceCapped(strA, strB, cap) {
114
114
  if (Math.abs(strA.length - strB.length) >= cap) {
115
115
  return cap;
@@ -4,10 +4,6 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- /*!
8
- * Module dependencies
9
- */
10
-
11
7
  import {flag} from './flag.js';
12
8
 
13
9
  /**
@@ -15,12 +11,12 @@ import {flag} from './flag.js';
15
11
  *
16
12
  * Test an object for expression.
17
13
  *
18
- * @param {Object} object (constructed Assertion)
19
- * @param {Arguments} chai.Assertion.prototype.assert arguments
14
+ * @param {object} obj (constructed Assertion)
15
+ * @param {unknown} args
16
+ * @returns {unknown}
20
17
  * @namespace Utils
21
18
  * @name test
22
19
  */
23
-
24
20
  export function test(obj, args) {
25
21
  var negate = flag(obj, 'negate')
26
22
  , expr = args[0];
@@ -12,21 +12,19 @@
12
12
  * assertion flags (namely `object`, `ssfi`, `lockSsfi`,
13
13
  * and `message`) will not be transferred.
14
14
  *
15
- *
16
15
  * var newAssertion = new Assertion();
17
16
  * utils.transferFlags(assertion, newAssertion);
18
17
  *
19
18
  * var anotherAssertion = new Assertion(myObj);
20
19
  * utils.transferFlags(assertion, anotherAssertion, false);
21
20
  *
22
- * @param {Assertion} assertion the assertion to transfer the flags from
23
- * @param {Object} object the object to transfer the flags to; usually a new assertion
24
- * @param {Boolean} includeAll
21
+ * @param {import('../assertion.js').Assertion} assertion the assertion to transfer the flags from
22
+ * @param {object} object the object to transfer the flags to; usually a new assertion
23
+ * @param {boolean} includeAll
25
24
  * @namespace Utils
26
25
  * @name transferFlags
27
- * @api private
26
+ * @private
28
27
  */
29
-
30
28
  export function transferFlags(assertion, object, includeAll) {
31
29
  var flags = assertion.__flags || (assertion.__flags = Object.create(null));
32
30
 
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @param {unknown} obj
3
+ * @returns {string}
4
+ */
1
5
  export function type(obj) {
2
6
  if (typeof obj === 'undefined') {
3
7
  return 'undefined';
package/lib/chai.js CHANGED
@@ -15,10 +15,7 @@ import {assert} from './chai/interface/assert.js';
15
15
 
16
16
  const used = [];
17
17
 
18
- /*!
19
- * Assertion Error
20
- */
21
-
18
+ // Assertion Error
22
19
  export {AssertionError};
23
20
 
24
21
  /**
@@ -26,11 +23,10 @@ export {AssertionError};
26
23
  *
27
24
  * Provides a way to extend the internals of Chai.
28
25
  *
29
- * @param {Function}
26
+ * @param {Function} fn
30
27
  * @returns {this} for chaining
31
- * @api public
28
+ * @public
32
29
  */
33
-
34
30
  export function use(fn) {
35
31
  const exports = {
36
32
  AssertionError,
@@ -50,38 +46,20 @@ export function use(fn) {
50
46
  return exports;
51
47
  };
52
48
 
53
- /*!
54
- * Utility Functions
55
- */
56
-
49
+ // Utility Functions
57
50
  export {util};
58
51
 
59
- /*!
60
- * Configuration
61
- */
62
-
52
+ // Configuration
63
53
  export {config};
64
54
 
65
- /*!
66
- * Primary `Assertion` prototype
67
- */
68
-
55
+ // Primary `Assertion` prototype
69
56
  export * from './chai/assertion.js';
70
57
 
71
- /*!
72
- * Expect interface
73
- */
74
-
58
+ // Expect interface
75
59
  export * from './chai/interface/expect.js';
76
60
 
77
- /*!
78
- * Should interface
79
- */
80
-
61
+ // Should interface
81
62
  export * from './chai/interface/should.js';
82
63
 
83
- /*!
84
- * Assert interface
85
- */
86
-
64
+ // Assert interface
87
65
  export * from './chai/interface/assert.js';
package/package.json CHANGED
@@ -18,7 +18,7 @@
18
18
  "Veselin Todorov <hi@vesln.com>",
19
19
  "John Firebaugh <john.firebaugh@gmail.com>"
20
20
  ],
21
- "version": "5.0.3",
21
+ "version": "5.1.1",
22
22
  "repository": {
23
23
  "type": "git",
24
24
  "url": "https://github.com/chaijs/chai"
@@ -31,10 +31,11 @@
31
31
  "prebuild": "npm run clean",
32
32
  "build": "npm run build:esm",
33
33
  "build:esm": "esbuild --bundle --format=esm --keep-names --outfile=chai.js index.js",
34
- "pretest": "npm run build",
34
+ "pretest": "npm run lint && npm run build",
35
35
  "test": "npm run test-node && npm run test-chrome",
36
36
  "test-node": "mocha --require ./test/bootstrap/index.js --reporter dot test/*.js",
37
37
  "test-chrome": "web-test-runner --playwright",
38
+ "lint": "eslint lib/",
38
39
  "clean": "rm -f chai.js coverage"
39
40
  },
40
41
  "engines": {
@@ -42,7 +43,7 @@
42
43
  },
43
44
  "dependencies": {
44
45
  "assertion-error": "^2.0.1",
45
- "check-error": "^2.0.0",
46
+ "check-error": "^2.1.1",
46
47
  "deep-eql": "^5.0.1",
47
48
  "loupe": "^3.1.0",
48
49
  "pathval": "^2.0.0"
@@ -53,6 +54,8 @@
53
54
  "@web/test-runner": "^0.18.0",
54
55
  "@web/test-runner-playwright": "^0.11.0",
55
56
  "esbuild": "^0.19.10",
57
+ "eslint": "^8.56.0",
58
+ "eslint-plugin-jsdoc": "^48.0.4",
56
59
  "mocha": "^10.2.0"
57
60
  }
58
61
  }
@@ -5,7 +5,10 @@ const commonjs = fromRollup(rollupCommonjs);
5
5
 
6
6
  export default {
7
7
  nodeResolve: true,
8
- files: ["test/*.js"],
8
+ files: [
9
+ "test/*.js",
10
+ "!test/virtual-machines.js"
11
+ ],
9
12
  plugins: [
10
13
  commonjs({
11
14
  include: [