Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Fails Jest unit test #38

Open
Thegrep01 opened this issue Nov 5, 2021 · 2 comments · May be fixed by #41
Open

Fails Jest unit test #38

Thegrep01 opened this issue Nov 5, 2021 · 2 comments · May be fixed by #41

Comments

@Thegrep01
Copy link

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
Copy link

any solution? , i am facing the same

@ArnabXD
Copy link

ArnabXD commented Jun 13, 2022

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"
+    }
  }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants