
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@sa11y/jest
Advanced tools
@sa11y/jest
Accessibility matcher for Jest
The toBeAccessible()
API from this library can be used in Jest unit tests to test HTML elements or DOM for accessibility.
yarn add -D @sa11y/jest
npm install -D @sa11y/jest
The accessibility APIs need to be registered with Jest before they can be used in tests.
You can set up the sa11y API once at the project level to make it available to all the Jest tests in the project. For an example look at the Integration test setup in @sa11y.
jest-setup.js
) and add the following code that registers the sa11y API// Import using either CommonJS `require` or ES6 `import`
const { setup } = require('@sa11y/jest'); // CommonJS
import { setup } from '@sa11y/jest'; // ES6
// Register the sa11y matcher
setup();
jest.config.js
at the root of your project, add:module.exports = {
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
};
toBeAccessible
API available for any test in the project.Invoke setup
before using the toBeAccessible
API in the tests
import { setup } from '@sa11y/jest';
beforeAll(() => {
setup();
});
toBeAccessible
API available for the tests only in that specific test module where setup()
is invoked.The sa11y API can be setup to be automatically invoked at the end of each test
setup({ autoCheckOpts: { runAfterEach: true } });
// To optionally cleanup the body after running a11y checks
setup({ autoCheckOpts: { runAfterEach: true, cleanupAfterEach: true } });
// Options can also be passed to setup() using environment variables
// E.g. Invoking jest test runner in command line: "SA11Y_AUTO=1 SA11Y_CLEANUP=1 jest ..."
setup(); // Automatic checks will be enabled due to the environment variables
toBeAccessible
API at the end of each testsetup()
to opt-in to automatic checkstoBeAccessible
must be invoked with async/wait
or Promise
or the equivalent supported asynchronous method in your environment
audio
, video
elements cannot be checked with Jest as they are stubbed out in JSDOM@sa11y/wdio
toBeAccessible
can either be invoked on the entire document
(JSDOM) or on a specific HTML element to check for accessibilityimport { base, full } from '@sa11y/preset-rules';
import { setup } from '@sa11y/jest';
beforeAll(() => {
setup();
});
it('should be accessible', async () => {
// Setup DOM to be tested for accessibility
//...
// assert that DOM is accessible (using recommended preset-rule)
await expect(document).toBeAccessible();
// Can be used to test accessibility of a specific HTML element
const elem = document.getElementById('foo');
await expect(elem).toBeAccessible();
// If you want to test against all rules provided by axe
await expect(document).toBeAccessible(full);
// If you have any a11y issues from the default recommended preset-rule
// that you can't fix for now, you can use the base preset-rule
await expect(document).toBeAccessible(base);
});
0.3.0-beta (2020-08-19)
FAQs
Accessibility testing matcher for Jest
The npm package @sa11y/jest receives a total of 9,992 weekly downloads. As such, @sa11y/jest popularity was classified as popular.
We found that @sa11y/jest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.