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
|
@@ -42,14 +42,18 @@ export {expect};
|
|
|
42
42
|
*/
|
|
43
43
|
expect.fail = function (actual, expected, message, operator) {
|
|
44
44
|
if (arguments.length < 2) {
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
message = actual;
|
|
46
|
+
actual = undefined;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
message = message || 'expect.fail()';
|
|
50
|
-
throw new AssertionError(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
throw new AssertionError(
|
|
51
|
+
message,
|
|
52
|
+
{
|
|
53
|
+
actual: actual,
|
|
54
|
+
expected: expected,
|
|
55
|
+
operator: operator
|
|
56
|
+
},
|
|
57
|
+
chai.expect.fail
|
|
58
|
+
);
|
|
55
59
|
};
|
|
@@ -10,17 +10,19 @@ import {AssertionError} from 'assertion-error';
|
|
|
10
10
|
/**
|
|
11
11
|
* @returns {void}
|
|
12
12
|
*/
|
|
13
|
-
function loadShould
|
|
13
|
+
function loadShould() {
|
|
14
14
|
// explicitly define this method as function as to have it's name to include as `ssfi`
|
|
15
15
|
/**
|
|
16
16
|
* @returns {Assertion}
|
|
17
17
|
*/
|
|
18
18
|
function shouldGetter() {
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
if (
|
|
20
|
+
this instanceof String ||
|
|
21
|
+
this instanceof Number ||
|
|
22
|
+
this instanceof Boolean ||
|
|
23
|
+
(typeof Symbol === 'function' && this instanceof Symbol) ||
|
|
24
|
+
(typeof BigInt === 'function' && this instanceof BigInt)
|
|
25
|
+
) {
|
|
24
26
|
return new Assertion(this.valueOf(), null, shouldGetter);
|
|
25
27
|
}
|
|
26
28
|
return new Assertion(this, null, shouldGetter);
|
|
@@ -44,12 +46,12 @@ function loadShould () {
|
|
|
44
46
|
}
|
|
45
47
|
// modify Object.prototype to have `should`
|
|
46
48
|
Object.defineProperty(Object.prototype, 'should', {
|
|
47
|
-
set: shouldSetter
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
set: shouldSetter,
|
|
50
|
+
get: shouldGetter,
|
|
51
|
+
configurable: true
|
|
50
52
|
});
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
let should = {};
|
|
53
55
|
|
|
54
56
|
/**
|
|
55
57
|
* ### .fail([message])
|
|
@@ -74,16 +76,20 @@ function loadShould () {
|
|
|
74
76
|
*/
|
|
75
77
|
should.fail = function (actual, expected, message, operator) {
|
|
76
78
|
if (arguments.length < 2) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
message = actual;
|
|
80
|
+
actual = undefined;
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
message = message || 'should.fail()';
|
|
82
|
-
throw new AssertionError(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
throw new AssertionError(
|
|
85
|
+
message,
|
|
86
|
+
{
|
|
87
|
+
actual: actual,
|
|
88
|
+
expected: expected,
|
|
89
|
+
operator: operator
|
|
90
|
+
},
|
|
91
|
+
should.fail
|
|
92
|
+
);
|
|
87
93
|
};
|
|
88
94
|
|
|
89
95
|
/**
|
|
@@ -147,10 +153,10 @@ function loadShould () {
|
|
|
147
153
|
*/
|
|
148
154
|
should.exist = function (val, msg) {
|
|
149
155
|
new Assertion(val, msg).to.exist;
|
|
150
|
-
}
|
|
156
|
+
};
|
|
151
157
|
|
|
152
158
|
// negation
|
|
153
|
-
should.not = {}
|
|
159
|
+
should.not = {};
|
|
154
160
|
|
|
155
161
|
/**
|
|
156
162
|
* ### .not.equal(actual, expected, [message])
|
|
@@ -209,13 +215,13 @@ function loadShould () {
|
|
|
209
215
|
*/
|
|
210
216
|
should.not.exist = function (val, msg) {
|
|
211
217
|
new Assertion(val, msg).to.not.exist;
|
|
212
|
-
}
|
|
218
|
+
};
|
|
213
219
|
|
|
214
220
|
should['throw'] = should['Throw'];
|
|
215
221
|
should.not['throw'] = should.not['Throw'];
|
|
216
222
|
|
|
217
223
|
return should;
|
|
218
|
-
}
|
|
224
|
+
}
|
|
219
225
|
|
|
220
226
|
export const should = loadShould;
|
|
221
227
|
export const Should = loadShould;
|
|
@@ -15,27 +15,26 @@ import {transferFlags} from './transferFlags.js';
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
// Check whether `Object.setPrototypeOf` is supported
|
|
18
|
-
|
|
18
|
+
let canSetPrototype = typeof Object.setPrototypeOf === 'function';
|
|
19
19
|
|
|
20
20
|
// Without `Object.setPrototypeOf` support, this module will need to add properties to a function.
|
|
21
21
|
// However, some of functions' own props are not configurable and should be skipped.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
let testFn = function () {};
|
|
23
|
+
let excludeNames = Object.getOwnPropertyNames(testFn).filter(function (name) {
|
|
24
|
+
let propDesc = Object.getOwnPropertyDescriptor(testFn, name);
|
|
25
25
|
|
|
26
26
|
// Note: PhantomJS 1.x includes `callee` as one of `testFn`'s own properties,
|
|
27
27
|
// but then returns `undefined` as the property descriptor for `callee`. As a
|
|
28
28
|
// workaround, we perform an otherwise unnecessary type-check for `propDesc`,
|
|
29
29
|
// and then filter it out if it's not an object as it should be.
|
|
30
|
-
if (typeof propDesc !== 'object')
|
|
31
|
-
return true;
|
|
30
|
+
if (typeof propDesc !== 'object') return true;
|
|
32
31
|
|
|
33
32
|
return !propDesc.configurable;
|
|
34
33
|
});
|
|
35
34
|
|
|
36
35
|
// Cache `Function` properties
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
let call = Function.prototype.call,
|
|
37
|
+
apply = Function.prototype.apply;
|
|
39
38
|
|
|
40
39
|
/**
|
|
41
40
|
* ### .addChainableMethod(ctx, name, method, chainingBehavior)
|
|
@@ -67,12 +66,12 @@ var call = Function.prototype.call,
|
|
|
67
66
|
*/
|
|
68
67
|
export function addChainableMethod(ctx, name, method, chainingBehavior) {
|
|
69
68
|
if (typeof chainingBehavior !== 'function') {
|
|
70
|
-
chainingBehavior = function () {
|
|
69
|
+
chainingBehavior = function () {};
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
let chainableBehavior = {
|
|
73
|
+
method: method,
|
|
74
|
+
chainingBehavior: chainingBehavior
|
|
76
75
|
};
|
|
77
76
|
|
|
78
77
|
// save the methods so we can overwrite them later, if we need to.
|
|
@@ -81,67 +80,67 @@ export function addChainableMethod(ctx, name, method, chainingBehavior) {
|
|
|
81
80
|
}
|
|
82
81
|
ctx.__methods[name] = chainableBehavior;
|
|
83
82
|
|
|
84
|
-
Object.defineProperty(ctx, name,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
var result = chainableBehavior.method.apply(this, arguments);
|
|
109
|
-
if (result !== undefined) {
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
var newAssertion = new Assertion();
|
|
114
|
-
transferFlags(this, newAssertion);
|
|
115
|
-
return newAssertion;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
addLengthGuard(chainableMethodWrapper, name, true);
|
|
119
|
-
|
|
120
|
-
// Use `Object.setPrototypeOf` if available
|
|
121
|
-
if (canSetPrototype) {
|
|
122
|
-
// Inherit all properties from the object by replacing the `Function` prototype
|
|
123
|
-
var prototype = Object.create(this);
|
|
124
|
-
// Restore the `call` and `apply` methods from `Function`
|
|
125
|
-
prototype.call = call;
|
|
126
|
-
prototype.apply = apply;
|
|
127
|
-
Object.setPrototypeOf(chainableMethodWrapper, prototype);
|
|
83
|
+
Object.defineProperty(ctx, name, {
|
|
84
|
+
get: function chainableMethodGetter() {
|
|
85
|
+
chainableBehavior.chainingBehavior.call(this);
|
|
86
|
+
|
|
87
|
+
let chainableMethodWrapper = function () {
|
|
88
|
+
// Setting the `ssfi` flag to `chainableMethodWrapper` causes this
|
|
89
|
+
// function to be the starting point for removing implementation
|
|
90
|
+
// frames from the stack trace of a failed assertion.
|
|
91
|
+
//
|
|
92
|
+
// However, we only want to use this function as the starting point if
|
|
93
|
+
// the `lockSsfi` flag isn't set.
|
|
94
|
+
//
|
|
95
|
+
// If the `lockSsfi` flag is set, then this assertion is being
|
|
96
|
+
// invoked from inside of another assertion. In this case, the `ssfi`
|
|
97
|
+
// flag has already been set by the outer assertion.
|
|
98
|
+
//
|
|
99
|
+
// Note that overwriting a chainable method merely replaces the saved
|
|
100
|
+
// methods in `ctx.__methods` instead of completely replacing the
|
|
101
|
+
// overwritten assertion. Therefore, an overwriting assertion won't
|
|
102
|
+
// set the `ssfi` or `lockSsfi` flags.
|
|
103
|
+
if (!flag(this, 'lockSsfi')) {
|
|
104
|
+
flag(this, 'ssfi', chainableMethodWrapper);
|
|
128
105
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (excludeNames.indexOf(asserterName) !== -1) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
var pd = Object.getOwnPropertyDescriptor(ctx, asserterName);
|
|
138
|
-
Object.defineProperty(chainableMethodWrapper, asserterName, pd);
|
|
139
|
-
});
|
|
106
|
+
|
|
107
|
+
let result = chainableBehavior.method.apply(this, arguments);
|
|
108
|
+
if (result !== undefined) {
|
|
109
|
+
return result;
|
|
140
110
|
}
|
|
141
111
|
|
|
142
|
-
|
|
143
|
-
|
|
112
|
+
let newAssertion = new Assertion();
|
|
113
|
+
transferFlags(this, newAssertion);
|
|
114
|
+
return newAssertion;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
addLengthGuard(chainableMethodWrapper, name, true);
|
|
118
|
+
|
|
119
|
+
// Use `Object.setPrototypeOf` if available
|
|
120
|
+
if (canSetPrototype) {
|
|
121
|
+
// Inherit all properties from the object by replacing the `Function` prototype
|
|
122
|
+
let prototype = Object.create(this);
|
|
123
|
+
// Restore the `call` and `apply` methods from `Function`
|
|
124
|
+
prototype.call = call;
|
|
125
|
+
prototype.apply = apply;
|
|
126
|
+
Object.setPrototypeOf(chainableMethodWrapper, prototype);
|
|
144
127
|
}
|
|
145
|
-
|
|
128
|
+
// Otherwise, redefine all properties (slow!)
|
|
129
|
+
else {
|
|
130
|
+
let asserterNames = Object.getOwnPropertyNames(ctx);
|
|
131
|
+
asserterNames.forEach(function (asserterName) {
|
|
132
|
+
if (excludeNames.indexOf(asserterName) !== -1) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let pd = Object.getOwnPropertyDescriptor(ctx, asserterName);
|
|
137
|
+
Object.defineProperty(chainableMethodWrapper, asserterName, pd);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
transferFlags(this, chainableMethodWrapper);
|
|
142
|
+
return proxify(chainableMethodWrapper);
|
|
143
|
+
},
|
|
144
|
+
configurable: true
|
|
146
145
|
});
|
|
147
146
|
}
|
|
@@ -46,13 +46,26 @@ export function addLengthGuard(fn, assertionName, isChainable) {
|
|
|
46
46
|
Object.defineProperty(fn, 'length', {
|
|
47
47
|
get: function () {
|
|
48
48
|
if (isChainable) {
|
|
49
|
-
throw Error(
|
|
50
|
-
'
|
|
51
|
-
|
|
49
|
+
throw Error(
|
|
50
|
+
'Invalid Chai property: ' +
|
|
51
|
+
assertionName +
|
|
52
|
+
'.length. Due' +
|
|
53
|
+
' to a compatibility issue, "length" cannot directly follow "' +
|
|
54
|
+
assertionName +
|
|
55
|
+
'". Use "' +
|
|
56
|
+
assertionName +
|
|
57
|
+
'.lengthOf" instead.'
|
|
58
|
+
);
|
|
52
59
|
}
|
|
53
60
|
|
|
54
|
-
throw Error(
|
|
55
|
-
'
|
|
61
|
+
throw Error(
|
|
62
|
+
'Invalid Chai property: ' +
|
|
63
|
+
assertionName +
|
|
64
|
+
'.length. See' +
|
|
65
|
+
' docs for proper usage of "' +
|
|
66
|
+
assertionName +
|
|
67
|
+
'".'
|
|
68
|
+
);
|
|
56
69
|
}
|
|
57
70
|
});
|
|
58
71
|
|
|
@@ -36,7 +36,7 @@ import {Assertion} from '../assertion.js';
|
|
|
36
36
|
* @public
|
|
37
37
|
*/
|
|
38
38
|
export function addMethod(ctx, name, method) {
|
|
39
|
-
|
|
39
|
+
let methodWrapper = function () {
|
|
40
40
|
// Setting the `ssfi` flag to `methodWrapper` causes this function to be the
|
|
41
41
|
// starting point for removing implementation frames from the stack trace of
|
|
42
42
|
// a failed assertion.
|
|
@@ -53,11 +53,10 @@ export function addMethod(ctx, name, method) {
|
|
|
53
53
|
flag(this, 'ssfi', methodWrapper);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
if (result !== undefined)
|
|
58
|
-
return result;
|
|
56
|
+
let result = method.apply(this, arguments);
|
|
57
|
+
if (result !== undefined) return result;
|
|
59
58
|
|
|
60
|
-
|
|
59
|
+
let newAssertion = new Assertion();
|
|
61
60
|
transferFlags(this, newAssertion);
|
|
62
61
|
return newAssertion;
|
|
63
62
|
};
|
|
@@ -37,35 +37,34 @@ import {transferFlags} from './transferFlags.js';
|
|
|
37
37
|
export function addProperty(ctx, name, getter) {
|
|
38
38
|
getter = getter === undefined ? function () {} : getter;
|
|
39
39
|
|
|
40
|
-
Object.defineProperty(ctx, name,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
40
|
+
Object.defineProperty(ctx, name, {
|
|
41
|
+
get: function propertyGetter() {
|
|
42
|
+
// Setting the `ssfi` flag to `propertyGetter` causes this function to
|
|
43
|
+
// be the starting point for removing implementation frames from the
|
|
44
|
+
// stack trace of a failed assertion.
|
|
45
|
+
//
|
|
46
|
+
// However, we only want to use this function as the starting point if
|
|
47
|
+
// the `lockSsfi` flag isn't set and proxy protection is disabled.
|
|
48
|
+
//
|
|
49
|
+
// If the `lockSsfi` flag is set, then either this assertion has been
|
|
50
|
+
// overwritten by another assertion, or this assertion is being invoked
|
|
51
|
+
// from inside of another assertion. In the first case, the `ssfi` flag
|
|
52
|
+
// has already been set by the overwriting assertion. In the second
|
|
53
|
+
// case, the `ssfi` flag has already been set by the outer assertion.
|
|
54
|
+
//
|
|
55
|
+
// If proxy protection is enabled, then the `ssfi` flag has already been
|
|
56
|
+
// set by the proxy getter.
|
|
57
|
+
if (!isProxyEnabled() && !flag(this, 'lockSsfi')) {
|
|
58
|
+
flag(this, 'ssfi', propertyGetter);
|
|
59
|
+
}
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return result;
|
|
61
|
+
let result = getter.call(this);
|
|
62
|
+
if (result !== undefined) return result;
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
let newAssertion = new Assertion();
|
|
65
|
+
transferFlags(this, newAssertion);
|
|
66
|
+
return newAssertion;
|
|
67
|
+
},
|
|
68
|
+
configurable: true
|
|
70
69
|
});
|
|
71
70
|
}
|
|
@@ -22,25 +22,33 @@ import {type} from './type-detect.js';
|
|
|
22
22
|
* @public
|
|
23
23
|
*/
|
|
24
24
|
export function expectTypes(obj, types) {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
let flagMsg = flag(obj, 'message');
|
|
26
|
+
let ssfi = flag(obj, 'ssfi');
|
|
27
27
|
|
|
28
28
|
flagMsg = flagMsg ? flagMsg + ': ' : '';
|
|
29
29
|
|
|
30
30
|
obj = flag(obj, 'object');
|
|
31
|
-
types = types.map(function (t) {
|
|
31
|
+
types = types.map(function (t) {
|
|
32
|
+
return t.toLowerCase();
|
|
33
|
+
});
|
|
32
34
|
types.sort();
|
|
33
35
|
|
|
34
36
|
// Transforms ['lorem', 'ipsum'] into 'a lorem, or an ipsum'
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
let str = types
|
|
38
|
+
.map(function (t, index) {
|
|
39
|
+
let art = ~['a', 'e', 'i', 'o', 'u'].indexOf(t.charAt(0)) ? 'an' : 'a';
|
|
40
|
+
let or = types.length > 1 && index === types.length - 1 ? 'or ' : '';
|
|
41
|
+
return or + art + ' ' + t;
|
|
42
|
+
})
|
|
43
|
+
.join(', ');
|
|
40
44
|
|
|
41
|
-
|
|
45
|
+
let objType = type(obj).toLowerCase();
|
|
42
46
|
|
|
43
|
-
if (
|
|
47
|
+
if (
|
|
48
|
+
!types.some(function (expected) {
|
|
49
|
+
return objType === expected;
|
|
50
|
+
})
|
|
51
|
+
) {
|
|
44
52
|
throw new AssertionError(
|
|
45
53
|
flagMsg + 'object tested must be ' + str + ', but ' + objType + ' given',
|
|
46
54
|
undefined,
|
package/lib/chai/utils/flag.js
CHANGED
|
@@ -15,16 +15,17 @@
|
|
|
15
15
|
* utils.flag(this, 'foo', 'bar'); // setter
|
|
16
16
|
* utils.flag(this, 'foo'); // getter, returns `bar`
|
|
17
17
|
*
|
|
18
|
-
* @
|
|
18
|
+
* @template {{__flags?: {[key: PropertyKey]: unknown}}} T
|
|
19
|
+
* @param {T} obj object constructed Assertion
|
|
19
20
|
* @param {string} key
|
|
20
|
-
* @param {unknown} value
|
|
21
|
+
* @param {unknown} [value]
|
|
21
22
|
* @namespace Utils
|
|
22
23
|
* @name flag
|
|
23
24
|
* @returns {unknown | undefined}
|
|
24
25
|
* @private
|
|
25
26
|
*/
|
|
26
27
|
export function flag(obj, key, value) {
|
|
27
|
-
|
|
28
|
+
let flags = obj.__flags || (obj.__flags = Object.create(null));
|
|
28
29
|
if (arguments.length === 3) {
|
|
29
30
|
flags[key] = value;
|
|
30
31
|
} else {
|
|
@@ -21,26 +21,32 @@ import {objDisplay} from './objDisplay.js';
|
|
|
21
21
|
* - `#{exp}` expected value
|
|
22
22
|
*
|
|
23
23
|
* @param {object} obj object (constructed Assertion)
|
|
24
|
-
* @param {
|
|
25
|
-
* @returns {
|
|
24
|
+
* @param {IArguments} args chai.Assertion.prototype.assert arguments
|
|
25
|
+
* @returns {string}
|
|
26
26
|
* @namespace Utils
|
|
27
27
|
* @name getMessage
|
|
28
28
|
* @public
|
|
29
29
|
*/
|
|
30
30
|
export function getMessage(obj, args) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
let negate = flag(obj, 'negate');
|
|
32
|
+
let val = flag(obj, 'object');
|
|
33
|
+
let expected = args[3];
|
|
34
|
+
let actual = getActual(obj, args);
|
|
35
|
+
let msg = negate ? args[2] : args[1];
|
|
36
|
+
let flagMsg = flag(obj, 'message');
|
|
37
37
|
|
|
38
|
-
if(typeof msg ===
|
|
38
|
+
if (typeof msg === 'function') msg = msg();
|
|
39
39
|
msg = msg || '';
|
|
40
40
|
msg = msg
|
|
41
|
-
.replace(/#\{this\}/g, function () {
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
.replace(/#\{this\}/g, function () {
|
|
42
|
+
return objDisplay(val);
|
|
43
|
+
})
|
|
44
|
+
.replace(/#\{act\}/g, function () {
|
|
45
|
+
return objDisplay(actual);
|
|
46
|
+
})
|
|
47
|
+
.replace(/#\{exp\}/g, function () {
|
|
48
|
+
return objDisplay(expected);
|
|
49
|
+
});
|
|
44
50
|
|
|
45
51
|
return flagMsg ? flagMsg + ': ' + msg : msg;
|
|
46
52
|
}
|
|
@@ -6,8 +6,8 @@ import {type} from './type-detect.js';
|
|
|
6
6
|
* @returns {boolean}
|
|
7
7
|
*/
|
|
8
8
|
function isObjectType(obj) {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
let objectType = type(obj);
|
|
10
|
+
let objectTypes = ['Array', 'Object', 'Function'];
|
|
11
11
|
|
|
12
12
|
return objectTypes.indexOf(objectType) !== -1;
|
|
13
13
|
}
|
|
@@ -29,10 +29,10 @@ function isObjectType(obj) {
|
|
|
29
29
|
* @public
|
|
30
30
|
*/
|
|
31
31
|
export function getOperator(obj, args) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
let operator = flag(obj, 'operator');
|
|
33
|
+
let negate = flag(obj, 'negate');
|
|
34
|
+
let expected = args[3];
|
|
35
|
+
let msg = negate ? args[2] : args[1];
|
|
36
36
|
|
|
37
37
|
if (operator) {
|
|
38
38
|
return operator;
|
|
@@ -49,7 +49,7 @@ export function getOperator(obj, args) {
|
|
|
49
49
|
return undefined;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
let isObject = isObjectType(expected);
|
|
53
53
|
if (/\snot\s/.test(msg)) {
|
|
54
54
|
return isObject ? 'notDeepStrictEqual' : 'notStrictEqual';
|
|
55
55
|
}
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* @public
|
|
18
18
|
*/
|
|
19
19
|
export function getProperties(object) {
|
|
20
|
-
|
|
20
|
+
let result = Object.getOwnPropertyNames(object);
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* @param {unknown} property
|
|
@@ -28,7 +28,7 @@ export function getProperties(object) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
let proto = Object.getPrototypeOf(object);
|
|
32
32
|
while (proto !== null) {
|
|
33
33
|
Object.getOwnPropertyNames(proto).forEach(addProperty);
|
|
34
34
|
proto = Object.getPrototypeOf(proto);
|
package/lib/chai/utils/index.js
CHANGED
|
@@ -48,7 +48,7 @@ export {getPathInfo, hasProperty} from 'pathval';
|
|
|
48
48
|
* @returns {string}
|
|
49
49
|
*/
|
|
50
50
|
export function getName(fn) {
|
|
51
|
-
return fn.name
|
|
51
|
+
return fn.name;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
// add Property
|
|
@@ -107,6 +107,12 @@ export function isRegExp(obj) {
|
|
|
107
107
|
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Determines if an object is numeric or not
|
|
112
|
+
*
|
|
113
|
+
* @param {unknown} obj Object to test
|
|
114
|
+
* @returns {boolean}
|
|
115
|
+
*/
|
|
110
116
|
export function isNumeric(obj) {
|
|
111
|
-
return ['Number', 'BigInt'].includes(type(obj))
|
|
117
|
+
return ['Number', 'BigInt'].includes(type(obj));
|
|
112
118
|
}
|
|
@@ -21,11 +21,11 @@ import {config} from '../config.js';
|
|
|
21
21
|
* @name inspect
|
|
22
22
|
*/
|
|
23
23
|
export function inspect(obj, showHidden, depth, colors) {
|
|
24
|
-
|
|
24
|
+
let options = {
|
|
25
25
|
colors: colors,
|
|
26
|
-
depth:
|
|
26
|
+
depth: typeof depth === 'undefined' ? 2 : depth,
|
|
27
27
|
showHidden: showHidden,
|
|
28
|
-
truncate: config.truncateThreshold ? config.truncateThreshold : Infinity
|
|
28
|
+
truncate: config.truncateThreshold ? config.truncateThreshold : Infinity
|
|
29
29
|
};
|
|
30
30
|
return _inspect(obj, options);
|
|
31
31
|
}
|
package/lib/chai/utils/isNaN.js
CHANGED
|
@@ -4,23 +4,4 @@
|
|
|
4
4
|
* MIT Licensed
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
* ### .isNaN(value)
|
|
9
|
-
*
|
|
10
|
-
* Checks if the given value is NaN or not.
|
|
11
|
-
*
|
|
12
|
-
* utils.isNaN(NaN); // true
|
|
13
|
-
*
|
|
14
|
-
* @param {unknown} value The value which has to be checked if it is NaN
|
|
15
|
-
* @returns {boolean}
|
|
16
|
-
* @name isNaN
|
|
17
|
-
* @private
|
|
18
|
-
*/
|
|
19
|
-
function _isNaN(value) {
|
|
20
|
-
// Refer http://www.ecma-international.org/ecma-262/6.0/#sec-isnan-number
|
|
21
|
-
// section's NOTE.
|
|
22
|
-
return value !== value;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// If ECMAScript 6's Number.isNaN is present, prefer that.
|
|
26
|
-
export const isNaN = Number.isNaN || _isNaN;
|
|
7
|
+
export const isNaN = Number.isNaN;
|