question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I tried to test a component that has Image, the test failed with an error

here is the message: ```● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/Users/mospro/Documents/Pharaohsoft/Mehan360/node_modules/react-native-elements/dist/index.js:6
import Button from './buttons/Button';
^^^^^^

SyntaxError: Cannot use import statement outside a module

  2 | import {useState, useEffect} from 'react';
  3 | import {View} from 'react-native';
> 4 | import {Image} from 'react-native-elements';
    | ^
  5 | import Loader from '../Loader';
  6 | const CustomImage = ({
  7 |   src,

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/components/Image/index.tsx:4:1)```

react-native-elements : “^3.4.2”, react-native : “0.66.0”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
mustafakameldevcommented, Jan 14, 2022

I solved this issue by adding this line "node_modules/(?!(jest-)?@react-native|react-native|react-native-elements/*)", on transformIgnorePatterns array on jest configuration to make the whole jest like this:

  "preset": "react-native",
  "globals": {
    "ts-jest": {
      "babelConfig": true
    }
  },
  "testEnvironment": "jsdom",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
  "moduleDirectories": [
    "node_modules",
    "src"
  ],
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)?$",
  "transformIgnorePatterns": [
    "node_modules/(?!(jest-)?@react-native|react-native|react-native-flash-message/*)",
    "node_modules/(?!(jest-)?@react-native|react-native|react-native-elements/*)",
  ],
  "setupFiles": [
    "./node_modules/react-native-gesture-handler/jestSetup.js"
  ],
  "cacheDirectory": ".jest/cache",
  "moduleNameMapper": {
    "^.+\\.(css|less|scss)$": "identity-obj-proxy"
  }

hope It will work for you @tulio-mcsantos

1reaction
jasonfmacommented, Nov 19, 2021

I have the same issue - I’ve gone through and followed the instructions at https://reactnativeelements.com/docs/web_usage/ and I still have the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

COVID-19 Testing: What You Need to Know - CDC
Viral tests look for a current infection with SARS-CoV-2, the virus that causes COVID-19, by testing specimens from your nose or mouth. There...
Read more >
Community-Based Testing Sites for COVID-19 - HHS.gov
Find Testing Resources in Your State. COVID-19 tests are available to everyone in the U.S., including the uninsured. Select your state below to...
Read more >
Get free at-⁠home COVID-⁠19 tests this winter
15,000+ Free Testing Sites. No-cost antigen and PCR COVID-⁠19 tests are available to everyone in the U.S., including the uninsured, at more than...
Read more >
Testing.com: Order Lab Tests and Blood Tests Online
Hundreds of easy-to-read lab testing guides. Confidential, secure and convenient online lab test ordering powered by trusted physician networks. Compassionate ...
Read more >
COVID-19 Testing and Locations | MinuteClinic - CVS
CVS Health is offering no cost coronavirus testing. Get a rapid COVID test for same day results, or a molecular lab test (PCR/NAAT)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found