akveo / react-native-ui-kitten

:boom: React Native UI Library based on Eva Design System :new_moon_with_face::sparkles:Dark Mode
https://akveo.github.io/react-native-ui-kitten/
MIT License
10.28k stars 951 forks source link

Syntax Error with Optional Chaining in Typescript 3.9.7 #1220

Closed kuznetsov-from-wonderland closed 3 years ago

kuznetsov-from-wonderland commented 4 years ago

🐛 Bug Report

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:
 • 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/......./Projects/.../apps/....../node_modules/@ui-kitten/components/ui/autocomplete/autocomplete.component.js:95
            this.popoverRef.current?.show();
                                    ^

SyntaxError: Unexpected token '.'

To Reproduce

Steps to reproduce the behavior: jest.config.js

module.exports = {
  preset: 'react-native',
  setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
}

package.json

"dependencies": {
    "@dudigital/react-native-zoomable-view": "^1.0.15",
    "@eva-design/eva": "^2.0.0",
    "@react-native-community/async-storage": "^1.11.0",
    "@react-native-community/audio-toolkit": "^2.0.3",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "^5.9.2",
    "@react-native-community/push-notification-ios": "^1.2.2",
    "@react-native-firebase/analytics": "^7.1.4",
    "@react-native-firebase/app": "^7.2.1",
    "@react-native-firebase/messaging": "^7.1.5",
    "@react-navigation/bottom-tabs": "^5.5.2",
    "@react-navigation/drawer": "^5.8.2",
    "@react-navigation/native": "^5.5.1",
    "@react-navigation/stack": "^5.5.1",
    "@sentry/react-native": "^1.4.5",
    "@types/axios": "^0.14.0",
    "@types/jest": "^26.0.10",
    "@types/js-base64": "^2.3.1",
    "@types/moment": "^2.13.0",
    "@types/ramda": "^0.27.6",
    "@types/react-native-communications": "^2.2.1",
    "@types/react-native-indicators": "^0.16.0",
    "@types/react-native-share": "^3.3.0",
    "@typescript-eslint/eslint-plugin": "^3.2.0",
    "@typescript-eslint/parser": "^3.2.0",
    "@ui-kitten/components": "^5.0.0",
    "@ui-kitten/eva-icons": "^5.0.0",
    "axios": "^0.19.2",
    "buffer": "^5.6.0",
    "date-fns": "^2.14.0",
    "eslint": "^7.2.0",
    "eslint-config-react-native-wcandillon": "^3.0.1",
    "eslint-plugin-import": "^2.21.2",
    "jest": "^26.4.0",
    "js-base64": "^2.5.2",
    "moment": "^2.27.0",
    "nanoid": "^3.1.10",
    "prettier": "^2.0.5",
    "ramda": "^0.27.0",
    "react": "16.11.0",
    "react-native": "0.62.2",
    "react-native-communications": "^2.2.1",
    "react-native-device-info": "^5.6.1",
    "react-native-fast-image": "^8.3.2",
    "react-native-flash-message": "^0.1.15",
    "react-native-fs": "^2.16.6",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-haptic-feedback": "^1.10.0",
    "react-native-image-crop-picker": "^0.32.2",
    "react-native-image-size": "^1.1.3",
    "react-native-image-zoom-viewer": "^3.0.1",
    "react-native-indicators": "^0.17.0",
    "react-native-keyboard-aware-scroll-view": "^0.9.1",
    "react-native-navigation-bar-color": "^2.0.1",
    "react-native-permissions": "^2.1.5",
    "react-native-push-notification": "^3.5.2",
    "react-native-reanimated": "^1.9.0",
    "react-native-redash": "^14.1.1",
    "react-native-safe-area-context": "^3.0.5",
    "react-native-screens": "^2.8.0",
    "react-native-share": "^3.7.0",
    "react-native-sound-recorder": "^1.3.6",
    "react-native-splash-screen": "^3.2.0",
    "react-native-status-bar-height": "^2.5.0",
    "react-native-svg": "^12.1.0",
    "react-native-video": "^4.4.5",
    "react-native-video-controls": "^2.6.0",
    "react-native-webview": "^10.3.2",
    "rn-secure-storage": "^2.0.4",
    "ts-jest": "^26.2.0",
    "use-memo-one": "^1.1.1"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.6.2",
    "@testing-library/jest-native": "^3.3.0",
    "@testing-library/react-native": "^7.0.2",
    "@types/react-native": "^0.62.0",
    "@types/react-test-renderer": "16.9.2",
    "babel-jest": "^24.9.0",
    "husky": "^4.2.5",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-test-renderer": "16.11.0",
    "typescript": "^3.8.3"
  }

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
}

tsconfig.json


{
  "compilerOptions": {
    /* Basic Options */
    "target": "es2019",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": ["es2019"],                           /* Specify library files to be included in the compilation. */
    "allowJs": true,                          /* Allow javascript files to be compiled. */
    "skipLibCheck": true,
    // "checkJs": true,                       /* Report errors in .js files. */
    "jsx": "react-native",                    /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    // "outDir": "./",                        /* Redirect output structure to the directory. */
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "removeComments": true,                /* Do not emit comments to output. */
    "noEmit": true,                           /* Do not emit outputs. */
    // "incremental": true,                   /* Enable incremental compilation */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    "isolatedModules": true,                  /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    "moduleResolution": "node",               /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "baseUrl": "./",                          /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    "allowSyntheticDefaultImports": true,     /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */

    /* Source Map Options */
    // "sourceRoot": "./",                    /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "./",                       /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
  },
  "exclude": [
    "node_modules", "babel.config.js", "metro.config.js", "jest.config.js"
  ]
}

ok.test.tsx

import React from 'react'

import { render } from '@testing-library/react-native'
import { View, Text } from 'react-native'

describe('<Component />', () => {
  test('should render with no errors', () => {
    const props = { title: '123', value: '456' }
    const { debug } = render(
      <View>
        <Text>{props?.vavava || 'hi'}</Text>
      </View>
    )
    debug()
  })
})

image

failed.test.tsx

import React from 'react'

import { render } from '@testing-library/react-native'
import { Layout as View, Text } from '@ui-kitten/components'

describe('< Component />', () => {
  test('should render with no errors', () => {
    const props = { title: '123', value: '456' }
    const { debug } = render(
      <View>
        <Text>{props?.vavava || 'hi'}</Text>
      </View>
    )
    debug()
  })
})

image

Expected behavior

Link to runnable example or repository (highly encouraged)

UI Kitten and Eva version

Package Version
@eva-design/eva 5.0.0
@ui-kitten/components 5.0.0

And I also know that all UI-Kitten components should be rendered in a provider. I have a custom render fn just like this

  return (
    <ApplicationProvider mapping={mapping} theme={currentTheme} customMapping={currentMapping}>
      {children}
    </ApplicationProvider>
  )

this, by the way, does not work ether, because of the same transpilation error

The babel transpilation magic seems to work just fine according to first example. I have no idea of why this can be broken if I use any @ui-kitten component

You are welcome!

Environment information

node v12.13.1
macos v10.15.6
kuznetsov-from-wonderland commented 4 years ago

I guess this issue will probably disappear if I upgrade to Node v14 ☝️

lesmo commented 3 years ago

This is sad. I've had to forcibly transpile it with Babel, and I'm not sure if it's the reason I'm unable to have UI KItten properly tree-shaken 🤔

kuznetsov-from-wonderland commented 3 years ago

Node v14 solves this problem

lesmo commented 3 years ago

Please add to documentation that Node v14 is now a requirement.

icgarlin commented 3 years ago

Upgrading to Node v14 does not work for me