Skip to content

Instantly share code, notes, and snippets.

@liamjones
Created October 14, 2021 14:29
Show Gist options
  • Save liamjones/cca9c81f26e0d998c9904d734e9be400 to your computer and use it in GitHub Desktop.
Save liamjones/cca9c81f26e0d998c9904d734e9be400 to your computer and use it in GitHub Desktop.
Patch to fix `Realm is not defined` in Realm JS 10.5.0+
Patch Realm to fix 'Realm is not defined' issues in Jest and Chrome Remote Debugger, see https://github.com/realm/realm-js/pull/3748 for more info
Formatting below is deliberately bad (e.g. not indenting the function) to minimise the patch and make it easier to understand if it needs altering for a later version of Realm
diff --git a/node_modules/realm/lib/collection-methods.js b/node_modules/realm/lib/collection-methods.js
index 0e1e395..0c52173 100644
--- a/node_modules/realm/lib/collection-methods.js
+++ b/node_modules/realm/lib/collection-methods.js
@@ -18,6 +18,10 @@
/* global Realm */
+module.exports = function (realmConstructor) {
+
+var exports = {}
+
var arrayPrototype = Array.prototype;
// eslint-disable-next-line no-undef
@@ -57,7 +61,7 @@ Object.defineProperty(iteratorPrototype, Symbol.iterator, {
var method = function () {
// 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;
+ const isSet = (this.constructor && this.constructor.name === "Set") || this instanceof realmConstructor.Set;
var self = this.type === "object" || isSet ? this.snapshot() : this;
var index = 0;
@@ -93,3 +97,7 @@ Object.defineProperty(iteratorPrototype, Symbol.iterator, {
});
exports[Symbol.iterator] = exports.values;
+
+return exports
+
+}
diff --git a/node_modules/realm/lib/extensions.js b/node_modules/realm/lib/extensions.js
index 57ec5a5..54a7ab8 100644
--- a/node_modules/realm/lib/extensions.js
+++ b/node_modules/realm/lib/extensions.js
@@ -42,7 +42,7 @@ function openLocalRealm(realmConstructor, config) {
module.exports = function (realmConstructor) {
// Add the specified Array methods to the Collection prototype.
- Object.defineProperties(realmConstructor.Collection.prototype, require("./collection-methods"));
+ Object.defineProperties(realmConstructor.Collection.prototype, require("./collection-methods")(realmConstructor));
Object.defineProperties(realmConstructor.Set.prototype, require("./set-methods")(realmConstructor));
realmConstructor.DictionaryProxy = require("./dictionary").DictionaryProxy;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment