How to use deepCyclicCopyObject method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

deepCyclicCopy.js

Source: deepCyclicCopy.js Github

copy
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4  value: true
5});
6exports.default = deepCyclicCopy;
7
8/**
9 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
10 *
11 * This source code is licensed under the MIT license found in the
12 * LICENSE file in the root directory of this source tree.
13 */
14const EMPTY = new Set();
15
16// Node 6 does not have gOPDs, so we define a simple polyfill for it.
17if (!Object.getOwnPropertyDescriptors) {
18  // @ts-ignore: polyfill
19  Object.getOwnPropertyDescriptors = obj => {
20    const list = {};
21    Object.getOwnPropertyNames(obj)
22      .concat(Object.getOwnPropertySymbols(obj))
23      .forEach(key => {
24        // @ts-ignore: assignment with a Symbol is OK.
25        list[key] = Object.getOwnPropertyDescriptor(obj, key);
26      });
27    return list;
28  };
29}
30
31function deepCyclicCopy(
32  value,
33  options = {
34    blacklist: EMPTY,
35    keepPrototype: false
36  },
37  cycles = new WeakMap()
38) {
39  if (typeof value !== 'object' || value === null) {
40    return value;
41  } else if (cycles.has(value)) {
42    return cycles.get(value);
43  } else if (Array.isArray(value)) {
44    return deepCyclicCopyArray(value, options, cycles);
45  } else {
46    return deepCyclicCopyObject(value, options, cycles);
47  }
48}
49
50function deepCyclicCopyObject(object, options, cycles) {
51  const newObject = options.keepPrototype
52    ? Object.create(Object.getPrototypeOf(object))
53    : {};
54  const descriptors = Object.getOwnPropertyDescriptors(object);
55  cycles.set(object, newObject);
56  Object.keys(descriptors).forEach(key => {
57    if (options.blacklist && options.blacklist.has(key)) {
58      delete descriptors[key];
59      return;
60    }
61
62    const descriptor = descriptors[key];
63
64    if (typeof descriptor.value !== 'undefined') {
65      descriptor.value = deepCyclicCopy(
66        descriptor.value,
67        {
68          blacklist: EMPTY,
69          keepPrototype: options.keepPrototype
70        },
71        cycles
72      );
73    }
74
75    descriptor.configurable = true;
76  });
77  return Object.defineProperties(newObject, descriptors);
78}
79
80function deepCyclicCopyArray(array, options, cycles) {
81  const newArray = options.keepPrototype
82    ? new (Object.getPrototypeOf(array)).constructor(array.length)
83    : [];
84  const length = array.length;
85  cycles.set(array, newArray);
86
87  for (let i = 0; i < length; i++) {
88    newArray[i] = deepCyclicCopy(
89      array[i],
90      {
91        blacklist: EMPTY,
92        keepPrototype: options.keepPrototype
93      },
94      cycles
95    );
96  }
97
98  return newArray;
99}
100
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)