Performance testing companion for React and React Native.
Read The Docs
The problem
You want your React Native app to perform well and fast at all times. As a part of this goal, you profile the app, observe render patterns, apply memoization in the right places, etc. But it's all manual and too easy to unintentionally introduce performance regressions that would only get caught during QA or worse, by your users.
This solution
Reassure allows you to automate React Native app performance regression testing on CI or a local machine. In the same way, you write your integration and unit tests that automatically verify that your app is still working correctly, you can write performance tests that verify that your app is still working performantly.
You can think about it as a React performance testing library. In fact, Reassure is designed to reuse as much of your React Native Testing Library tests and setup as possible.
Reassure works by measuring render characteristics – duration and count – of the testing scenario you provide and comparing that to the stable version. It repeats the scenario multiple times to reduce the impact of random variations in render times caused by the runtime environment. Then, it applies statistical analysis to determine whether the code changes are statistically significant. As a result, it generates a human-readable report summarizing the results and displays it on the CI or as a comment to your pull request.
In addition to measuring component render times it can also measure execution of regular JavaScript functions.
Installation and setup
To install Reassure, run the following command in your app folder:
Using yarn
Using npm
npm install --save-dev reassure
You will also need a working Jest setup as well as one of either React Native Testing Library or React Testing Library.
See Installation guide.
You can check our example projects:
Reassure will try to detect which Testing Library you have installed. If both React Native Testing Library and React Testing Library are present, it will warn you about that and give precedence to React Native Testing Library. You can explicitly specify Testing Library to be used by using configure option:
configure({ testingLibrary: 'react-native' });
// or
configure({ testingLibrary: 'react' });
You should set it in your Jest setup file, and you can override it in particular test files if needed.
Writing your first test
Now that the library is installed, you can write your first test scenario in a file with .perf-test.js/.perf-test.tsx extension:
// ComponentUnderTest.perf-test.tsx
import { measureRenders } from 'reassure';
import { ComponentUnderTest } from './ComponentUnderTest';
test('Simple test', async () => {
await measureRenders(<ComponentUnderTest />);
});
This test will measure render times of ComponentUnderTest during mounting and resulting sync effects.
Note: Reassure will automatically match test filenames using Jest's --testMatch option with value "**/__perf__/**/*.[jt]s?(x)", "**/*.(perf|perf-test).[jt]s?(x)". However, if you want to pass a custom --testMatch or --testRegex option, you may add it to the reassure measure script to pass your own glob. More about --testMatch and --testRegex in Jest docs
Writing async tests
If your component contains any async logic or you want to test some interaction, you should pass the scenario option:
import { measureRenders } from 'reassure';
import { screen, fireEvent } from '@testing-library/react-native';
import { ComponentUnderTest } from './ComponentUnderTest';
test('Test with scenario', async () => {
const scenario = async () => {
fireEvent.press(screen.getByText('Go'));
await screen.findByText('Done');
};
await measureRenders(<ComponentUnderTest />, { scenario });
});
The body of the scenario function is using familiar React Native Testing Library methods.
In case of using a version of React Native Testing Library lower than v10.1.0, where screen helper is not available, the scenario function provides it as its first argument:
import { measureRenders } from 'reassure';
import { fireEvent } from '@testing-library/react-native';
test('Test with scenario', async () => {
const scenario = async (screen) => {
fireEvent.press(screen.getByText('Go'));
await screen.findByText('Done');
};
await measureRenders(<ComponentUnderTest />, { scenario });
});
If your test contains any async changes, you will need to make sure that the scenario waits for these changes to settle, e.g. using findBy queries, waitFor or waitForElementToBeRemoved functions from RNTL.
Measuring test performance
To measure your first test performance, you need to run the following command in the terminal:
This command will run your tests multiple times using Jest, gathering performance statistics and will write them to .reassure/current.perf file. To check your setup, check if the output file exists after running the command for the first time.
Note: You can add .reassure/ folder to your .gitignore file to avoid accidentally committing your results.
Reassure CLI will automatically try to detect your source code branch name and commit hash when you are using Git. You can override these options, e.g. if you are using a different version control system:
yarn reassure --branch [branch name] --commit-hash [commit hash]
Write performance testing script
To detect performance changes, you must measure the performance of two versions of your code current (your modified code) and baseline (your reference point, e.g. main branch). To measure performance on two branches, you must switch branches in Git or clone two copies of your repository.
We want to automate this task to run on the CI. To do that, you will need to create a performance-testing script. You should save it in your repository, e.g. as reassure-tests.sh.
A simple version of such script, using a branch-changing approach, is as follows:
#!/usr/bin/env bash
set -e
BASELINE_BRANCH=${GITHUB_BASE_REF:="main"}
# Required for `git switch` on CI
git fetch origin
# Gather baseline perf measurements
git switch "$BASELINE_BRANCH"
yarn install
yarn reassure --baseline
# Gather current perf measurements & compare results
git switch --detach -
yarn install
yarn reassure
CI setup
To make setting up the CI integration and all prerequisites more convenient, we have prepared a CLI command to generate all necessary templates for you to start with.
Simply run:
This will generate the following file structure
├── <ROOT>
│ ├── reassure-tests.sh
│ ├── dangerfile.ts/js (or dangerfile.reassure.ts/js if dangerfile.ts/js already present)
│ └── .gitignore
Scaffolding