Jest Testing React Native cannot use import statement outside of a module
Solution 1:
I've tried out a lot of changes to my .babelrc
file, but I eventually fixed the error by converting my .babelrc
file to a babel.config.js
file that currently looks like this:
module.exports = function(api) {
api.cache(true);
return {
presets: [
'module:metro-react-native-babel-preset',
'module:react-native-dotenv',
'@babel/preset-flow',
],
plugins: [
[
'module-resolver',
{
root: ['./src'],
extensions: ['.js', '.ios.js', '.android.js'],
alias: {
api: './src/api',
assets: './src/assets',
services: './src/services',
styles: './src/styles',
components: './src/components',
app: './src',
},
},
],
['transform-class-properties'],
],
};
};
Aside from this I've added a lot of mocks inside my __mocks__
directory for different packages like for example react-native-firebase
. I pretty sure that the mock files I added didn't have a part in fixing the error in my question, but there were other similar errors that were fixed by adding mock files.
By similar errors I mean errors that have an error message like:
SyntaxError: Cannot use import statement outside a module