Skip to content

Commit a41ef90

Browse files
committed
Wrap calls to Axe in act() to prevent warnings
Axe seems to trigger React state updates, which must be wrapped in act() to prevent warnings.
1 parent da26b74 commit a41ef90

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

  • packages/bonito-ui/src/test-util

packages/bonito-ui/src/test-util/a11y.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import type { AxeResults, RunOptions } from "axe-core";
66
import type { JestAxe, JestAxeConfigureOptions } from "jest-axe";
77
import { MockBrowserEnvironment } from "..";
88
import { getMockBrowserEnvironment } from "../environment";
9+
import { act } from "react-dom/test-utils";
910

1011
// Globally configured axe instance
1112
let _axe: JestAxe | null = null;
1213

1314
/**
1415
* A wrapper around react-testing-library's render() function which performs
1516
* accessibility tests after the container is rendered. Note that this is
16-
* an async function unlike render() because running aXe is async.
17+
* an async function unlike render() because running Axe is async.
1718
*
1819
* @param ui The element to render
1920
* @param options Render options
@@ -66,7 +67,7 @@ export async function runAxe(
6667
// Give a slightly more useful exception if the current environment
6768
// isn't a mock browser env
6869
if (e instanceof Error) {
69-
throw new Error("Unable to run aXe: " + e.message);
70+
throw new Error("Unable to run Axe: " + e.message);
7071
}
7172
throw e;
7273
}
@@ -82,11 +83,19 @@ export async function runAxe(
8283
} as unknown as AxeResults;
8384
}
8485

85-
if (!_axe) {
86-
throw new Error("aXe was not initialized properly");
86+
let results: AxeResults | null = null;
87+
await act(async () => {
88+
if (!_axe) {
89+
throw new Error("Axe was not initialized properly");
90+
}
91+
results = await _axe(html, options);
92+
});
93+
94+
if (!results) {
95+
throw new Error("Axe did not return results");
8796
}
8897

89-
return _axe(html, options);
98+
return results;
9099
}
91100

92101
/**

0 commit comments

Comments
 (0)