Skip to content

Commit

Permalink
improvement(jest): add config to automate setup of a11y matchers
Browse files Browse the repository at this point in the history
Add integration test package to test setup of a11y matcher
  • Loading branch information
Mohan Raj Rajamanickam committed May 4, 2020
1 parent c77f356 commit 2b18c4b
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 9 deletions.
8 changes: 1 addition & 7 deletions cSpell.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"ignorePaths": ["node_modules/**", "package.json"],
"ignoreWords": [
"assertAccessible",
"doctoc",
"CNCF",
"SPDX", // license header
"tsdoc"
],
"ignoreWords": ["assertAccessible", "doctoc", "CNCF", "integ", "SPDX", "tsdoc"],
"ignoreRegExpList": ["ruleset"]
}
31 changes: 30 additions & 1 deletion packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,42 @@ Accessibility matcher for [Jest](https://jestjs.io)


- [Setup](#setup)
- [Automatic](#automatic)
- [Manual](#manual)
- [Usage](#usage)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Setup

- TODO: Based on https://github.com/jest-community/jest-extended#setup
The accessibility matcher helper APIs need to be registered with Jest before they can be used in tests.

### Automatic

Modify Jest config to add the required setup for accessibility matchers.

In the `jest.config.js` at the root of your project, add

```javascript
const { jestConfig } = require('@sa11y/jest');

module.exports = {
...jestConfig,
// Your config ..
};
```

### Manual

Invoke `registerA11yMatchers` before using the accessibility matchers in the tests.

```typescript
import { registerA11yMatchers } from '@sa11y/jest';

beforeAll(() => {
registerA11yMatchers();
});
```

## Usage

Expand Down
4 changes: 4 additions & 0 deletions packages/jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@

export { toBeAccessible } from './matcher';
export { adaptA11yConfig, registerA11yMatchers } from './setup';

export const jestConfig = {
setupFilesAfterEnv: [require.resolve('./setup')],
};
3 changes: 3 additions & 0 deletions packages/jest/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ export function adaptA11yConfig(config: A11yConfig): A11yConfig {
},
};
}

// When this file is used as part of Jest setup with setupFilesAfterEnv
registerA11yMatchers();
11 changes: 11 additions & 0 deletions packages/test-integ/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [`test-integ`](#test-integ)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# `test-integ`

Private package for integration testing @sa11y packages
13 changes: 13 additions & 0 deletions packages/test-integ/__tests__/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2020, 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
*/

describe('integration test @sa11y/jest', () => {
it.skip('should have a11y matchers working with setup in jest.config.js', () => {
// TODO(Fix) : Fails with TypeError: expect(...).toBeAccessible is not a function
expect(document).toBeAccessible();
});
});
13 changes: 13 additions & 0 deletions packages/test-integ/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2020, 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
*/

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { jestConfig } = require('@sa11y/jest');

module.exports = {
...jestConfig,
};
16 changes: 16 additions & 0 deletions packages/test-integ/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@sa11y/test-integ",
"version": "0.1.0",
"private": true,
"description": "Private package for integration testing @sa11y packages",
"license": "BSD-3-Clause",
"homepage": "https://github.com/salesforce/sa11y/tree/master/packages/test-integ#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/salesforce/sa11y.git",
"directory": "packages/test-integ"
},
"devDependencies": {
"@sa11y/jest": "0.1.0"
}
}
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"compilerOptions": { "strict": true },
"include": ["./packages/**/*.ts", "*.js"]
"include": ["*.js", "./packages/**/*.ts", "./packages/**/*.js"]
}

0 comments on commit 2b18c4b

Please sign in to comment.