@sa11y/jest
Version:
Accessibility testing matcher for Jest
41 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBeAccessible = exports.isTestUsingFakeTimer = void 0;
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const jest_matcher_utils_1 = require("jest-matcher-utils");
const preset_rules_1 = require("@sa11y/preset-rules");
const matcher_1 = require("@sa11y/matcher");
/**
* Detect if fake timer is being used in a jest test.
* Fake timers result in axe timeout https://github.com/dequelabs/axe-core/issues/3055
* Workaround until underlying issue can be fixed in axe.
* Ref: https://github.com/facebook/jest/issues/10555
*/
function isTestUsingFakeTimer() {
return (typeof jest !== 'undefined' &&
typeof setTimeout !== 'undefined' &&
// eslint-disable-next-line no-prototype-builtins
(setTimeout.hasOwnProperty('_isMockFunction') || setTimeout.hasOwnProperty('clock')));
}
exports.isTestUsingFakeTimer = isTestUsingFakeTimer;
async function toBeAccessible(received = document, config = preset_rules_1.defaultRuleset) {
if (isTestUsingFakeTimer())
throw new Error(matcher_1.fakeTimerErrMsg);
const { isAccessible, a11yError, receivedMsg } = await (0, matcher_1.runA11yCheck)(received, config);
return {
pass: isAccessible,
message: () => (0, jest_matcher_utils_1.matcherHint)(`toBeAccessible`) +
`\n\nExpected: no accessibility violations\nReceived: ${receivedMsg}\n\n${a11yError.format({
...matcher_1.formatOptions,
highlighter: jest_matcher_utils_1.printReceived,
})}`,
a11yError,
};
}
exports.toBeAccessible = toBeAccessible;
//# sourceMappingURL=matcher.js.map