Как проверить что обьекты равны в Jest?
Начала изучать Jest и проходить задания. Одно не пропускает. Не понимаю, как иначе реализовать.
Привела мой код
/**
* Write test to check that objects equal after
* calling function boo
*/
const obj1 = {
name: "Test",
};
const obj2 = {
name: "John",
};
const boo = (obj1, obj2) => {
obj1.name = obj2.name;
};
describe("Practicing with tests", () => {
it("objects equal after calling function boo", () => {
boo(obj1, obj2);
console.log(obj1 === obj2);
});
}); // Modify this
Ответы (2 шт):
Автор решения: Виктор
→ Ссылка
Необходимо var вместо const:
var obj1 = {
name: "Test",
};
var obj2 = {
name: "John",
};
и:
describe("Practicing with tests", () => {
it("objects equal after calling function boo", () => {
obj1 = obj2; // оба объекта одинаковы
console.log(obj1 === obj2);
});
});
Или, как в Вашем случае надо:
const obj1 = {
name: "Test",
};
const obj2 = {
name: "John",
};
const boo = (obj1, obj2) => {
obj1.name = obj2.name;
};
describe("Practicing with tests", () => {
it("objects equal after calling function boo", () => {
boo(obj1, obj2);
console.log(obj1.name === obj2.name); // <<<-------
});
});
Автор решения: LIMPIX64
→ Ссылка
Используйте expect
Метод toEqual проверяет свойства объекта, а сравнение === только ссылки на эти объекты
Читайте подробнее
const obj1 = {
name: "Test",
};
const obj2 = {
name: "John",
};
const boo = (obj1, obj2) => {
obj1.name = obj2.name;
};
describe("Practicing with tests", () => {
it("objects equal after calling function boo", () => {
boo(obj1, obj2)
expect(obj1).toEqual(obj2)
});
});
Можно также сравнить ТОЛЬКО интересующее свойство
expect(obj1.name).toBe(obj2.name)