How to fix "Support for the experimental syntax 'classProperties' isn't currently enabled" error if it has been already enabled? - react-native

Im building a crossplatform monorepo application for Web, Android and iOS from this example https://github.com/brunolemos/react-native-web-monorepo and when I added React Native Base to my project I changed my config-overrides.js according to this guide https://docs.nativebase.io/docs/GetStarted.html for Web
But after that I got
SyntaxError: C:\Users\maksi\Desktop\ecmsk\node_modules\native-base\node_modules\react-native-vector-icons\lib\create-icon-set-from-fontawesome5.js: Support for the experimental syntax 'classProperties' isn't currently enabled error.
I tried adding loose option to #babel/plugin-proposal-class-properties plugin but that didn't work. And I also tried adding plugins to my package.json and that didn't work either.
package.json
{
"name": "web",
"version": "0.0.1",
"private": true,
"dependencies": {
"#types/history": "^4.7.2",
"#types/js-cookie": "^2.2.2",
"#types/react": "^16.8.23",
"#types/react-redux": "^7.1.1",
"#types/redux-api-middleware": "^3.0.1",
"connected-react-router": "^6.5.2",
"history": "^4.9.0",
"js-cookie": "^2.2.0",
"native-base": "^2.13.4",
"react": "16.8.4",
"react-art": "^16.8.6",
"react-dom": "16.8.4",
"react-native-elements": "^1.1.0",
"react-native-vector-icons": "^6.6.0",
"react-native-web": "0.10.0",
"react-redux": "^7.1.0",
"react-router": "^5.0.1",
"react-scripts": "2.1.8",
"redux": "^4.0.4",
"redux-api-middleware": "^3.0.1"
},
"scripts": {
"compile": "tsc -b",
"prestart": "npm run compile",
"start": "concurrently \"npm run compile -- -w\" \"npm run _start\"",
"_start": "react-app-rewired start",
"prebuild": "npm run compile",
"build": "react-app-rewired build",
"pretest": "npm run compile",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"#babel/plugin-proposal-class-properties": "^7.5.5",
"babel-plugin-react-native-web": "^0.11.5",
"concurrently": "*",
"customize-cra": "^0.4.1",
"react-app-rewired": "^2.1.3",
"typescript": "*"
}
}
config-overrides.js
const path = require('path');
const {
override,
addWebpackAlias,
babelInclude,
addBabelPlugins
} = require('customize-cra');
module.exports = override(
addWebpackAlias({
"react-native/Libraries/Renderer/shims/ReactNativePropRegistry": "react-native-web/dist/modules/ReactNativePropRegistry",
"react-native": "react-native-web"
}),
babelInclude([
path.resolve('src'),
path.resolve('../components/src'),
path.resolve('../../node_modules/native-base-shoutem-theme'),
path.resolve('../../node_modules/react-navigation'),
path.resolve('../../node_modules/react-native-easy-grid'),
path.resolve('../../node_modules/react-native-drawer'),
path.resolve('../../node_modules/react-native-safe-area-view'),
path.resolve('../../node_modules/react-native-vector-icons'),
path.resolve('../../node_modules/react-native-keyboard-aware-scroll-view'),
path.resolve('../../node_modules/react-native-web'),
path.resolve('../../node_modules/react-native-tab-view'),
path.resolve('../../node_modules/static-container'),
]),
addBabelPlugins(
"#babel/plugin-proposal-class-properties"
),
);

The problem might be the included paths. In my case I have it this way:
babelInclude([
path.resolve('node_modules/react-native-typing-animation'),
path.resolve('src')
])
Paths are relative to the config.overrides file.

Related

Jest Testing with TypeORM

I'm trying to get Jest working with the TypeORM Expo-Example project
I've done a lot to try to get either ts-jest as well as expo-jest working. I have followed official guides for both Jest and Expo Jest. I was able to expo init a new empty project (not the TypeORM example) and get Expo-Jest working as expected, but when I to apply the changes to the TypeORM expo project, it ran into issues.
I think expo-jest is preferred. It will probably be easier to start from a clean clone of the TypeORM Expo-Example repo.
I've edited the package.json to a version that shouldn't have dependency issues if it is an easier starting point. (the jest stuff can be modified or removed in favor of separate config)
{
"name": "expo-example",
"version": "0.1.0",
"private": true,
"devDependencies": {
"#babel/core": "^7.18.0",
"#babel/preset-env": "^7.18.0",
"#types/jest": "^27.5.1",
"#types/node": "^10.1.3",
"#types/react": "~17.0.21",
"#types/react-native": "~0.67.6",
"#types/react-test-renderer": "^16.0.1",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"jest": "27",
"jest-expo": "^45.0.1",
"react-native-typescript-transformer": "^1.2.9",
"react-test-renderer": "17",
"typescript": "^4.6.4"
},
"scripts": {
"start": "expo start --dev-client",
"eject": "expo eject",
"android": "expo run:android",
"ios": "expo run:ios",
"test": "jest",
"postinstall": "patch-package"
},
"jest": {
"preset": "jest-expo",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"testPathIgnorePatterns": [
"\\.snap$",
"<rootDir>/node_modules/"
],
"cacheDirectory": ".jest/cache",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|#react-native(-community)?)|expo(nent)?|#expo(nent)?/.*|#expo-google-fonts/.*|react-navigation|#react-navigation/.*|#unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
]
},
"dependencies": {
"babel-jest": "27",
"expo": "^45.0.0",
"expo-splash-screen": "~0.15.1",
"expo-sqlite": "~10.2.0",
"expo-status-bar": "~1.3.0",
"patch-package": "^6.4.7",
"postinstall-postinstall": "^2.1.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.1",
"react-native-sqlite-storage": "^4.1.0",
"react-native-web": "0.17.7",
"reflect-metadata": "^0.1.13",
"ts-jest": "27",
"tslib": "^1.9.1",
"typeorm": "^0.3.6"
}
}
I have the same issue, my problem is an error when i try test the return of getConnection() typeorm function or DataSource({...}).initialize() with expo-sqlite driver... to make it work i use slite3 lib instead expo-sqlite... so it work, see: https://github.com/EmanuelVogt/expo-sqlite-typeorm-jest

Why Jest coverage creates an .html file from every component?

I'm testing a React Native app.
I run jest --coverage and I get 233 files:
I still get the coverage file and I can see the coverage through index.html.
But why the other files.
Here is the package.json
{
"name": "entoutonika",
"version": "0.0.1",
"private": true,
"scripts": {
"8": "react-native run-ios --simulator=\"iPhone 8 Plus\" ",
"11": "react-native run-ios --simulator=\"iPhone 11 Pro Max\" ",
"ios": "react-native run-ios",
"and": "react-native run-android",
"se": "react-native run-ios --simulator=\"iPhone SE (2nd generation)\" ",
"ipad13": "react-native run-ios --simulator=\"iPad Pro (12.9-inch) (5th generation)\" ",
"air": "react-native run-ios --simulator=\"iPad Air (4th generation)\" ",
"start": "react-native start",
"test": "jest --runInBand --detectOpenHandles --forceExit",
"watch": "jest --watch",
"cov": "jest --coverage",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"typecheck": "npx tsc",
"checkformat": "prettier --check \"**/*.{js,jsx,ts,tsx,json}\"",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json}\"",
"prepare": "npm run typecheck && lint-staged && husky install",
"postinstall": "npx typesync"
},
"dependencies": {
"#react-native-community/async-storage": "^1.12.1",
"#react-native-community/netinfo": "^7.1.9",
"#react-navigation/drawer": "^6.1.8",
"#react-navigation/elements": "^1.2.1",
"#react-navigation/native": "^6.0.6",
"#react-navigation/native-stack": "^6.2.5",
"firebase": "^9.4.1",
"formik": "^2.2.9",
"lottie-react-native": "^4.1.3",
"moment": "^2.29.1",
"react": "17.0.2",
"react-native": "0.66.1",
"react-native-bootsplash": "^4.0.2",
"react-native-code-push": "^7.0.4",
"react-native-gesture-handler": "^1.10.3",
"react-native-image-picker": "^4.3.0",
"react-native-reanimated": "^2.2.3",
"react-native-safe-area-context": "^3.3.2",
"react-native-screens": "^3.10.1",
"react-native-sound": "^0.11.1",
"react-native-vector-icons": "^9.0.0",
"react-redux": "^7.2.5",
"redux": "^4.1.1",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.3.0",
"typescript": "^4.4.4",
"yup": "^0.32.11"
},
"devDependencies": {
"#babel/core": "^7.12.9",
"#babel/preset-typescript": "^7.16.7",
"#babel/runtime": "^7.12.5",
"#react-native-community/eslint-config": "^2.0.0",
"#testing-library/jest-native": "^4.0.4",
"#testing-library/react": "^12.1.2",
"#testing-library/react-native": "^9.0.0",
"#types/babel__core": "^7.1.16",
"#types/eslint": "^7.28.2",
"#types/jest": "^26.0.24",
"#types/mocha": "^9.0.0",
"#types/react": "17.0.2",
"#types/react-native": "^0.65.0",
"#types/react-native-vector-icons": "^6.4.9",
"#types/react-redux": "^7.1.20",
"#types/react-test-renderer": "^17.0.1",
"#typescript-eslint/eslint-plugin": "^5.1.0",
"#typescript-eslint/parser": "^5.1.0",
"babel-jest": "^26.6.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-testing-library": "^5.0.5",
"husky": "^7.0.0",
"jest": "^26.6.3",
"jest-fetch-mock": "^3.0.3",
"lint-staged": ">=10",
"metro-react-native-babel-preset": "^0.66.2",
"react-test-renderer": "^17.0.2",
"redux-devtools-extension": "^2.13.9"
},
"resolutions": {
"#types/react": "^17"
},
"jest": {
"verbose": true,
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"setupFilesAfterEnv": [
"#testing-library/jest-native/extend-expect"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/assetsTransformer.js",
"\\.(css|less)$": "<rootDir>/assetsTransformer.js"
},
"moduleDirectories": [
"node_modules",
"src"
],
"transform": {
"^.+\\.js$": "babel-jest"
},
"transformIgnorePatterns": [
"/node_modules/(?!native-base)/"
],
"setupFiles": [
"./setupJest.js"
]
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": "eslint --cache --fix",
"*.{js,jsx,ts,tsx, json}": "prettier --write"
},
"rnpm": {
"assets": [
"./assets/fonts/"
]
}
}
What I do now is check the coverage, and then delete all the files.
I don't have this issue with React projects.
Is there something wrong with the configuration?
Those files are expected since are part of the repo itself so you can see lines of code covered by your tests etc.
You need to add the folder where those are generated into your .gitignore since the whole coverage shouldn't be tracked in your repo.

Module exists in package.json but not found on firebase

I have a module in my nextjs project which is mentioned under dependencies in package.json. It works locally. But when I build and deploy to firebase function I get the following error
Cannot find module 'react-lazyload'
My package.json
{
"name": "ui",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"export": "next export",
"predeploy": "next build",
"deploy": "firebase use popcorntales-40767 & firebase deploy"
},
"engines": {
"node": "10"
},
"dependencies": {
"#material-ui/core": "^4.11.0",
"#material-ui/icons": "^4.9.1",
"#material-ui/lab": "^4.0.0-alpha.56",
"#zeit/next-css": "^1.0.1",
"bootstrap": "^4.5.0",
"cacheable-response": "^2.1.0",
"chart.js": "^2.9.3",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
"next": "9.4.4",
"prop-types": "^15.7.2",
"react": "16.13.1",
"react-bootstrap": "^1.0.1",
"react-chartkick": "^0.4.0",
"react-dom": "16.13.1",
"react-lazyload": "^3.0.0",
"react-responsive-carousel": "^3.2.9",
"react-share": "^4.2.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
}
}
Commands used
1) next build
2) firebase deploy

Babel configuration for react-native Expo

so i am trying to run this react-native app built on expo i am having trouble configuring things.
here's my .babelrc
{
"presets": ["#babel/preset-env","#babel/react"],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
my package.json
{
"name": "APP NAME",
"version": "0.1.0",
"private": true,
"devDependencies": {
"#babel/preset-react": "^7.9.4",
"babel-eslint": "^10.0.1",
"babel-preset-env": "^1.7.0",
"eslint": "^5.7.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^21.25.1",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "^7.11.1",
"jest-expo": "^37.0.0",
"react-native-debugger-open": "^0.3.17",
"react-native-scripts": "1.11.1",
"react-test-renderer": "16.2.0"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"lint": "eslint --ignore-path .gitignore .",
"lint:fix": "eslint --fix --ignore-path .gitignore .",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js",
"debug": "open 'rndebugger://set-debugger-loc?host=localhost&port=19001'"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#react-native-community/viewpager": "^3.3.0",
"axios": "^0.18.0",
"babel-preset-react-native": "^4.0.1",
"date-fns": "^1.29.0",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"expo": "^37.0.0",
"lodash": "^4.17.10",
"prop-types": "^15.6.2",
"react": "16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-app-intro-slider": "^0.2.4",
"react-native-chart-kit": "^1.2.1",
"react-native-datepicker": "^1.7.2",
"react-native-hr-component": "^1.0.6",
"react-native-mock-render": "^0.1.2",
"react-native-modal-datetime-picker": "^6.0.0",
"react-native-paystack": "^3.1.4",
"react-native-picker-select": "^5.1.0",
"react-native-scrollable-tab-view": "^1.0.0",
"react-native-tab-view": "^2.14.0",
"react-native-vector-icons": "^4.6.0",
"react-native-webview-bridge": "^0.33.0",
"react-navigation": "^2.9.3",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-devtools-extension": "^2.13.5",
"redux-logger": "^3.0.6",
"redux-mock-store": "^1.5.3",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.2.0",
"styled-components": "^3.2.3",
"whatwg-fetch": "^2.0.4"
}
}
so at first i had an error like
node_modules/react-native-scrollable-tab-view/node_modules/#react-native-community/viewpager/js/index.js: Plugin/Preset files are not allowed to export objects, only functions. In node_modules/babel-preset-expo/index.js
this got fixed when i remove "babel-preset-expo" from my .babelrc and replaced it with the above content now i am having another error like this:
SyntaxError: /node_modules/react-native-scrollable-tab-view/node_modules/#react-native-community/viewpager/js/index.js: Unexpected token (10:12)
8 | */
9 |
> 10 | import type {PageScrollState as _PageScrollState} from './types';
| ^
11 | import type {PageScrollEvent as _PageScrollEvent} from './types';
12 | import type {PageScrollStateChangedEvent as _PageScrollStateChangedEvent} from './types';
13 | import type {PageSelectedEvent as _PageSelectedEvent} from './types';
Failed building JavaScript bundle
so any ideas why this is happening and how to fix it would be appreciated. Thanks
EDIT:
okay so i have added "react-native" to the babel presets array and the previous error i gone but now i got a new error saying:
node_modules/react-navigation-deprecated-tab-navigator/node_modules/react-native-tab-view/src/TabViewPagerExperimental.js: Cannot read property 'bindings' of null
If there is no .babel.rc present, you can create your own. Add whatever else you may need in there just as you would with .babelrc and it should work. I tried this out yesterday on my Expo managed RN app and it seems to have worked.
// Filename: .babelrc
{
"presets": ["babel-preset-expo"],
"plugins": ["transform-decorators-legacy"],
"env": {
"development": {
"plugins": ["transform-react-jsx-source"]
}
}
}
Edit: This was initial answer...
Expo can be created with two workflows. You would get a .babelrc for a bare workflow, and a babel.config.js for a managed workflow.
If you have "babel.config.js" you could try the following instead of worrying about .babelrc
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: ["babel-plugin-proposal-class-properties"]
};
};

React Native Jest Error: Cannot find module 'warnOnce' from 'react-native-implementation.js'

I get this error when I run npm test. I've looked at a number of posts, solutions, etc. that suggest making edits to package.json, babel.config.js, jest.config.js and I still receive this error. I thought it could be a version issue with React Native, but I'm on the latest version 0.60.4 and it's mentioned in a few issues that this is solved in master. Perhaps I'm overlooking something, but I'm coming up short on what else it could be.
The following is the test that fails as a result:
import { geolocationRequest } from '../location';
let mockGeoCoding = jest.fn();
jest.mock('react-native-geocoding', () => ({
openURL: mockGeoCoding,
}));
describe('geolocationRequest', () => {
it('creates a properly formatted action', () => {
expect(geolocationRequest()).toMatchSnapshot();
})
})
Thoughts?
package.json
{
"name": "<app-name>",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start --config ../../../../rn-cli.config.js",
"start:ios": "react-native run-ios",
"start:android": "react-native run-android",
"clean:ios": "cd ios/ && pod deintegrate && pod install",
"clean:android": "cd android && ./gradlew clean",
"test": "jest --watch",
"test:coverage": "jest --coverage && open coverage/lcov-report/index.html",
"fix": "eslint --fix .",
"lint:ts": "tslint --fix --project ./tsconfig.json",
"lint:fix": "npm run lint:ts --fix",
"lint:check": "tslint --type-check --project"
},
"dependencies": {
"#react-native-community/async-storage": "1.4.2",
"appcenter": "1.12.2",
"appcenter-analytics": "1.12.2",
"appcenter-crashes": "1.12.2",
"axios": "^0.19.0",
"expo-font": "4.0.0",
"polyline": "0.2.0",
"react": "16.8.6",
"react-devtools": "3.6.1",
"react-native": "0.60.*",
"react-native-animatable": "1.3.2",
"react-native-auth0": "1.4.2",
"react-native-easy-grid": "0.2.1",
"react-native-elements": "1.1.0",
"react-native-geocoding": "0.3.0",
"react-native-gesture-handler": "1.3.0",
"react-native-maps": "0.24.2",
"react-native-vector-icons": "6.4.2",
"react-navigation": "3.9.1",
"react-redux": "7.1.0",
"redux": "4.0.0",
"redux-thunk": "2.3.0",
"scheduler": "0.14.0"
},
"devDependencies": {
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/preset-typescript": "^7.3.3",
"#types/enzyme": "^3.10.3",
"#types/enzyme-adapter-react-16": "^1.0.5",
"#types/jest": "^24.0.17",
"#types/polyline": "0.1.28",
"#types/react": "16.8.13",
"#types/react-native": "0.57.43",
"#types/react-native-auth0": "1.3.0",
"#types/react-native-vector-icons": "6.4.0",
"#types/react-redux": "^7.1.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "24.3.1",
"babel-preset-expo": "5.1.1",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"enzyme-to-json": "^3.4.0",
"eslint": "5.15.1",
"eslint-config-airbnb": "17.1.0",
"eslint-plugin-import": "2.16.0",
"eslint-plugin-jsx-a11y": "6.2.1",
"eslint-plugin-react": "7.12.4",
"jest": "^24.3.1",
"jest-fetch-mock": "^2.1.2",
"jest-localstorage-mock": "^2.4.0",
"metro-react-native-babel-preset": "0.53.0",
"react-dom": "^16.8.6",
"react-native-typescript-transformer": "1.2.12",
"react-test-renderer": "^16.6.3",
"redux-mock-store": "^1.5.3",
"ts-jest": "^24.0.2",
"tslint": "5.16.0",
"typescript": "3.4.3"
},
"jest": {
"preset": "react-native"
}
}
jest.config.js
module.exports = {
"preset": 'react-native',
"verbose": true,
"setupFilesAfterEnv": ["<rootDir>/__tests__/setup.js", "jest-localstorage-mock"],
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest",
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"testPathIgnorePatterns": ['/node_modules/'],
"snapshotSerializers": ["enzyme-to-json/serializer"],
// "collectCoverageFrom": ["src/**/*.tsx"],
"collectCoverage": true,
};
babel.config.js
module.exports = {
"presets": ["module:metro-react-native-babel-preset"]
}
I've been struggling with the same issue.
Based on this comment on github From harudev
[...] So I tested some files to test project, and find out "react-native.config.js" file was the reason.
If there is someone suffered this issue, try to remove file or options from one of your "*.config.js" 😅
So I did remove all of my *.config.js file and the error is now gone.
Hopefully this could help you.
Note that you can reproduce the behavior of the *.confing.js file with package.json
For me this happened at an update of RN from 0.59.10 to 0.61.4.
I tried several fixes but nothing worked.
The only workaround that I've found was to set metro.config.js as below:
const blacklist = require('metro-config/src/defaults/blacklist');
module.exports = {
resolver: {
blacklistRE: blacklist([
/node_modules\/.*\/node_modules\/react-native\/.*/,
]),
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
Source: jamalx31's answer from here:
https://github.com/facebook/react-native/issues/24065#issuecomment-537489786
The issue is reported and documented here:
https://github.com/facebook/react-native/issues/24065
and here:
https://github.com/facebook/react-native/issues/23943
Had the same error, after a good amount of soul searching (google searching)
i just ran npx react-native start oposed to runing my local react-native.