{"id":651,"hash":"9420fe6ddd93a626582ccfd376d0b001c5f94e7018d2c2fbdfdfc62b3d20040f","pattern":"Jest.js error: &quot;Received: serializes to the same string&quot;","full_message":"I've having a strange problem with this test:\n\ndeal.test.js\nimport Deal from \"../src/models/Deal\";\nimport apiProducts from \"../__mocks__/api/products\";\n\ndescribe(\"Deal\", () => {\n  describe(\"Deal.fromApi\", () => {\n    it(\"takes an api product and returns a Deal\", () => {\n      const apiDeal = apiProducts[0];\n      const newDeal = Deal.fromApi(apiDeal);\n      const expected = expectedDeal();\n      expect(newDeal).toEqual(expected);\n    });\n  });\n});\n\nDeal.js\nexport default class Deal {\n  // no constructor since we only ever create a deal from Deal.fromApi\n\n  static fromApi(obj: Object): Deal {\n    const deal = new Deal();\n    deal.id = obj.id;\n    deal.name = obj.name;\n    deal.slug = obj.slug;\n    deal.permalink = obj.permalink;\n    deal.dateCreated = obj.date_created;\n    deal.dateModified = obj.date_modified;\n    deal.status = obj.status;\n    deal.featured = obj.featured;\n    deal.catalogVisibility = obj.catalog_visibility;\n    deal.descriptionHTML = obj.description;\n    deal.shortDescriptionHTML = obj.short_description;\n    deal.price = Number(obj.price);\n    deal.regularPrice = Number(obj.regular_price);\n    deal.salePrice = Number(obj.sale_price);\n    deal.dateOnSaleFrom = obj.date_on_sale_from;\n    deal.dateOnSaleTo = obj.date_on_sale_to;\n    deal.onSale = obj.on_sale;\n    deal.purchasable = obj.purchasable;\n    deal.relatedIds = obj.related_ids;\n    deal.upsellIds = obj.upsell_ids;\n    deal.crossSellIds = obj.cross_sell_ids;\n    deal.categories = obj.categories;\n    deal.tags = obj.tags;\n    deal.images = obj.images;\n    return deal;\n  }\n\n descriptionWithTextSize(size: number): string {\n    return this.descriptionWithStyle(`font-size:${size}`);\n  }\n\n  descriptionWithStyle(style: string): string {\n    return `<div style=\"${style}\">${this.description}</div>`;\n  }\n\n  distanceFromLocation = (\n    location: Location,\n    unit: unitOfDistance = \"mi\"\n  ): number => {\n    return distanceBetween(this.location, location);\n  };\n\n  distanceFrom = (otherDeal: Deal, unit: unitOfDistance = \"mi\"): number => {\n    return distanceBetween(this.location, otherDeal.location);\n  };\n\n  static toApi(deal: Deal): Object {\n    return { ...deal };\n  }\n}\n\nThe test fails with this error:\n\n  ● Deal › Deal.fromApi › takes an api product and returns a Deal\n\n    expect(received).toEqual(expected) // deep equality\n\n    Expected: {\"catalogVisibility\": \"visible\", \"categories\": [{\"id\": 15, \"name\": \"New York\", \"slug\": \"new-york\"}], \"crossSellIds\": [34, 31], \"dateCreated\": \"2019-05-18T17:36:14\", \"dateModified\": \"2019-05-18T17:39:02\", \"dateOnSaleFrom\": null, \"dateOnSaleTo\": null, \"descriptionHTML\": \"<p>Pete's Tavern<br />\n    129 E 18th St<br />\n    New York, NY 10003</p>\n    <p>Weekdays from 4 p.m. to 7 p.m.<br />\n    $5 wines and beers</p>\n    \", \"distanceFromLocation\": [Function anonymous], \"featured\": false, \"id\": 566, \"images\": [{\"alt\": \"\", \"date_created\": \"2019-05-18T17:38:52\", \"date_created_gmt\": \"2019-05-18T17:38:52\", \"date_modified\": \"2019-05-18T17:38:52\", \"date_modified_gmt\": \"2019-05-18T17:38:52\", \"id\": 567, \"name\": \"wine and beers2\", \"src\": \"https://tragodeals.com/wp-content/uploads/2019/05/wine-and-beers2.jpg\"}], \"name\": \"Wines and beers\", \"onSale\": true, \"permalink\": \"https://tragodeals.com/product/wines-and-beers/\", \"price\": 5, \"purchasable\": true, \"regularPrice\": 11, \"relatedIds\": [552, 564, 390, 37, 543], \"salePrice\": 5, \"shortDescriptionHTML\": \"<p>$5 wines and beers</p>\n    \", \"slug\": \"wines-and-beers\", \"status\": \"publish\", \"tags\": [{\"id\": 58, \"name\": \"beers\", \"slug\": \"beers\"}, {\"id\": 54, \"name\": \"Cocktails\", \"slug\": \"cocktails\"}, {\"id\": 45, \"name\": \"drink\", \"slug\": \"drink\"}, {\"id\": 57, \"name\": \"wine\", \"slug\": \"wine\"}], \"upsellIds\": [53]}\n    Received: serializes to the same string\n\n    > 15 |       expect(newDeal).toEqual(expected);\n         |                       ^\n      16 |     });\n      17 |   });\n      18 | });\n\n      at Object.toEqual (__tests__/deal.test.js:15:23)\n\nI inserted this loop to investigate:\n\nfor (let key in expected) {\n  expect(expected[key]).toEqual(newDeal[key]);\n}\n\nAnd I see that the problem is with functions. So I changed the whole test to this:\n\n      for (let key in expected) {\n        if (typeof expected[key] === \"function\") continue;\n        expect(expected[key]).toEqual(newDeal[key]);\n      }\n     // expect(newDeal).toEqual(expected);\n\nAnd it passes, and also fails when it should. (if you read the old version of this question where I was getting passing tests that I didn't understand, it was because I was returning from the loop when I should have been continueing).\n\nBut I'd like to be able to do it with the standard assertion expect(newDeal).toEqual(expected). It looks like there's something I'm not understanding about checking for class object (Deal) equality with functions.\n\nPS. You might suggest using toMatchObject. But, sadly:\n\n  ● Deal › Deal.fromApi › takes an api product and returns a Deal\n\n    expect(received).toMatchObject(expected)\n\n    - Expected\n    + Received\n\n    @@ -1,6 +1,6 @@\n    - Deal {\n    + Object {\n        \"address\": \"129 E 18th St New York, NY 10003\",\n        \"catalogVisibility\": \"visible\",\n        \"categories\": Array [\n          Object {\n            \"id\": 15,\n\n      13 |         expect(expected[key]).toEqual(newDeal[key]);\n      14 |       }\n    > 15 |       expect(newDeal).toMatchObject(expected);\n         |                       ^\n      16 |     });\n      17 |   });\n      18 | });","ecosystem":"npm","package_name":"unit-testing","package_version":null,"solution":"Similarly to other colleagues I had this issue with an Array comparison, I was basically testing a function that got the largest string in an array, additionally it should return an array if more than 1 of those strings matched the largest length possible.\n\nWhen I started testing I got the following message:\n\nSo I replaced the toBe method\n\nexpect(function(array1)).toBe('one result')\n\nwith toStrictEqual to make a deep equality comparison\n\nexpect(function(array2)).toStrictEqual(['more than one', 'more than one']);","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/56839801/jest-js-error-received-serializes-to-the-same-string","votes":209,"created_at":"2026-04-19T04:51:27.258943+00:00","updated_at":"2026-04-19T04:51:27.258943+00:00"}