realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Jest test: "Cannot find module 'realm'"

Open ldoot opened this issue 5 months ago • 11 comments

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

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

ldoot avatar Mar 19 '24 10:03 ldoot

➤ PM Bot commented:

Jira ticket: RJS-2766

sync-by-unito[bot] avatar Mar 19 '24 10:03 sync-by-unito[bot]

@ldoot Thank you for reporting.

In v12.6.1 we migrated away from rollup, and I wonder if it has an impact on Jest. Is it possible to try v12.6.0 to give us an indication?

kneth avatar Mar 20 '24 08:03 kneth

@kneth thanks for you response.

We are using v12.6.2 due to the export of the Unmanaged type, however I reverted back to 12.6.0 to test your assertion and it still falls over on the import, albeit this time with a TypeError rather than an 'Cannot find module' error.

 FAIL  src/__tests__/useStartOfDayData.spec.tsx
  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'bind')

      1 | import React from "react";
    > 2 | import Realm from "realm";

Let me know if there's any more information I can provide to help with this.

ldoot avatar Mar 22 '24 10:03 ldoot

   Cannot find module '/Users/diongrendelman/Projects/verbleif/verbleifapp/node_modules/realm/binding/build/realm.node' from '/Users/diongrendelman/Projects/verbleif/verbleifapp/node_modules/realm/binding/generated'

    Require stack:
      node_modules/realm/binding/generated/native.node.cjs
      node_modules/realm/dist/platform/node/binding.js
      node_modules/realm/dist/platform/node/index.js
      node_modules/realm/index.react-native.js
      src/database/schemas/ClientSchema.ts
      src/database/realm.config.ts

Same issue here

"realm": "^12.6.2",

Acetyld avatar Mar 26 '24 20:03 Acetyld

Update, not happining on 12.6.0

Acetyld avatar Mar 26 '24 20:03 Acetyld

I had the same problem. Doesn't happen on 12.6.0.

villemuittari avatar Mar 27 '24 08:03 villemuittari

I appreciate that this issue may be fixed with a downgrade as mentioned to be working by Acetyld and villemuittari, however as we are making use of the Unmanged type exported in v12.6.2 this can't work in our case and I suspect the case of others too. Is there any other possible fix for this issue?

ldoot avatar Apr 04 '24 10:04 ldoot

same issue on 12.7.0+, rollback to 12.6.0 working 👍

huneau avatar Apr 17 '24 14:04 huneau

this seems to be an issue when using expo with realm 12.7.0

houssam-adesso avatar Apr 19 '24 11:04 houssam-adesso

@houssam-adesso @huneau Please create a new issue with more details.

kneth avatar Apr 19 '24 11:04 kneth

@houssam-adesso @huneau Please create a new issue with more details.

https://github.com/realm/realm-js/issues/6630

houssam-adesso avatar Apr 19 '24 12:04 houssam-adesso