realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.62k stars 558 forks source link

Jest test: "Cannot find module 'realm'" #6562

Open ldoot opened 2 months ago

ldoot commented 2 months ago

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

sync-by-unito[bot] commented 2 months ago

➤ PM Bot commented:

Jira ticket: RJS-2766

kneth commented 2 months ago

@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?

ldoot commented 1 month ago

@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.

Acetyld commented 1 month ago
   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 commented 1 month ago

Update, not happining on 12.6.0

villemuittari commented 1 month ago

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

ldoot commented 1 month ago

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?

huneau commented 1 month ago

same issue on 12.7.0+, rollback to 12.6.0 working 👍

houssam-adesso commented 1 month ago

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

kneth commented 1 month ago

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

houssam-adesso commented 1 month ago

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

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