cypress test - provide more details upon deepCompare failure
[vid.git] / vid-webpack-master / cypress / support / steps / general / compareDeepObjects.step.ts
1 const { _ } = Cypress;
2
3 declare namespace Cypress {
4   interface Chainable {
5     deepCompare: typeof deepCompare,
6   }
7 }
8
9 function deepCompare(actual : any, expected : any) {
10   if(actual !== null && expected !== null){
11     const actualOriginal = JSON.stringify(actual);
12     const expectedOriginal = JSON.stringify(expected);
13
14     let diff : any[] = [];
15     Cypress._.mergeWith(actual, expected, function (objectValue, sourceValue, key, object, source) {
16       if ( !(_.isEqual(objectValue, sourceValue)) && (Object(objectValue) !== objectValue)) {
17         diff.push("key: " +key + ", expected: " + sourceValue + ", actual: " + objectValue);
18       }
19     });
20
21     Cypress._.mergeWith(expected, actual, function (objectValue, sourceValue, key, object, source) {
22       if ( !(_.isEqual(objectValue, sourceValue)) && (Object(objectValue) !== objectValue)) {
23         diff.push("key: " +key + ", expected: " + sourceValue + ", actual: " + objectValue);
24       }
25     });
26
27     if(diff.length > 0){
28       console.error("diff", diff);
29       cy.log("The object are not equals", diff);
30       expect(actual).equals(expected, `diff: ${diff}, actual:${actualOriginal}, expected:${expectedOriginal}`);
31     }
32   }
33 }
34
35 Cypress.Commands.add('deepCompare', deepCompare);