summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/cypress/support/steps/general/compareDeepObjects.step.ts
blob: f8a8b0fccc3b717e6f36f6d38c5b728595cd740d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const { _ } = Cypress;

declare namespace Cypress {
  interface Chainable {
    deepCompare: typeof deepCompare,
  }
}

function deepCompare(actual : any, expected : any) {
  if(actual !== null && expected !== null){
    const actualOriginal = JSON.stringify(actual);
    const expectedOriginal = JSON.stringify(expected);

    let diff : any[] = [];
    Cypress._.mergeWith(actual, expected, function (objectValue, sourceValue, key, object, source) {
      if ( !(_.isEqual(objectValue, sourceValue)) && (Object(objectValue) !== objectValue)) {
        diff.push("key: " +key + ", expected: " + sourceValue + ", actual: " + objectValue);
      }
    });

    Cypress._.mergeWith(expected, actual, function (objectValue, sourceValue, key, object, source) {
      if ( !(_.isEqual(objectValue, sourceValue)) && (Object(objectValue) !== objectValue)) {
        diff.push("key: " +key + ", expected: " + sourceValue + ", actual: " + objectValue);
      }
    });

    if(diff.length > 0){
      console.error("diff", diff);
      cy.log("The object are not equals", diff);
      expect(actual).equals(expected, `diff: ${diff}, actual:${actualOriginal}, expected:${expectedOriginal}`);
    }
  }
}

Cypress.Commands.add('deepCompare', deepCompare);