Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1196

"Realm is not defined" when iterating through Realm Sets using Jest

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I'm getting the error: "ReferenceError: Realm is not defined" when attempting to iterate through a Realm set using Jest.

      Realm version: 10.5.0-beta.2 (although I've tested on 10.5.0-beta.1 as well)

      Sample unit test:

      test("should work with the Set data type", async () => {
          const characterSchema = {
            name: "Character",
            primaryKey: "_id",
            properties: {
              _id: "objectId",
              name: "string",
              levelsCompleted: "int<>",
              inventory: "string<>",
            },
          };
          const realm = await Realm.open({
            schema: [characterSchema],
          });
      
          let link, hunter;
          realm.write(() => {
            link = realm.create("Character", {
              _id: new BSON.ObjectId(),
              name: "Link",
              inventory: ["elixir", "compass", "glowing shield"],
              levelsCompleted: [4, 9],
            });
            hunter = realm.create("Character", {
              _id: new BSON.ObjectId(),
              name: "Hunter",
              inventory: ["estus flask", "gloves", "rune"],
              levelsCompleted: [1, 2, 5, 24],
            });
          });
          
          hunter.inventory.forEach((inventoryItem) => {
            console.log("The hunter has an inventory item:", inventoryItem);
          });
      
          realm.write(() => {
            realm.delete(link);
            realm.delete(hunter);
          });
          // close the realm
          realm.close();
        });
      

      Some notes:

      • Switching from a forEach loop to an alternative iterator method will still result in the error.
      • If you comment out the `forEach` method in the jest test you will not get the Realm undefined error.
      • If this code (minus the jest specific stuff) is run outside of a jest suite, for instance in a js file, it runs fine without error.

            Assignee:
            franck.franck@mongodb.com Franck Franck (Inactive)
            Reporter:
            mohammad.hunan@mongodb.com Mohammad Chughtai (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: