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

Jest test: "Cannot find module 'realm'"

      How frequently does the bug occur?

      Always

      Description

      Scenario: Running a Jest unit test suite which imports and interacts with Realm on an expo react-native codebase.

      Expected result: Successful import of and interaction with Realm during Jest test suite.
      Received result: When evaluating the test file, the line for importing Realm fails.

      This bug occurs on version 12.6.2 of Realm.
      This bug does NOT occur on version 12.3.0 Realm.

      In both cases, we have been using version 29.7.0 of Jest.

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Test log output:
      
       FAIL  src/__tests__/useStartOfDayData.spec.tsx
        ● Test suite failed to run
      
          Cannot find module 'realm' from 'src/__tests__/test.spec.tsx'
      
            1 | import React from "react";
          > 2 | import Realm from "realm";
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      Using Realm version 12.6.2 and Jest version 29.7.0:
      The bug is reproducible with the minimal set of Jest configuration:

      export default {
        preset: "react-native",
        testMatch: [ "<rootDir>/src/**/?(*.)+(spec|test).[jt]s?(x)" ],
        globalSetup: "<rootDir>/jest.setup.ts",
        testEnvironment: "jsdom",
        transform: {
          "^.+\\.jsx$": "babel-jest",
          "^.+\\.tsx?$": [ "ts-jest", { tsconfig: "tsconfig.jest.json" } ]
        }
      };
      

      And the corresponding babel configuration:

      const path = require('path');
      
      /** @type {import("@babel/core").ConfigFunction} */
      module.exports = function (api) {
        // @ts-ignore
        api.cache(true);
      
        return {
          presets: [ "babel-preset-expo" ],
          plugins: [
            "expo-router/babel"
          ]
        };
      };
      

      And some Jest test file that imports and uses Realm, such as:

      import Realm from "realm";
      
      test("hello world equals hello world", () => {
        console.log(Realm);
        const result = "hello world";
      
        expect(result).toBe("hello world");
      });
      

      Simply running the test will reproduce the issue. We've observed this issue on multiple people's machines.

      Version

      12.6.2

      What services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      MacOs Ventura 13.5

      Build environment

      Which debugger for React Native: Hermes

      Cocoapods version

      No response

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: