Modern Environment For React Native Applications

TypeScript helps us avoid development mistakes and write a more efficient mobile application. Modern tools allow integrating TypeScript into the development environment. We can also use VS Code that supports TypeScript.
 
Integration with React Native will give us the opportunity to use the auto-completion service, code navigation, and refactoring.
 
Expo is a toolkit that simplifies the creation of native React applications. This tutorial will give you an idea of how you can quickly create
native React applications using Expo.
 

Creating a Project

 
First, we need to install all the dependencies needed to create the application template from yarn.
 
yarn global add expo-cli
 
Next, we initialize the React Native application and select the empty TypeScript template.
 
~/ expo init exampleApp
? Choose a template: expo-template-blank-typescript
✔ Please enter a few initial configuration values.
? Yarn v1.15.2 found. Use Yarn to install dependencies? Yes
 
Now, we can launch the project and begin developing the application.
 
cd exampleApp
yarn start
 

Tslint Configuration

 
Let's configure tslint to use VSCode or another editor correctly and see errors at the development stage.
 
This will ensure a uniform code style and prevent complications.
 
First, we will install the tslint package in the global directory.
 
yarn global add tslint
 
Next, let us create the tslint configuration.
 
tslint --init
 
This command will create the tslint.json file with the following configuration.
  1. {  
  2.   "extends": ["tslint:recommended"],  
  3.   "jsRules": {},  
  4.   "rules": {},  
  5.   "rulesDirectory": []  
  6. }  
Next, we ned to check the TypeScript files using our configuration.
 
tslint 'src/**/*.ts'
 
After that, VS Code will automatically use the configuration file to validate the code in the editor. Type errors will be displayed in the editor in the process of app development.
 
Also, for convenience, we add commands for yarn. These commands can be used for local development or for verification at the stage of continuous integration.
  1. "scripts": {  
  2.     "lint""tslint '*.ts*'"  
  3. }  
Tslint also makes it possible to extend validation rules by installing plugins. At this point, we will add the eslint rules support.
 
yarn add tslint-eslint-rules --dev
 
To add a plugin to the tslint configuration, we add the plugin name to the “extends” field,
  1. "extends": [  
  2.     "tslint:recommended",  
  3.     "tslint-eslint-rules"  
  4. ]  

Jest and tests on TypeScript

 
Jest is a framework for testing the JavaScript code. It facilitates the testing, support, and development of React Native applications.
 
First, you need to install the framework and add TypeScript support, since the codebase was written with support for static typing.
 
yarn add --dev jest ts-jest @types/jest
 
It is also worth installing additional packages.
  • ts-jest - for compiling and processing typescript test code in Javascript
  • @ types / jest - for adding types from the Jest environment
  • react-native-testing-library - for rendering React components without DOM
And the last step is to create a configuration file for the tests.
 
yarn ts-jest config:init
 
This command will generate a configuration file from the template. We will tailor it to the React Native environment. The file should look like this.
  1. module.exports = {  
  2.   jest: {  
  3.     preset: "react-native",  
  4.     transform: {  
  5.       "^.+\\.js$""./node_modules/react-native/jest/preprocessor.js",  
  6.       "\\.(ts|tsx)$""ts-jest"  
  7.     },  
  8.     globals: {  
  9.       "ts-jest": {  
  10.         tsConfig: "tsconfig.json"  
  11.       }  
  12.     },  
  13.     moduleFileExtensions: ["ts""tsx""js"],  
  14.     testRegex: "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$"  
  15.   }  
  16. };  
And finally, we should add the command to run the tests in package.json.
 
"test": "yarn test"
 

Writing Tests for Jest

 
Let's try to write a sample test for Jest. To do this, we will create an App.test.tsx file.
  1. export const helloFn = (name?: String = "World") => `Hello, ${$name}`;  
  2.   
  3. describe("hello function", () => {  
  4.   it("should return `hello world`", () => {  
  5.     expect(helloFn()).toEqual(`Hello, World`);  
  6.   });  
  7.   
  8.   it("should return `hello name`", () => {  
  9.     expect(helloFn("Zuck")).toEqual(`Hello, Zuck`);  
  10.   });  
  11. });  
To run the tests, we execute the previously created yarn command.
 
yarn test
 
 PASS ./App.test.tsx
hello function
✓ should return `hello world` (4ms)
✓ should return `hello name` (1ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.316s
 
Ran all test suites.
 
✨ Done in 2.74s.
 
If you do all of the above correctly, tests should pass successfully. But you will still not be able to write tests for components. Let's expand the environment for testing React Native components.
 
We need one more package - react-test-renderer. It provides a special rendering engine for React with a JavaScript structure on the output. Therefore, we do not need to configure DOM or native modules in the test environment.
 
yarn add -D react-test-renderer
 
Next, we will update our App.test.tsx file with a simple test for the App.tsx component.
  1. import React from "react";  
  2. import renderer from "react-test-renderer";  
  3. import App from "./App";  
  4.   
  5. describe("App", () => {  
  6.   it("should display welcome message", () => {  
  7.     const tree = renderer.create(<App />);  
  8.   
  9.     expect(tree.toJSON()).toMatchSnapshot();  
  10.     expect(tree.root.findByType("Text").children).toContain(  
  11.       "Open up App.tsx to start working on your app!"  
  12.     );  
  13.   });  
  14. });  
We can test native components in the test environment. In this example, we have obtained an array of child elements for the Text tag. This is a native component from the React Native package.
 

Conclusion

 
This technology stack has allowed us to quickly create an environment for developing native applications. Business logic based on static types makes the application more stable. TypeScript's strong typing also helps avoid coding errors.
 
Test development for React Native components within Jest is exactly the same as for regular React applications.
 
I hope this article will help you overcome the initial stage of setting up an environment for developing React Native applications.