Skip to content

Bug: cloneDeep does not respect instances of classes (affects compat/omit and compat/pick) #801

Closed
@eturino

Description

@eturino

this happens with both cloneDeep and compat.cloneDeep.

It is then affecting other functions like omit.

it('should work with custom classes', () => {
    class CustomTest {
      constructor(public value: number) {}
      myFunc() {
        return `value is ${this.value}`;
      }
    }

    const object = new CustomTest(1);
    const cloned = cloneDeep(object);

    expect(cloned).not.toBe(object);
    expect(cloned).toEqual(object);
    expect(cloned.myFunc()).toBe(object.myFunc()); // <- Error: TypeError: cloned.myFunc is not a function
    expect(cloned.myFunc()).toEqual('value is 1');

    const nested = { customTest: new CustomTest(1) };

    const clonedNested = cloneDeep(nested);

    expect(clonedNested).not.toBe(nested);
    expect(clonedNested).toEqual(nested);
    expect(clonedNested.customTest.myFunc()).toBe(nested.customTest.myFunc());
    expect(clonedNested.customTest.myFunc()).toEqual('value is 1');
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions