react-native-splash-screen icon indicating copy to clipboard operation
react-native-splash-screen copied to clipboard

[yarn test issue] SyntaxError: Cannot use import statement outside a module

Open zorba0517 opened this issue 3 years ago • 5 comments

react native info

스크린샷 2021-08-26 오전 10 10 56

Version Info: "3.2.0"

What platform does your issue occur on? (Android/iOS/Both)

=> Android

Problem

: when i run yarn test, it ran well without importing "react-native-splash screen" to App.tsx but, with it, i get error shown in below. 스크린샷 2021-08-26 오전 10 07 31

code

// babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ['babel-plugin-styled-components'],
    [
      'module-resolver',
      {
        root: ['./src']
      },
    ],
  ],
};

// in package.json
...
"jest": {
    "preset": "react-native",
    "verbose": true,
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }


// App.tsx
import React, { useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import SplashScreen from 'react-native-splash-screen';

import Profile from './Profile';

const App = () => {
  useEffect(() => {
    setTimeout(() => {
      SplashScreen.hide();
    }, 1000);
  }, []);
  return (
    <View style={styles.container}>
      <Profile userName={'samslow'} name={'서현석'} />
    </View>
  );
};

export default App;

zorba0517 avatar Aug 26 '21 01:08 zorba0517

same issue

vishaldhanotiyadev avatar Jun 14 '22 06:06 vishaldhanotiyadev

If it is also used on the web, I solved it like this.

import { NativeModules } from "react-native"

const RNSplashScreen = NativeModules.SplashScreen

dong-ho-lee avatar Jun 14 '22 06:06 dong-ho-lee

it occurs in mobile app

vishaldhanotiyadev avatar Jun 14 '22 06:06 vishaldhanotiyadev

I fixed the issue by mocking react-native-splash-screen into my jest.setup.js file like so:

jest.mock('react-native-splash-screen', () => {
  return {
    hide: () => true,
  };
});

riamon-v avatar Sep 06 '22 07:09 riamon-v

same issue here. any solutions

samuelhenshaw2020 avatar Jan 18 '23 12:01 samuelhenshaw2020