React Browser Tests
  • Home
  • Components
  • Helper Functions
  • Run in a Terminal
  • Tests

Helper Functions

React Browser Tests exports multiple functions and hooks that can be used in tests. The main ones are:

waitFor

Waits for a condition to be true. Receives a function that returns a boolean. Returns a promise that resolves when the function returns true, or rejects after a timeout.

getTestArrayFromContainerState

Receives a containerState object and returns the array of tests from it.

groupTests

Receives an array of tests and a group name. Returns an array with only the tests with the passed in group name.

assert, expect, should

These are re-exported from Chai.

window.reactBrowserTests

The global window object has multiple functions available. They can be accessed through the window.reactBrowserTests object. The type of this object is:

type ReactBrowserTestsWindowObject = {
  testContainers: TestContainerState[];
  getContainerState: (containerId?: string, windowRef?: Window) => TestContainerState | undefined;
  getTestRecord: (containerId?: string, windowRef?: Window) => Record<string, TestType>;
  getTestArray: (containerId?: string, windowRef?: Window) => TestType[];
  checkIfContainerTestsComplete: (containerId?: string, windowRef?: Window) => boolean;
  checkIfTestsFromAllContainersComplete: (windowRef?: Window) => boolean;
  sumTotalNumberOfTests: (windowRef?: Window) => number | null;
  checkIfAllTestsRegistered: (windowRef?: Window) => boolean;
}

Here's an example of how to fetch an array with all the tests:

const testArray = window.reactBrowserTests.getTestArray();

Others

There are other functions available. Their source code is available on:

  • testHelpers.ts
  • window.ts
  • hooks.ts
  • iframeHelpers.ts