Help with writing mocha/jest test for the deep copy function (inheritance)

How can i write a mocha / jest test for deep clone function in javascript?

When cloning an array or object, the biggest danger is when both arrays have the same reference to a part of the original array, so that adding information to one array results in the other array changing as well.

You could start with a test that when given an empty array, you get an empty array that is different from the one that was given.

There’s a number of different ways to check if two arrays are different, but an easy way to start with is to add something to one array, and compare the array length.

describe("clones an array", function () {
  it("clones an empty array", function () {
    const empty = [];
    const cloned = deepClone(empty);
    empty.push("original array");
    expect(empty.length).to.not.equal(cloned.length);
  });
});

Write code to make that test pass, refactor if needed, then add a new test for a new feature, such as a nested array being properly and deeply cloned:

  it("clones a nested array", function () {
    const original = [
      ["to be cloned"]
    ];
    const cloned = deepClone(empty);
    original[0].push("original array");
    expect(original[0].length).to.not.equal(cloned[0].length);
  });

Then you could do something similar with cloning objects too.

2 Likes

thank you, very good insight for the tests. :innocent:

Thanks. To give more detail, the empty array test should have multiple tests:

One test for that you get an array, another test that the array is empty, and another test that the array isn’t the same reference to the original one.

It’s important when using tests that only one thing is tested at a time. That way when an error occurs, you get good details about what caused the problem.

describe("clones an array", function () {
  describe("clones an empty array", function () {
    it("gives an array", function () {
      const emptyArray = [];
      const cloned = deepClone(emptyArray);
      expect(cloned).to.be.an("array");
    });
    it("is empty", function () {
      const emptyArray = [];
      const cloned = deepClone(emptyArray);
      expect(cloned).to.be.empty;
    });
    it("has no reference to original array", function () {
      const emptyArray = [];
      const cloned = deepClone(emptyArray);
      empty.push("original array");
      expect(emptyArray.length).to.not.equal(cloned.length);
    });
  });
});

And the test code can be refactored by moving common variables up to a beforeEach section.

describe("clones an array", function () {
  describe("clones an empty array", function () {
    let emptyArray;
    beforeEach(function () {
      emptyArray = [];
    });
    it("gives an array", function () {
      const cloned = deepClone(emptyArray);
      expect(cloned).to.be.an("array");
    });
    it("is empty", function () {
      const cloned = deepClone(emptyArray);
      expect(cloned).to.be.empty;
    });
    it("has no reference to original array", function () {
      const cloned = deepClone(emptyArray);
      empty.push("original array");
      expect(emptyArray.length).to.not.equal(cloned.length);
    });
  });
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.