Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do object->deep.include and array.of.object->deep.include behave differently? #1479

Open
mknj opened this issue Sep 5, 2022 · 1 comment

Comments

@mknj
Copy link

mknj commented Sep 5, 2022

summary

I would expect that a deep.include of an array of objects compares the contained objects via deep.include and not via deep.equal.
aka. expect([{x:1,y:2}]).deep.include([{x:1}]) behaves like expect({x:1,y:2}).deep.include({x:1})

version

chai 4.3.6

expected

1
2
3

actual

1

/home/mknj/js/chai/node_modules/chai/lib/chai/assertion.js:152
      throw new AssertionError(
      ^
AssertionError: expected [ { d: 1, e: 2 } ] to deep include [ { d: 1 } ]
    at Object.<anonymous> (/home/mknj/js/chai/test.js:13:28)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  showDiff: false,
  actual: [ { d: 1, e: 2 } ],
  expected: undefined,
  operator: 'strictEqual'
}

example code with workaround using lodash

const expect = require("chai").expect
const _ = require("lodash")

const a      = {    b:{c:[{d:1    }]}}
const result = {a:1,b:{c:[{d:1,e:2}]}}

// workaround for 3
expect(result).to.deep.equal(_.merge(result,a))
// ok (object include)
expect(result.b.c[0]).to.deep.include(a.b.c[0])
console.log(1)
// bad (array of object include)
expect(result.b.c).to.deep.include(a.b.c)
console.log(2)
// bad (object..array of object include)
expect(result).to.deep.include(a)
console.log(3)
@mknj
Copy link
Author

mknj commented Sep 5, 2022

use case

I have an API with a request object, that is similar to the response object. The response object does contain some additional information including timestamps.
To verify that the original elements are in the response i would like to do a expect(response).to.deep.INCLUDE(request).
After that i want to do a expect(response).to.deep.INCLUDE([{some_important_fields}])
to check additional fields excluding i.e. timestamps.

I understand for simple cases that include/contains behave differently for arrays and objects.

However deep comparing objects containing arrays of objects leads to strange situations.
i.e. object_include is applied recursively to the main object, but not to the objects in an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant