0

I have a React Native app in which I installed and used jail-monkey to check if the device is rooted. As soon as I added it, some of my Jest tests started failing with the following error:

SyntaxError: Cannot use import statement outside a module
> 3 | import JailMonkey from 'jail-monkey';

After googling I came upon this stack overflow thread which has many answers but neither of which helped me. That being said I imagine this problem has to do with the babel and jest configs - How to resolve "Cannot use import statement outside a module" in jest

My babel.config.js looks like this:

module.exports = {
    presets: ['module:metro-react-native-babel-preset'],
    plugins: [
        [
            require.resolve('babel-plugin-module-resolver'),
            {
                cwd: 'babelrc',
                extensions: ['.ts', '.tsx', '.ios.tsx', '.android.tsx', '.js'],
                alias: {
                    '@src': './src',
                },
            },
        ],
        [
            'module:react-native-dotenv',
            {
                moduleName: 'react-native-dotenv',
            },
        ],
        // Reanimated needs to be at the bottom of the list
        'react-native-reanimated/plugin',
    ],
};

And my jest.config.js looks like this:

const { defaults: tsjPreset } = require('ts-jest/presets');

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
    ...tsjPreset,
    preset: 'react-native',
    transform: {
        '^.+\\.jsx$': 'babel-jest',
    },
    // Lists all react-native dependencies
    // that don't have compiled ES6 code
    // and need to be ignored by the transformer
    transformIgnorePatterns: [
        'node_modules/(?!(react-native' +
            '|react-navigation-tabs' +
            '|react-native-splash-screen' +
            '|react-native-screens' +
            '|react-native-reanimated' +
            '|@react-native' +
            '|react-native-vector-icons' +
            '|react-native-webview' +
            ')/)',
    ],
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    moduleNameMapper: {
        // Help Jest map the @src's added by babel transform
        '^@src(.*)$': '<rootDir>/src$1',
        // Allow Jest to mock static asset imports
        '\\.(jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
            '<rootDir>/__mocks__/assetMock.js',
        // Mock SVG Component imports (from React Native SVG)
        '\\.svg': '<rootDir>/__mocks__/svgMock.js',
    },
    setupFiles: ['./jest.setup.js'],
    setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
};
Onyx
  • 5,186
  • 8
  • 39
  • 86

1 Answers1

0

I solved this issue by including jail-monkey in my transformIgnorePatterns on jest.config.js and them mocking the jailmonkey.js. In my case I have the file on __mocks__/jail-monkey/index.js with the following content:

export default {
  jailBrokenMessage: () => '',
  isJailBroken: () => false,
  androidRootedDetectionMethods: {
    rootBeer: {
      detectRootManagementApps: false,
      detectPotentiallyDangerousApps: false,
      checkForSuBinary: false,
      checkForDangerousProps: false,
      checkForRWPaths: false,
      detectTestKeys: false,
      checkSuExists: false,
      checkForRootNative: false,
      checkForMagiskBinary: false,
    },
    jailMonkey: false,
  },
  hookDetected: () => false,
  canMockLocation: () => false,
  trustFall: () => false,
  isOnExternalStorage: () => false,
  isDebuggedMode: () => Promise.resolve(false),
  isDevelopmentSettingsMode: () => Promise.resolve(false),
  AdbEnabled: () => false,
};