[英]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.