Keys that are defined as class getters aren't seen by toMatchObject.
class Base {
readonly a = 1;
get b() {
return 2;
}
}
class Sub extends Base {
get c() {
return 3;
}
}
expect(new Sub()).toMatchObject({ a: 1, b: 2, c: 3 });
This fails with:
Error: expect(received).toMatchObject(expected)
- Expected - 2
+ Received + 0
Object {
"a": 1,
- "b": 2,
- "c": 3,
}
I expect the b & c keys to be found and asserted as present/equal to the expectation.
Fwiw I can work around this by using expect.objectContaining:
expect(new Sub()).toEqual(expect.objectContaining({ a: 1, b: 2, c: 3 }));
But this is longer to type out, and just in general I expected toMatchObject to work the same way as "recursive objectContainings", which afaiu is the intent.
I tried to setup a repl.it but it looks like the class syntax isn't supported out-of-the-box; I'm sure they support it with the right configuration; let me know if I should figure that out, otherwise I think the reproduction is simple enough that hopefully it's fine to skip the repl.it link.
$ npx envinfo --preset jest
npx: installed 1 in 1.058s
System:
OS: Linux 5.4 Ubuntu 20.04 LTS (Focal Fossa)
CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Binaries:
Node: 13.14.0 - ~/.nvm/versions/node/v13.14.0/bin/node
Yarn: 1.22.4 - /usr/bin/yarn
npm: 6.14.4 - ~/.nvm/versions/node/v13.14.0/bin/npm
npmPackages:
jest: ^26.0.1 => 26.0.1
Hey! I started looking into this and should have a PR up soon :smile:
Most helpful comment
Hey! I started looking into this and should have a PR up soon :smile: