简体   繁体   English

在我的 React Native 应用程序中使用 jail-monkey package 后,Jest 测试开始失败并显示“无法在模块外使用导入语句”

[英]Jest tests started failing with "Cannot use import statement outside a module" after using jail-monkey package in my React Native application

I have a React Native app in which I installed and used jail-monkey to check if the device is rooted.我有一个 React Native 应用程序,我在其中安装并使用 jail-monkey 来检查设备是否已植根。 As soon as I added it, some of my Jest tests started failing with the following error:一旦我添加它,我的一些 Jest 测试就开始失败并出现以下错误:

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话虽如此,我想这个问题与 babel 和 jest 配置有关 - 如何解决 jest 中的“无法在模块外使用导入语句”

My babel.config.js looks like this:我的babel.config.js看起来像这样:

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:我的jest.config.js看起来像这样:

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

I solved this issue by including jail-monkey in my transformIgnorePatterns on jest.config.js and them mocking the jailmonkey.js .我通过在jest.config.js上的transformIgnorePatterns中包含jail-monkey和它们 mocking jailmonkey.js解决了这个问题。 In my case I have the file on __mocks__/jail-monkey/index.js with the following content:在我的例子中,我在__mocks__/jail-monkey/index.js上有一个包含以下内容的文件:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest-Native“SyntaxError:无法在模块外使用导入语句” - Jest-Native "SyntaxError: Cannot use import statement outside a module" 使用 VSCode 调试器进行 Jest 测试会引发错误“SyntaxError: Cannot use import statement outside a module” - Using VSCode Debugger for Jest Tests throws error “SyntaxError: Cannot use import statement outside a module” 不能在模块外使用 import 语句 - React Native - Cannot use import statement outside a module - React Native 语法错误:无法在模块外使用导入语句:运行 Jest-expo 测试时 - SyntaxError: Cannot use import statement outside a module: when running Jest-expo tests Jest - SyntaxError: 无法在模块外使用 import 语句 - Jest - SyntaxError: Cannot use import statement outside a module React CKeditor Jest 错误:SyntaxError:无法在模块外使用导入语句 - React CKeditor Jest error: SyntaxError: Cannot use import statement outside a module Jest 或 Mocha 与 Vue:SyntaxError:无法在模块外使用 import 语句 - Jest or Mocha with Vue: SyntaxError: Cannot use import statement outside a module 开玩笑的单元测试 - SyntaxError:不能在模块外使用 import 语句 - Jest unit test - SyntaxError: Cannot use import statement outside a module 如何在玩笑中解决“SyntaxError: Cannot use import statement outside a module” - How to resolve "SyntaxError: Cannot use import statement outside a module" in jest Jest 和 Create-react-app:不能在模块外使用 import 语句 - Jest and Create-react-app: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM