Jest: toMatchObject doesn't work with class getters

Created on 12 Jul 2020  路  2Comments  路  Source: facebook/jest

馃悰 Bug Report

Keys that are defined as class getters aren't seen by toMatchObject.

To Reproduce

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,
  }

Expected behavior

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.

Link to repl or repo (highly encouraged)

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.

envinfo

$ 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
Bug Report Help Wanted Needs Triage

Most helpful comment

Hey! I started looking into this and should have a PR up soon :smile:

All 2 comments

Hey! I started looking into this and should have a PR up soon :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StephanBijzitter picture StephanBijzitter  路  3Comments

kgowru picture kgowru  路  3Comments

mmcgahan picture mmcgahan  路  3Comments

paularmstrong picture paularmstrong  路  3Comments

hramos picture hramos  路  3Comments