Jest.js错误:“收到:序列化为相同的字符串" [英] Jest.js error: "Received: serializes to the same string"
问题描述
此测试存在一个奇怪的问题:
I've having a strange problem with this test:
import Deal from "../src/models/Deal";
import apiProducts from "../__mocks__/api/products";
describe("Deal", () => {
describe("Deal.fromApi", () => {
it("takes an api product and returns a Deal", () => {
const apiDeal = apiProducts[0];
const newDeal = Deal.fromApi(apiDeal);
const expected = expectedDeal();
expect(newDeal).toEqual(expected);
});
});
});
Deal.js
export default class Deal {
// no constructor since we only ever create a deal from Deal.fromApi
static fromApi(obj: Object): Deal {
const deal = new Deal();
deal.id = obj.id;
deal.name = obj.name;
deal.slug = obj.slug;
deal.permalink = obj.permalink;
deal.dateCreated = obj.date_created;
deal.dateModified = obj.date_modified;
deal.status = obj.status;
deal.featured = obj.featured;
deal.catalogVisibility = obj.catalog_visibility;
deal.descriptionHTML = obj.description;
deal.shortDescriptionHTML = obj.short_description;
deal.price = Number(obj.price);
deal.regularPrice = Number(obj.regular_price);
deal.salePrice = Number(obj.sale_price);
deal.dateOnSaleFrom = obj.date_on_sale_from;
deal.dateOnSaleTo = obj.date_on_sale_to;
deal.onSale = obj.on_sale;
deal.purchasable = obj.purchasable;
deal.relatedIds = obj.related_ids;
deal.upsellIds = obj.upsell_ids;
deal.crossSellIds = obj.cross_sell_ids;
deal.categories = obj.categories;
deal.tags = obj.tags;
deal.images = obj.images;
return deal;
}
descriptionWithTextSize(size: number): string {
return this.descriptionWithStyle(`font-size:${size}`);
}
descriptionWithStyle(style: string): string {
return `<div style="${style}">${this.description}</div>`;
}
distanceFromLocation = (
location: Location,
unit: unitOfDistance = "mi"
): number => {
return distanceBetween(this.location, location);
};
distanceFrom = (otherDeal: Deal, unit: unitOfDistance = "mi"): number => {
return distanceBetween(this.location, otherDeal.location);
};
static toApi(deal: Deal): Object {
return { ...deal };
}
}
测试失败,并显示以下错误:
The test fails with this error:
● Deal › Deal.fromApi › takes an api product and returns a Deal
expect(received).toEqual(expected) // deep equality
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 />
129 E 18th St<br />
New York, NY 10003</p>
<p>Weekdays from 4 p.m. to 7 p.m.<br />
$5 wines and beers</p>
", "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>
", "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]}
Received: serializes to the same string
> 15 | expect(newDeal).toEqual(expected);
| ^
16 | });
17 | });
18 | });
at Object.toEqual (__tests__/deal.test.js:15:23)
我插入了此循环以进行调查:
I inserted this loop to investigate:
for (let key in expected) {
expect(expected[key]).toEqual(newDeal[key]);
}
我看到问题出在函数上.因此,我将整个测试更改为:
And I see that the problem is with functions. So I changed the whole test to this:
for (let key in expected) {
if (typeof expected[key] === "function") continue;
expect(expected[key]).toEqual(newDeal[key]);
}
// expect(newDeal).toEqual(expected);
它通过了,并且在应有的情况下也失败了. (如果您阅读了这个问题的旧版本,而我却通过了我不理解的测试,那是因为当我应该continue
ing时,我正在循环中return
).
And 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 return
ing from the loop when I should have been continue
ing).
但是我希望能够使用标准断言expect(newDeal).toEqual(expected)
来做到这一点.在检查类对象(Deal
)与函数的相等性方面,我似乎不太了解.
But 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.
PS.您可能建议使用toMatchObject
.但是,可悲的是:
PS. You might suggest using toMatchObject
. But, sadly:
● Deal › Deal.fromApi › takes an api product and returns a Deal
expect(received).toMatchObject(expected)
- Expected
+ Received
@@ -1,6 +1,6 @@
- Deal {
+ Object {
"address": "129 E 18th St New York, NY 10003",
"catalogVisibility": "visible",
"categories": Array [
Object {
"id": 15,
13 | expect(expected[key]).toEqual(newDeal[key]);
14 | }
> 15 | expect(newDeal).toMatchObject(expected);
| ^
16 | });
17 | });
18 | });
推荐答案
当尝试比较数组时,在一个数组中有一个元素设置为-1索引的元素时遇到了这个问题(想象除了从0至N). 因此,在以下情况下您可能会遇到此错误:
Just had this problem when tried to compare arrays where in one array there was an element with -1 index set (imagine any other key to be set except numbers from 0 to N). So you may have this error in the following scenario:
const arr = [1, 2]
arr[-1] = 'foo'
expect(arr).toEqual([1, 2])
它们都序列化为相同的字符串,但是不相等.
They both serialized to the same string, but they are not equal.
这篇关于Jest.js错误:“收到:序列化为相同的字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!