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

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

Open houssam-adesso opened 4 weeks ago

houssam-adesso commented 4 weeks ago

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

sync-by-unito[bot] commented 4 weeks ago

➤ PM Bot commented:

Jira ticket: RJS-2807

houssam-adesso commented 4 weeks ago

The last Version where it works was 12.5.1