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

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

Open houssam-adesso opened this issue 1 year ago • 3 comments
trafficstars

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

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

➤ PM Bot commented:

Jira ticket: RJS-2807

sync-by-unito[bot] avatar Apr 19 '24 12:04 sync-by-unito[bot]

The last Version where it works was 12.5.1

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

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',
},

ApacheEx avatar Jun 03 '24 13:06 ApacheEx