Setting up Jest for testing React Native applications

From: React Native Test Cases , Jest Last Updated: 10 months ago

Step 1: Install Jest and related dependencies

npm install --save-dev jest @testing-library/react-native react-test-renderer

 

Step 2: Create a Jest configuration file

// jest.config.js
module.exports = {
  preset: '@testing-library/react-native',
  transform: {
    '^.+\\.js$': 'babel-jest',
  },
};

 

Step 3: Configure Babel for Jest

babel.config.js
{
  "presets": ["module:metro-react-native-babel-preset"]
}

 

Step 4: Add a script to run tests in package.json

// package.json
{
  "scripts": {
    "test": "jest"
  }
}

 

Step 5: Run the tests

npm test