@jest/expect-utils 29.7.0 → 30.0.0-alpha.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/build/utils.js DELETED
@@ -1,462 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
- exports.arrayBufferEquality = void 0;
7
- exports.emptyObject = emptyObject;
8
- exports.typeEquality =
9
- exports.subsetEquality =
10
- exports.sparseArrayEquality =
11
- exports.pathAsArray =
12
- exports.partition =
13
- exports.iterableEquality =
14
- exports.isOneline =
15
- exports.isError =
16
- exports.getPath =
17
- exports.getObjectSubset =
18
- exports.getObjectKeys =
19
- void 0;
20
- var _jestGetType = require('jest-get-type');
21
- var _immutableUtils = require('./immutableUtils');
22
- var _jasmineUtils = require('./jasmineUtils');
23
- var Symbol = globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol;
24
- /**
25
- * Copyright (c) Meta Platforms, Inc. and affiliates.
26
- *
27
- * This source code is licensed under the MIT license found in the
28
- * LICENSE file in the root directory of this source tree.
29
- *
30
- */
31
- /**
32
- * Checks if `hasOwnProperty(object, key)` up the prototype chain, stopping at `Object.prototype`.
33
- */
34
- const hasPropertyInObject = (object, key) => {
35
- const shouldTerminate =
36
- !object || typeof object !== 'object' || object === Object.prototype;
37
- if (shouldTerminate) {
38
- return false;
39
- }
40
- return (
41
- Object.prototype.hasOwnProperty.call(object, key) ||
42
- hasPropertyInObject(Object.getPrototypeOf(object), key)
43
- );
44
- };
45
-
46
- // Retrieves an object's keys for evaluation by getObjectSubset. This evaluates
47
- // the prototype chain for string keys but not for symbols. (Otherwise, it
48
- // could find values such as a Set or Map's Symbol.toStringTag, with unexpected
49
- // results.)
50
- const getObjectKeys = object => [
51
- ...Object.keys(object),
52
- ...Object.getOwnPropertySymbols(object)
53
- ];
54
- exports.getObjectKeys = getObjectKeys;
55
- const getPath = (object, propertyPath) => {
56
- if (!Array.isArray(propertyPath)) {
57
- propertyPath = pathAsArray(propertyPath);
58
- }
59
- if (propertyPath.length) {
60
- const lastProp = propertyPath.length === 1;
61
- const prop = propertyPath[0];
62
- const newObject = object[prop];
63
- if (!lastProp && (newObject === null || newObject === undefined)) {
64
- // This is not the last prop in the chain. If we keep recursing it will
65
- // hit a `can't access property X of undefined | null`. At this point we
66
- // know that the chain has broken and we can return right away.
67
- return {
68
- hasEndProp: false,
69
- lastTraversedObject: object,
70
- traversedPath: []
71
- };
72
- }
73
- const result = getPath(newObject, propertyPath.slice(1));
74
- if (result.lastTraversedObject === null) {
75
- result.lastTraversedObject = object;
76
- }
77
- result.traversedPath.unshift(prop);
78
- if (lastProp) {
79
- // Does object have the property with an undefined value?
80
- // Although primitive values support bracket notation (above)
81
- // they would throw TypeError for in operator (below).
82
- result.endPropIsDefined =
83
- !(0, _jestGetType.isPrimitive)(object) && prop in object;
84
- result.hasEndProp = newObject !== undefined || result.endPropIsDefined;
85
- if (!result.hasEndProp) {
86
- result.traversedPath.shift();
87
- }
88
- }
89
- return result;
90
- }
91
- return {
92
- lastTraversedObject: null,
93
- traversedPath: [],
94
- value: object
95
- };
96
- };
97
-
98
- // Strip properties from object that are not present in the subset. Useful for
99
- // printing the diff for toMatchObject() without adding unrelated noise.
100
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
101
- exports.getPath = getPath;
102
- const getObjectSubset = (
103
- object,
104
- subset,
105
- customTesters = [],
106
- seenReferences = new WeakMap()
107
- ) => {
108
- /* eslint-enable @typescript-eslint/explicit-module-boundary-types */
109
- if (Array.isArray(object)) {
110
- if (Array.isArray(subset) && subset.length === object.length) {
111
- // The map method returns correct subclass of subset.
112
- return subset.map((sub, i) =>
113
- getObjectSubset(object[i], sub, customTesters)
114
- );
115
- }
116
- } else if (object instanceof Date) {
117
- return object;
118
- } else if (isObject(object) && isObject(subset)) {
119
- if (
120
- (0, _jasmineUtils.equals)(object, subset, [
121
- ...customTesters,
122
- iterableEquality,
123
- subsetEquality
124
- ])
125
- ) {
126
- // Avoid unnecessary copy which might return Object instead of subclass.
127
- return subset;
128
- }
129
- const trimmed = {};
130
- seenReferences.set(object, trimmed);
131
- getObjectKeys(object)
132
- .filter(key => hasPropertyInObject(subset, key))
133
- .forEach(key => {
134
- trimmed[key] = seenReferences.has(object[key])
135
- ? seenReferences.get(object[key])
136
- : getObjectSubset(
137
- object[key],
138
- subset[key],
139
- customTesters,
140
- seenReferences
141
- );
142
- });
143
- if (getObjectKeys(trimmed).length > 0) {
144
- return trimmed;
145
- }
146
- }
147
- return object;
148
- };
149
- exports.getObjectSubset = getObjectSubset;
150
- const IteratorSymbol = Symbol.iterator;
151
- const hasIterator = object => !!(object != null && object[IteratorSymbol]);
152
-
153
- /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
154
- const iterableEquality = (
155
- a,
156
- b,
157
- customTesters = [] /* eslint-enable @typescript-eslint/explicit-module-boundary-types */,
158
- aStack = [],
159
- bStack = []
160
- ) => {
161
- if (
162
- typeof a !== 'object' ||
163
- typeof b !== 'object' ||
164
- Array.isArray(a) ||
165
- Array.isArray(b) ||
166
- !hasIterator(a) ||
167
- !hasIterator(b)
168
- ) {
169
- return undefined;
170
- }
171
- if (a.constructor !== b.constructor) {
172
- return false;
173
- }
174
- let length = aStack.length;
175
- while (length--) {
176
- // Linear search. Performance is inversely proportional to the number of
177
- // unique nested structures.
178
- // circular references at same depth are equal
179
- // circular reference is not equal to non-circular one
180
- if (aStack[length] === a) {
181
- return bStack[length] === b;
182
- }
183
- }
184
- aStack.push(a);
185
- bStack.push(b);
186
- const iterableEqualityWithStack = (a, b) =>
187
- iterableEquality(
188
- a,
189
- b,
190
- [...filteredCustomTesters],
191
- [...aStack],
192
- [...bStack]
193
- );
194
-
195
- // Replace any instance of iterableEquality with the new
196
- // iterableEqualityWithStack so we can do circular detection
197
- const filteredCustomTesters = [
198
- ...customTesters.filter(t => t !== iterableEquality),
199
- iterableEqualityWithStack
200
- ];
201
- if (a.size !== undefined) {
202
- if (a.size !== b.size) {
203
- return false;
204
- } else if (
205
- (0, _jasmineUtils.isA)('Set', a) ||
206
- (0, _immutableUtils.isImmutableUnorderedSet)(a)
207
- ) {
208
- let allFound = true;
209
- for (const aValue of a) {
210
- if (!b.has(aValue)) {
211
- let has = false;
212
- for (const bValue of b) {
213
- const isEqual = (0, _jasmineUtils.equals)(
214
- aValue,
215
- bValue,
216
- filteredCustomTesters
217
- );
218
- if (isEqual === true) {
219
- has = true;
220
- }
221
- }
222
- if (has === false) {
223
- allFound = false;
224
- break;
225
- }
226
- }
227
- }
228
- // Remove the first value from the stack of traversed values.
229
- aStack.pop();
230
- bStack.pop();
231
- return allFound;
232
- } else if (
233
- (0, _jasmineUtils.isA)('Map', a) ||
234
- (0, _immutableUtils.isImmutableUnorderedKeyed)(a)
235
- ) {
236
- let allFound = true;
237
- for (const aEntry of a) {
238
- if (
239
- !b.has(aEntry[0]) ||
240
- !(0, _jasmineUtils.equals)(
241
- aEntry[1],
242
- b.get(aEntry[0]),
243
- filteredCustomTesters
244
- )
245
- ) {
246
- let has = false;
247
- for (const bEntry of b) {
248
- const matchedKey = (0, _jasmineUtils.equals)(
249
- aEntry[0],
250
- bEntry[0],
251
- filteredCustomTesters
252
- );
253
- let matchedValue = false;
254
- if (matchedKey === true) {
255
- matchedValue = (0, _jasmineUtils.equals)(
256
- aEntry[1],
257
- bEntry[1],
258
- filteredCustomTesters
259
- );
260
- }
261
- if (matchedValue === true) {
262
- has = true;
263
- }
264
- }
265
- if (has === false) {
266
- allFound = false;
267
- break;
268
- }
269
- }
270
- }
271
- // Remove the first value from the stack of traversed values.
272
- aStack.pop();
273
- bStack.pop();
274
- return allFound;
275
- }
276
- }
277
- const bIterator = b[IteratorSymbol]();
278
- for (const aValue of a) {
279
- const nextB = bIterator.next();
280
- if (
281
- nextB.done ||
282
- !(0, _jasmineUtils.equals)(aValue, nextB.value, filteredCustomTesters)
283
- ) {
284
- return false;
285
- }
286
- }
287
- if (!bIterator.next().done) {
288
- return false;
289
- }
290
- if (
291
- !(0, _immutableUtils.isImmutableList)(a) &&
292
- !(0, _immutableUtils.isImmutableOrderedKeyed)(a) &&
293
- !(0, _immutableUtils.isImmutableOrderedSet)(a) &&
294
- !(0, _immutableUtils.isImmutableRecord)(a)
295
- ) {
296
- const aEntries = Object.entries(a);
297
- const bEntries = Object.entries(b);
298
- if (!(0, _jasmineUtils.equals)(aEntries, bEntries)) {
299
- return false;
300
- }
301
- }
302
-
303
- // Remove the first value from the stack of traversed values.
304
- aStack.pop();
305
- bStack.pop();
306
- return true;
307
- };
308
- exports.iterableEquality = iterableEquality;
309
- const isObject = a => a !== null && typeof a === 'object';
310
- const isObjectWithKeys = a =>
311
- isObject(a) &&
312
- !(a instanceof Error) &&
313
- !(a instanceof Array) &&
314
- !(a instanceof Date);
315
- const subsetEquality = (object, subset, customTesters = []) => {
316
- const filteredCustomTesters = customTesters.filter(t => t !== subsetEquality);
317
-
318
- // subsetEquality needs to keep track of the references
319
- // it has already visited to avoid infinite loops in case
320
- // there are circular references in the subset passed to it.
321
- const subsetEqualityWithContext =
322
- (seenReferences = new WeakMap()) =>
323
- (object, subset) => {
324
- if (!isObjectWithKeys(subset)) {
325
- return undefined;
326
- }
327
- return getObjectKeys(subset).every(key => {
328
- if (isObjectWithKeys(subset[key])) {
329
- if (seenReferences.has(subset[key])) {
330
- return (0, _jasmineUtils.equals)(
331
- object[key],
332
- subset[key],
333
- filteredCustomTesters
334
- );
335
- }
336
- seenReferences.set(subset[key], true);
337
- }
338
- const result =
339
- object != null &&
340
- hasPropertyInObject(object, key) &&
341
- (0, _jasmineUtils.equals)(object[key], subset[key], [
342
- ...filteredCustomTesters,
343
- subsetEqualityWithContext(seenReferences)
344
- ]);
345
- // The main goal of using seenReference is to avoid circular node on tree.
346
- // It will only happen within a parent and its child, not a node and nodes next to it (same level)
347
- // We should keep the reference for a parent and its child only
348
- // Thus we should delete the reference immediately so that it doesn't interfere
349
- // other nodes within the same level on tree.
350
- seenReferences.delete(subset[key]);
351
- return result;
352
- });
353
- };
354
- return subsetEqualityWithContext()(object, subset);
355
- };
356
-
357
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
358
- exports.subsetEquality = subsetEquality;
359
- const typeEquality = (a, b) => {
360
- if (
361
- a == null ||
362
- b == null ||
363
- a.constructor === b.constructor ||
364
- // Since Jest globals are different from Node globals,
365
- // constructors are different even between arrays when comparing properties of mock objects.
366
- // Both of them should be able to compare correctly when they are array-to-array.
367
- // https://github.com/jestjs/jest/issues/2549
368
- (Array.isArray(a) && Array.isArray(b))
369
- ) {
370
- return undefined;
371
- }
372
- return false;
373
- };
374
- exports.typeEquality = typeEquality;
375
- const arrayBufferEquality = (a, b) => {
376
- if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) {
377
- return undefined;
378
- }
379
- const dataViewA = new DataView(a);
380
- const dataViewB = new DataView(b);
381
-
382
- // Buffers are not equal when they do not have the same byte length
383
- if (dataViewA.byteLength !== dataViewB.byteLength) {
384
- return false;
385
- }
386
-
387
- // Check if every byte value is equal to each other
388
- for (let i = 0; i < dataViewA.byteLength; i++) {
389
- if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) {
390
- return false;
391
- }
392
- }
393
- return true;
394
- };
395
- exports.arrayBufferEquality = arrayBufferEquality;
396
- const sparseArrayEquality = (a, b, customTesters = []) => {
397
- if (!Array.isArray(a) || !Array.isArray(b)) {
398
- return undefined;
399
- }
400
-
401
- // A sparse array [, , 1] will have keys ["2"] whereas [undefined, undefined, 1] will have keys ["0", "1", "2"]
402
- const aKeys = Object.keys(a);
403
- const bKeys = Object.keys(b);
404
- return (
405
- (0, _jasmineUtils.equals)(
406
- a,
407
- b,
408
- customTesters.filter(t => t !== sparseArrayEquality),
409
- true
410
- ) && (0, _jasmineUtils.equals)(aKeys, bKeys)
411
- );
412
- };
413
- exports.sparseArrayEquality = sparseArrayEquality;
414
- const partition = (items, predicate) => {
415
- const result = [[], []];
416
- items.forEach(item => result[predicate(item) ? 0 : 1].push(item));
417
- return result;
418
- };
419
- exports.partition = partition;
420
- const pathAsArray = propertyPath => {
421
- const properties = [];
422
- if (propertyPath === '') {
423
- properties.push('');
424
- return properties;
425
- }
426
-
427
- // will match everything that's not a dot or a bracket, and "" for consecutive dots.
428
- const pattern = RegExp('[^.[\\]]+|(?=(?:\\.)(?:\\.|$))', 'g');
429
-
430
- // Because the regex won't match a dot in the beginning of the path, if present.
431
- if (propertyPath[0] === '.') {
432
- properties.push('');
433
- }
434
- propertyPath.replace(pattern, match => {
435
- properties.push(match);
436
- return match;
437
- });
438
- return properties;
439
- };
440
-
441
- // Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/src/Angular.js#L685-L693
442
- exports.pathAsArray = pathAsArray;
443
- const isError = value => {
444
- switch (Object.prototype.toString.call(value)) {
445
- case '[object Error]':
446
- case '[object Exception]':
447
- case '[object DOMException]':
448
- return true;
449
- default:
450
- return value instanceof Error;
451
- }
452
- };
453
- exports.isError = isError;
454
- function emptyObject(obj) {
455
- return obj && typeof obj === 'object' ? !Object.keys(obj).length : false;
456
- }
457
- const MULTILINE_REGEXP = /[\r\n]/;
458
- const isOneline = (expected, received) =>
459
- typeof expected === 'string' &&
460
- typeof received === 'string' &&
461
- (!MULTILINE_REGEXP.test(expected) || !MULTILINE_REGEXP.test(received));
462
- exports.isOneline = isOneline;