1

We're using TestCafe for end-to-end testing, and I'm finding myself looking at an error like this:

AssertionError: expected [ Array(5) ] to deeply equal [ Array(5) ]

I'd like error messages like this to include a diff. I know object diffing modules are available on npm, but I'd like to add it in such a way that I don't have to import this library any time I'm asserting object equality.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
bigblind
  • 12,539
  • 14
  • 68
  • 123

2 Answers2

1

I wrote a very similar typescript function which involves a bread first search through all properties of an object. I slightly modified it for your purpose. Maybe it would work. I didn't tested it.

isPrimitiveType(o) {
  const t = typeof o;
  return t == 'string' || t == 'number' || t == 'boolean';
}

printDiff(obj: any, userPref: any) {
    if (obj === undefined || obj === null) {
      return;
    }
    for (let k in obj) {
      let prop = obj[k];
      if (isPrimitiveType(prop)) {
        if (!userPref[k]) {
          console.log(userPref[k], ' not matches');
        } 
      } else {
        if (!userPref[k]) {
          console.log(userPref[k], ' not matches');
        } else {
          this.setUserPrefs(obj[k], userPref[k]);
        }
      }
    }
}
canbax
  • 3,432
  • 1
  • 27
  • 44
1

The latest TestCafe version (1.11.0) shows the object differences. object differences

mlosev
  • 5,130
  • 1
  • 19
  • 31
Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29