Performance testing companion for React and React Native
Performance testing companion for React and React Native
Performance testing companion for React and React Native.
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.
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.
To install Reassure, run the following command in your app folder:
Using yarn
yarn add --dev reassureUsing npm
npm install --save-dev reassureYou 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.
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
--testMatchoption with value"**/__perf__/**/*.[jt]s?(x)", "**/*.(perf|perf-test).[jt]s?(x)". However, if you want to pass a custom--testMatchor--testRegexoption, you may add it to thereassure measurescript to pass your own glob. More about--testMatchand--testRegexin Jest docs
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.
To measure your first test performance, you need to run the following command in the terminal:
yarn reassureThis 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.gitignorefile 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]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 reassureTo 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:
yarn reassure initThis will generate the following file structure
├── <ROOT>
│ ├── reassure-tests.sh
│ ├── dangerfile.ts/js (or dangerfile.reassure.ts/js if dangerfile.ts/js already present)
│ └── .gitignorereassure-tests.sh)Basic script allowing you to run Reassure on CI. More on the importance and structure of this file in the following section.
If your project already contains a dangerfile.ts/js, the CLI will not override it in any way. Instead, it will generate a dangerfile.reassure.ts/js file, allowing you to compare and update your own at your convenience.
.gitignoreIf the .gitignore file is present and no mentions of reassure appear, the script will append the .reassure/ directory to its end.
reassure-tests.sh)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 reassureAs a final setup step, you must configure your CI to run the performance testing script and output the result. For presenting output at the moment, we integrate with Danger JS, which supports all major CI tools.
You will need a working Danger JS setup.
Then add Reass
No open issues yet, or sync has not completed.