Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

I couldn't run the unit test in react-native Jest?

$
0
0

I've RN App and want to run the first test, but I got this error

$ jest FAIL  __tests__/App-test.tsx● Test suite failed to run    SyntaxError: Unexpected token / in JSON at position 4        at JSON.parse (<anonymous>)      3 | import {I18nManager} from 'react-native';      4 | // the translations> 5 | const en = require('./lang/en.json');        |            ^      6 | const ar = require('./lang/ar.json');      7 |      8 | const resources = {      at Runtime._loadModule (node_modules/jest-runtime/build/index.js:796:59)      at Object.<anonymous> (src/i18n/index.ts:5:12)      at Object.<anonymous> (App.tsx:2:1)

The Root App Component

import React from 'react';import './src/i18n';....// Create a clientconst queryClient = new QueryClient();const App = () => {  return (<><QueryClientProvider client={queryClient}><SafeAreaView style={styles.topSafeArea} /><GeneralStatusBar backgroundColor={Colors.primary} /><AppContainer /></QueryClientProvider></>  );};export default App;

i18n file

import i18n from 'i18next';import {initReactI18next} from 'react-i18next';import {I18nManager} from 'react-native';// the translationsconst ar = require('./lang/ar.json');const en = require('./lang/en.json');const resources = {  en: {    translation: en,  },  ar: {    translation: ar,  },};i18n.use(initReactI18next).init({  resources,  lng: I18nManager.isRTL ? 'ar' : 'en',  fallbackLng: 'ar',  keySeparator: false,  interpolation: {    escapeValue: false,  },});export default i18n;

here are the configs

jest.config.js

module.exports = {  preset: 'react-native',  transform: {'^.+\\.tsx?$': 'babel-jest',  },  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],};

tsconfig.json

{"compilerOptions": {    /* Basic Options */"target": "esnext",                       /* 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": ["es6","ES2019"],                           /* Specify library files to be included in the compilation. */"allowJs": true,                          /* Allow javascript files to be compiled. */"jsx": "react-native",                    /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */"noEmit": true,                           /* Do not emit outputs. */"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. */    /* Module Resolution Options */"moduleResolution": "node",                               /* 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. */"resolveJsonModule": true,"esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */"skipLibCheck": true                      /* Skip type checking of declaration files. */ },"exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"  ]}

package.json

{"name": "myapp","version": "0.0.1","private": true,"scripts": {"android": "react-native run-android","ios": "react-native run-ios","start": "react-native start","test": "jest","lint": "eslint . --ext .js,.jsx,.ts,.tsx"  },"dependencies": {"@react-native-async-storage/async-storage": "^1.14.1","@react-native-community/masked-view": "^0.1.10","@react-native-community/netinfo": "^6.0.0","@react-navigation/bottom-tabs": "^5.11.7","@react-navigation/material-top-tabs": "^5.3.14","@react-navigation/native": "^5.9.2","@react-navigation/stack": "^5.14.2","axios": "^0.21.1","formik": "^2.2.6","i18next": "^19.8.7","react": "16.13.1","react-i18next": "^11.8.6","react-native": "0.63.4","react-native-bootsplash": "^3.2.3","react-native-confirmation-code-field": "^6.5.1","react-native-document-picker": "^5.0.3","react-native-gesture-handler": "^1.10.0","react-native-linear-gradient": "^2.5.6","react-native-reanimated": "^2.0.0","react-native-restart": "^0.0.22","react-native-safe-area-context": "^3.1.9","react-native-screens": "^2.17.1","react-native-select-dropdown": "^1.0.4","react-native-share": "^6.0.1","react-native-svg": "^12.1.0","react-native-tab-view": "^2.15.2","react-query": "^3.13.0","yup": "^0.32.9","zustand": "^3.3.1"  },"devDependencies": {"@babel/core": "^7.8.4","@babel/runtime": "^7.8.4","@react-native-community/eslint-config": "^1.1.0","@types/jest": "^25.2.3","@types/react-native": "^0.63.2","@types/react-native-restart": "^0.0.0","@types/react-test-renderer": "^16.9.2","babel-jest": "26.6.3","eslint": "^6.5.1","jest": "^25.1.0","metro-react-native-babel-preset": "^0.59.0","react-test-renderer": "16.13.1","typescript": "^3.8.3"  },"jest": {"preset": "react-native","setupFiles": ["./node_modules/react-native-gesture-handler/jestSetup.js","jest.config.js"    ]  }}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>