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

Cannot find module 'realm' from *** while using jest #6630

Open
houssam-adesso opened this issue Apr 19, 2024 · 3 comments
Open

Cannot find module 'realm' from *** while using jest #6630

houssam-adesso opened this issue Apr 19, 2024 · 3 comments

Comments

@houssam-adesso
Copy link

How frequently does the bug occur?

Always

Description

After updating Realm to 1.27.0, any realm import inside my *.test.ts or inside my application code it throws an Error, that it cannot find module 'realm'.

But if i start my react native (expo) application, everything works fine.

My Jest Config:

import { pathsToModuleNameMapper } from 'ts-jest';
// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
import type { JestConfigWithTsJest } from 'ts-jest';

import { compilerOptions } from './tsconfig.json';

const jestConfig: JestConfigWithTsJest = {
    preset: 'jest-expo', // Use the react native preset instead of jest-expo
    transform: {
        '^.+\\.[jt]sx?$': 'babel-jest',
    },
    /** Either execute tests from global __tests__ directory or those matched by file name with *.test.ts(x) pattern */
    testMatch: ['**/src/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(test).[jt]s?(x)'],
    coverageReporters: ['cobertura', 'lcov'],
    collectCoverage: true,
    testResultsProcessor: 'jest-sonar-reporter',
    coveragePathIgnorePatterns: [
        'node_modules',
        'jest.setup.tsx',
        '<rootDir>/src/__tests__',
    ],
    setupFilesAfterEnv: [
        './node_modules/react-native-gesture-handler/jestSetup.js',
        './node_modules/jest-expo/src/preset/setup.js',
        './src/__tests__/jest.setup.tsx',
    ],
    testPathIgnorePatterns: [
        '<rootDir>/node_modules/',
        '<rootDir>/src/__tests__/jest.setup.tsx',
        '<rootDir>/src/__tests__/__mocks__/',
        '<rootDir>/src/__tests__/__mockdata__/',
        '<rootDir>/src/__tests__/__utils__/',
    ],
    testEnvironment: 'jsdom',
    roots: ['<rootDir>'],
    modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
    moduleNameMapper: {
        ...pathsToModuleNameMapper(compilerOptions.paths /* , { prefix: '<rootDir>/' } */),
        '\\.svg': '<rootDir>/src/__tests__/__mocks__/svgMock.ts',
    },
    moduleDirectories: ['node_modules', 'src/__tests__/__utils__', '__dirname'],
    verbose: true,
};

export default jestConfig;

Babel Config:

module.exports = function (api) {
    api.cache(true);
    return {
        presets: ['@babel/preset-typescript', 'babel-preset-expo'],
        plugins: [
            ['@babel/plugin-proposal-decorators', { legacy: true }],
        ],
    };
};

Stacktrace & log output

Cannot find module 'realm' from 'src/__tests__/services/**/***.test.ts'

    > 1 | import Realm from 'realm';
        | ^
      2 |

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.require (src/__tests__/services/**/**.test.ts:1:1)

Can you reproduce the bug?

Always

Reproduction Steps

npx jest

Version

1.27.0

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

MacOS 14.4.1

Build environment

"expo": "~50.0.8",
"react-native": "0.73.5",
"ts-jest": "^29.1.2",
"babel-jest": "^29.7.0",
"jest-expo": "~50.0.2",
"jest-junit": "^16.0.0",
"jest-sonar-reporter": "^2.0.0",
"jest-specific-snapshot": "^8.0.0",

Cocoapods version

No response

Copy link

sync-by-unito bot commented Apr 19, 2024

➤ PM Bot commented:

Jira ticket: RJS-2807

@houssam-adesso
Copy link
Author

The last Version where it works was 12.5.1

@ApacheEx
Copy link

ApacheEx commented Jun 3, 2024

I've the same issue when using 12.9.0, as workaround this might help (adjust your jest config):

moduleNameMapper: {
  '^@realm/fetch$': '<rootDir>/node_modules/@realm/fetch/dist/react-native/react-native.js',
  '^realm$': '<rootDir>/node_modules/realm/dist/platform/node/index.js',
},

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

2 participants