diff --git a/cSpell.json b/cSpell.json index e4eecb67..e82b502e 100644 --- a/cSpell.json +++ b/cSpell.json @@ -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"] } diff --git a/packages/jest/README.md b/packages/jest/README.md index b0f1b332..a1f6887b 100644 --- a/packages/jest/README.md +++ b/packages/jest/README.md @@ -7,13 +7,42 @@ Accessibility matcher for [Jest](https://jestjs.io) - [Setup](#setup) + - [Automatic](#automatic) + - [Manual](#manual) - [Usage](#usage) ## 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 diff --git a/packages/jest/src/index.ts b/packages/jest/src/index.ts index e1dc1e92..445aac3a 100644 --- a/packages/jest/src/index.ts +++ b/packages/jest/src/index.ts @@ -7,3 +7,7 @@ export { toBeAccessible } from './matcher'; export { adaptA11yConfig, registerA11yMatchers } from './setup'; + +export const jestConfig = { + setupFilesAfterEnv: [require.resolve('./setup')], +}; diff --git a/packages/jest/src/setup.ts b/packages/jest/src/setup.ts index ba99440d..7bc3a913 100644 --- a/packages/jest/src/setup.ts +++ b/packages/jest/src/setup.ts @@ -45,3 +45,6 @@ export function adaptA11yConfig(config: A11yConfig): A11yConfig { }, }; } + +// When this file is used as part of Jest setup with setupFilesAfterEnv +registerA11yMatchers(); diff --git a/packages/test-integ/README.md b/packages/test-integ/README.md new file mode 100644 index 00000000..5306b9bd --- /dev/null +++ b/packages/test-integ/README.md @@ -0,0 +1,11 @@ + + + + +- [`test-integ`](#test-integ) + + + +# `test-integ` + +Private package for integration testing @sa11y packages diff --git a/packages/test-integ/__tests__/integration.test.js b/packages/test-integ/__tests__/integration.test.js new file mode 100644 index 00000000..661b8079 --- /dev/null +++ b/packages/test-integ/__tests__/integration.test.js @@ -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(); + }); +}); diff --git a/packages/test-integ/jest.config.js b/packages/test-integ/jest.config.js new file mode 100644 index 00000000..8bce042c --- /dev/null +++ b/packages/test-integ/jest.config.js @@ -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, +}; diff --git a/packages/test-integ/package.json b/packages/test-integ/package.json new file mode 100644 index 00000000..35950f66 --- /dev/null +++ b/packages/test-integ/package.json @@ -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" + } +} diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index 28e6af2d..86a2f3cb 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -1,4 +1,4 @@ { "compilerOptions": { "strict": true }, - "include": ["./packages/**/*.ts", "*.js"] + "include": ["*.js", "./packages/**/*.ts", "./packages/**/*.js"] }