ptelad / react-native-iphone-x-helper

A library to help you design your react-native app for notched iPhones
MIT License
942 stars 138 forks source link

Fails Jest unit test #38

Open Thegrep01 opened 3 years ago

Thegrep01 commented 3 years ago

In some units tests, it starts failing with logs

    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { Dimensions, Platform, StatusBar } from 'react-native';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (node_modules/react-native-paper/lib/commonjs/components/BottomNavigation.tsx:13:1)
ahsanTradeling commented 2 years ago

any solution? , i am facing the same

ArnabXD commented 2 years ago

Create a file __jest__/assetTransformer.js. (You can create the file anywhere in the project, make sure to fix the path in package.json)

const path = require('path');

module.exports = {
  process(src, filename, config, options) {
    return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
  },
};

now edit jest key of package.json or edit jest config file (whichever you have)

  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
+    "transformIgnorePatterns": [
+      "node_modules/(?!(@react-native|react-native|react-native-iphone-x-helper)/)"
+    ],
+    "moduleNameMapper": {
+      "\\.(jpg|jpeg|png|gif)$": "<rootDir>/__jest__/assetTransformer.js"
+    }
  }