2.2.7 • Published 8 months ago

@powala/react-native-visual-regression v2.2.7

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

react-native-visual-regression

Demo

This package orchestrates several tools to provide a flexible visual regression solution for React Native (RN).

If you are using Storybook in your project, you can perform visual regression testing against existing stories.

Storybook

You need to have an app build that can launch in Storybook mode. See the example directory for reference.

Storybook allows you to render any screen or component in your application. By capturing screenshots of these screens or components in a controlled environment, you can ensure consistent results.

Rendering only the screens or components that you are interested in regression testing will be more efficient, as you won't need to navigate through multiple screens to reach a particular screen.

Installation

npm install --save-dev @powala/react-native-visual-regression

Add to your .gitignore

visual-regression/current
visual-regression/diff
VisualRegressionTestReport.*

Usage

Add the configuration file rn-vr.config.js to the root of your project:

module.exports = {
  devices: [ // You can run visual regression for multiple devices
    {
      platform: 'android',
      name: 'Pixel_8_API_34',
    },
  ],
  appId: 'com.anonymous.VisualRegression', // appId of the app
  android: {
    activity: ".MainActivity",
  },
  storiesDirectories: [ // Directories where the stories are located
    './example/.storybook'
  ]
};

Mark which story should have visual regression coverage:

export const AnotherExample: StoryObj<typeof MyButton> = {
  args: {
    text: 'Another example',
  },
  parameters: {
    visualRegression: true,
    visualRegressionAwaitElement: "someTestID", // await initial element (optional)
  }
};
Run Visual Regression:
npx rn-vr

A visual regression run will generate a set of assets in the visual-regression directory at the root of the project.

  • baseline: The current source of truth for visual regression. If a given story does not have a baseline set, the initial run will create the baseline screenshot. This should be committed to version control as a reference.

  • current: Screenshots produced by the latest visual regression run. If approved, these will become the baseline and the new source of truth. This should be omitted from version history.

  • diff: A comparison between the current and baseline screenshots. This should be omitted from version history.

Additionally, the run will produce:

  • VisualRegressionTestReport.md: A report summarizing the run.
Approve changes
npx rn-vr -a

Command Arguments

ArgumentDefaultExampleNotes
--approve | -aundefined-aApprove base images with the current version
--file | -fundefined-f .storybook/stories/Button/Button.stories.tsxFilename to run visual regression on
--story | -sundefined-s MyButton-AnotherExampleTarget a particular Story kind-name
--device | -dundefined-d "Pixel 8" -d "iPhone 15"Run only on specified devices
--reportFormat | -rfmd--reportFormat "html"Format of the generated report
--appPathundefined--appPath="./DerivedData/Build/Products/Dev-iphonesimulator/VisualRegression.app"Path to *.app which can be installed on simulator
--apkPathundefined--apkPath="./android/app/build/outputs/apk/dev/release/app-dev-release.apk"Path to *.apk which can be installed on emulator
--maskHomeBarundefined--apkPath=trueMasks Home bar on iOS
--reinstallApp | -rfalse--reinstallApp=trueWill reinstall the app as long app paths are provided
--driverLogLevelsilent--driverLogLevel="debug"verbosity of driver
--migrateToV2undefined--migrateToV2Migrate baseline directory to V2 structure

Prerequisites

  • Ensure the app you want to run against is installed on the simulator/emulator you want to use.
  • For best results run the app in Release mode, otherwsie bundler will slow down the run and might produce inconsistencies.
  • The app can launch in Storybook mode, preferably without a UI, so you can capture only the screen. See the example directory for more details.
  • Story files should end with *.stories.tsx.

I want tests to run faster

Over time, test runs may slow down due to an increasing number of assertions. To address this, you can spin up additional devices to distribute the assertions across multiple devices of the same type.

Update rn-vr.config.js with number of addtional devices.

Modify the configuration to specify the number of additional devices:

module.exports = {
  devices: [
    {
      platform: 'android',
      name: 'Pixel_8_API_34',
      devices: 3, // <-- Number of devices you want to use
    },
     {
      platform: 'ios',
      name: 'iPhone 15',
      devices: 3, // <-- Number of devices you want to use
    },
  ],
  // Other configurations...
};

When the devices option is specified, additional devices will be created if they don't already exist.

These additional devices will be clones of the base device, with names like:

Pixel_8_API_34_2 Pixel_8_API_34_3

iPhone 15_2 iPhone 15_3

Preinstalling the App

If you provide the --apk and/or --app arguments, the app will be preinstalled on all devices before the tests are executed. Alternatively, you can manually install the app after the additional devices are created.

Migration from v1 to v2

Run following to migrate baseline directory to new folder structure which is now split by devices.

npx rn-vr --migrateToV2

Add android app activity to to the rn-vr.config.js config

...
android: {
  activity: ".MainActivity",
},
...
2.1.0-rc.2

11 months ago

2.0.0-rc.2

12 months ago

2.0.0-rc.3

12 months ago

2.0.0-rc.4

12 months ago

2.0.0-rc.1

12 months ago

2.1.0-rc

11 months ago

2.2.1

8 months ago

2.0.3

11 months ago

2.2.0

8 months ago

2.0.2

11 months ago

2.2.3

8 months ago

2.2.2

8 months ago

2.2.5

8 months ago

2.2.4

8 months ago

2.2.7

8 months ago

2.2.6

8 months ago

2.0.1

11 months ago

2.0.0

11 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago