diff --git a/packages/collection/__tests__/collection.test.ts b/packages/collection/__tests__/collection.test.ts index 3f0af6514..7160eaca2 100644 --- a/packages/collection/__tests__/collection.test.ts +++ b/packages/collection/__tests__/collection.test.ts @@ -207,6 +207,23 @@ describe('equals() tests', () => { const coll3 = createCollectionFrom(['a', 2], ['b', 3], ['c', 3]); expect(coll2.equals(coll3)).toBeFalsy(); }); + + test('collections with undefined values should be compared correctly', () => { + const collWithUndefined1 = new Collection([ + ['a', 1], + ['b', undefined], + ]); + const collWithUndefined2 = new Collection([ + ['a', 1], + ['b', undefined], + ]); + const collWithDifferentKeys = new Collection([ + ['a', 1], + ['c', undefined], + ]); + expect(collWithUndefined1.equals(collWithUndefined2)).toBeTruthy(); + expect(collWithUndefined1.equals(collWithDifferentKeys)).toBeFalsy(); + }); }); describe('every() tests', () => { diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 2608a3f1b..f3520a6e8 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -822,7 +822,11 @@ export class Collection extends Map { if (this === collection) return true; if (this.size !== collection.size) return false; for (const [key, value] of this) { - if (!collection.has(key) || value !== collection.get(key)) { + const otherValue = collection.get(key); + // If values differ, collections aren't equal. + // For undefined values, we must also verify the key exists in the other collection, + // since get() returns undefined for both missing keys and keys with undefined values. + if (otherValue !== value || (otherValue === undefined && !collection.has(key))) { return false; } }