From c1fc0860de75dbbbc031b93c08520f1a1fd45ba1 Mon Sep 17 00:00:00 2001 From: FFranck Date: Mon, 26 Jul 2021 17:33:57 +0200 Subject: [PATCH] Fix for Set prototype resolution (#3843) Fixed issue that could result in stack overflow when using Set iterators. --- CHANGELOG.md | 4 ++-- lib/collection-methods.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c479653809..2798138cd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ x.x.x Release notes (yyyy-MM-dd) * None. ### Fixed -* Fixed TypeScript definition of `Realm.Dictionary.remove()`. (since v10.6.0) -* Fixed Realm.Object#toJSON as it threw "Right-hand side of 'instanceof' is not an object". (since v10.6.0) +* Fix TypeScript definition of `Realm.Dictionary.remove()`. (since v10.6.0) +* Fixed `Realm not defined` error experienced when using `Realm.Set` iterators under Jest ### Compatibility * MongoDB Realm Cloud. diff --git a/lib/collection-methods.js b/lib/collection-methods.js index 76b56cbb18..0e1e395b3c 100644 --- a/lib/collection-methods.js +++ b/lib/collection-methods.js @@ -55,7 +55,9 @@ Object.defineProperty(iteratorPrototype, Symbol.iterator, { ["entries", "keys", "values"].forEach(function (methodName) { var method = function () { - const isSet = this instanceof Realm.Set; + // checks for Set datatype. Required for compatibility with node.js, Jest, and RN + // instanceof Set is not performing correctly in Jest tests, so we need to use an alternate method + const isSet = (this.constructor && this.constructor.name === "Set") || this instanceof Realm.Set; var self = this.type === "object" || isSet ? this.snapshot() : this; var index = 0;