question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Received: serializes to the same string" on object equality checking

See original GitHub issue

🐛 Bug Report

Using .toMatchObject() returns failing test with message Received: serializes to the same string

image

To Reproduce

I am trying to check the users object I receive against my expectedUsers. The received object coming back from MongoDB contains the fields "__v" and "_id" which I do not want to check for (they always change for every test). As such, I am using .toMatchObject() and cannot use something else like .toEqual(). My test snippet is below:

test("should show all existing users", async () => {
  const expectedUsers = [
    {
      email: "andy@example.com",
      friends: [],
      followers: [],
      following: [],
      blocked: []
    },
    {
      email: "john@example.com",
      friends: ["andy@example.com", "mary@example.com"],
      followers: [],
      following: [],
      blocked: []
    },
    {
      email: "mary@example.com",
      friends: [],
      followers: [],
      following: [],
      blocked: ["john@example.com"]
    }
  ];
  await request(app)
    .get(route(path))
    .expect("Content-Type", /json/)
    .expect(200);

  const users = await User.find();

  expect(users).toMatchObject(expectedUsers);
});

(request is made with supertest)

Expected behavior

As documented here,

Use .toMatchObject to check that a JavaScript object matches a subset of the properties of an object. It will match received objects with properties that are not in the expected object.

Since the expected objects is a subset of received objects, I expect my test to pass.

npx envinfo --preset jest result

System:
  OS: macOS 10.14.4
  CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Binaries:
  Node: 10.15.2 - ~/.asdf/shims/node
  npm: 6.9.0 - ~/.asdf/shims/npm
npmPackages:
  jest: ^24.8.0 => 24.8.0 

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:90
  • Comments:40 (8 by maintainers)

github_iconTop GitHub Comments

169reactions
manhhailuacommented, Oct 3, 2019

I’m also experiencing this issue. This is my workaround:

expect(JSON.stringify(result.current)).toEqual(JSON.stringify(expected));
93reactions
pedrottimarkcommented, May 24, 2019

@sabriele From reading Jest code and guessing about MongoDB, users array might have non-index properties which toMatchObject should (but does not) ignore.

Here is a work-around to get rid of them:

expect([...users]).toMatchObject(expectedUsers)

If you can paste the received users before work-around, we can make a realistic regression test.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest.js error: "Received: serializes to the same string"
toEqual(expected) . It looks like there's something I'm not understanding about checking for class object ( Deal ) equality with functions. PS.
Read more >
How to Fix 'serializes to the same string' Errors in Jest - Webtips
In its simplest form (using an empty array or object), this test won't pass. This happens because each object reference is different in ......
Read more >
Javascript Testing With Jest - Medium
To get started all you have to do is create a new file and give it the exact same name as the file...
Read more >
serializes to the same string" on object equality checking
Javascript Required. Kindly enable Javascript. · "Received: serializes to the same string" on object equality checking.
Read more >
Best 29 Serializes To The Same String - Interconex
1. “Received: serializes to the same string” on object equality … Author: github.com. Evaluate 3 ⭐ (17518 Ratings). Top rated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found