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

expect.toMatchObject throws TypeError instead of showing diff #6730

Closed
heygent opened this issue Jul 20, 2018 · 4 comments · Fixed by #7334
Closed

expect.toMatchObject throws TypeError instead of showing diff #6730

heygent opened this issue Jul 20, 2018 · 4 comments · Fixed by #7334

Comments

@heygent
Copy link

heygent commented Jul 20, 2018

🐛 Bug Report

expect.toMatchObject throws a TypeError when an object A is expected to match an object B, where A has been created with Object.create(null) and B has properties that aren't in A.

To Reproduce

it('should work with Object.create(null) objects', () => {
  const obj = Object.assign(
    Object.create(null),
    { hello: 'world' }
  );

  expect(obj).toMatchObject({
    hello: 'world',
    foo: 'bar'
  });
});

Output:

 FAIL  ./add-test.js
  ✕ should work with Object.create(null) objects (11ms)

  ● should work with Object.create(null) objects

    TypeError: Cannot read property 'prototype' of undefined

       5 |   );
       6 | 
    >  7 |   expect(obj).toMatchObject({
       8 |     hello: 'world',
       9 |     foo: 'bar'
      10 |   });
      
      at Object.<anonymous>.it (add-test.js:7:15)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.834s, estimated 1s
Ran all test suites.

Expected behavior

The test should fail and show the diff between the objects.

Link to repl or repo (highly encouraged)

https://repl.it/repls/SizzlingWoodenAcrobat

@shai32
Copy link

shai32 commented Nov 4, 2018

I did some debugging, it fails here
image
object it the object inside expect,
and its object.constructor is undefined

@richardgirges
Copy link

Been running into this issue for a while. It took me a while to realize that this TypeError is only thrown when the objects don't match. Makes a lot more sense now that I'm looking at @shai32's findings.

@SimenB
Copy link
Member

SimenB commented Nov 6, 2018

A PR fixing this is most welcome!

ryanoglesby08 added a commit to ryanoglesby08/jest that referenced this issue Nov 6, 2018
SimenB pushed a commit that referenced this issue Nov 7, 2018
* handle objects with no constructor in hasOwnProperty

Closes #6730

* update changelog
manueliglesias added a commit to manueliglesias/aws-mobile-appsync-sdk-js that referenced this issue Apr 16, 2019
To include fix for:
jestjs/jest#6730
manueliglesias added a commit to awslabs/aws-mobile-appsync-sdk-js that referenced this issue May 14, 2019
* update jest

To include fix for:
jestjs/jest#6730

* Remove noEmitOnError from tsconfig

* Remove unneeded mock

* Fix test

Not sure why, but jest seems to have trouble loading some modules, the filter(Boolean) makes the test pass

* Dedupe packages

* Accept keyPrefix in config to allow per client storage in multi-client

* Pass keyPrefix when creating store

* Typescript fixes

* Add colon to keyPrefix in store

* Remove unused import

* Return false instead of undefined when response is not optimistic

* Enforce unique keyPrefixes among clients

Unit tests refactoring to account for module state (keyPrefixesInUse)

* Improve typings for auth options

* Make invalid auth type error non retryable

* Add test coverage for AWS_IAM auth mode

* Kepp client instances around to prevent garbage collection

* Use default prefixKey from redux-persist
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants