Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ldoot opened this issue Mar 19, 2024 · 11 comments
Open

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

ldoot opened this issue Mar 19, 2024 · 11 comments

Comments

@ldoot
Copy link

ldoot commented Mar 19, 2024

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

Copy link

sync-by-unito bot commented Mar 19, 2024

➤ PM Bot commented:

Jira ticket: RJS-2766

@kneth
Copy link
Contributor

kneth commented Mar 20, 2024

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

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Mar 20, 2024
@ldoot
Copy link
Author

ldoot commented Mar 22, 2024

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

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Mar 22, 2024
@Acetyld
Copy link

Acetyld commented Mar 26, 2024

   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
Copy link

Acetyld commented Mar 26, 2024

Update, not happining on 12.6.0

@villemuittari
Copy link

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

@ldoot
Copy link
Author

ldoot commented Apr 4, 2024

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
Copy link

huneau commented Apr 17, 2024

same issue on 12.7.0+, rollback to 12.6.0 working 👍

@houssam-adesso
Copy link

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

@kneth
Copy link
Contributor

kneth commented Apr 19, 2024

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

@houssam-adesso
Copy link

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

#6630

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants