@out-of-order/vitest 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +33 -0
- package/package.json +57 -0
- package/src/index.ts +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bart Spaans
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @out-of-order/vitest
|
|
2
|
+
|
|
3
|
+
> ⚠️ **Under heavy development.** Released, but the API is still changing and may break between versions.
|
|
4
|
+
|
|
5
|
+
`toHaveValidTabOrder()` matcher for [Vitest Browser Mode](https://vitest.dev/guide/browser/). Runs the [`@out-of-order/core`](../core) analyzer against a real Chromium DOM, so a passing assertion reflects what users actually experience.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add -D @out-of-order/vitest vitest @vitest/browser playwright
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Configure
|
|
14
|
+
|
|
15
|
+
`vitest.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { defineConfig } from "vitest/config";
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
test: {
|
|
22
|
+
setupFiles: ["./test/setup.ts"],
|
|
23
|
+
browser: {
|
|
24
|
+
enabled: true,
|
|
25
|
+
provider: "playwright",
|
|
26
|
+
headless: true,
|
|
27
|
+
name: "chromium",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`test/setup.ts`:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import "@out-of-order/vitest"; // registers the matcher + types
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Use
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { expect, test } from "vitest";
|
|
43
|
+
|
|
44
|
+
test("dialog tab order is valid", () => {
|
|
45
|
+
document.body.innerHTML = `
|
|
46
|
+
<button>Cancel</button>
|
|
47
|
+
<button>Confirm</button>
|
|
48
|
+
`;
|
|
49
|
+
expect(document.body).toHaveValidTabOrder();
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Pass options to scope the rules:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
expect(form).toHaveValidTabOrder({
|
|
57
|
+
rules: { "missing-accessible-name": false },
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The target can be an `Element`, `Document`, or `DocumentFragment`. On failure the message lists every violation with its rule id and tab position.
|
|
62
|
+
|
|
63
|
+
> Why Browser Mode and not jsdom? The checks need CSS layout (visibility and visual position). jsdom has no layout engine, so it can't tell you whether the order is correct in a real browser. This matcher only runs where layout is real.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AuditOptions } from '@out-of-order/core';
|
|
2
|
+
export { AuditOptions, AuditResult, audit } from '@out-of-order/core';
|
|
3
|
+
|
|
4
|
+
interface TabOrderMatchers<Ret = unknown> {
|
|
5
|
+
/**
|
|
6
|
+
* Assert the focusable elements within the target form a valid tab order. Must
|
|
7
|
+
* run in a real browser (Vitest Browser Mode); the checks rely on CSS layout.
|
|
8
|
+
*/
|
|
9
|
+
toHaveValidTabOrder(options?: AuditOptions): Ret;
|
|
10
|
+
}
|
|
11
|
+
declare module "vitest" {
|
|
12
|
+
interface Assertion<T = any> extends TabOrderMatchers<T> {
|
|
13
|
+
}
|
|
14
|
+
interface AsymmetricMatchersContaining extends TabOrderMatchers {
|
|
15
|
+
}
|
|
16
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { expect } from "vitest";
|
|
3
|
+
import { audit } from "@out-of-order/core";
|
|
4
|
+
function resolveRoot(received) {
|
|
5
|
+
if (received instanceof Element || received instanceof Document || received instanceof DocumentFragment) {
|
|
6
|
+
return received;
|
|
7
|
+
}
|
|
8
|
+
throw new Error(
|
|
9
|
+
`toHaveValidTabOrder() expects an Element, Document, or DocumentFragment, received: ${String(
|
|
10
|
+
received
|
|
11
|
+
)}`
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
expect.extend({
|
|
15
|
+
toHaveValidTabOrder(received, options) {
|
|
16
|
+
const root = resolveRoot(received);
|
|
17
|
+
const result = audit(root, options);
|
|
18
|
+
const { isNot } = this;
|
|
19
|
+
const issueCount = result.violations.reduce(
|
|
20
|
+
(total, violation) => total + violation.issues.length,
|
|
21
|
+
0
|
|
22
|
+
);
|
|
23
|
+
return {
|
|
24
|
+
pass: result.valid,
|
|
25
|
+
actual: result.violations,
|
|
26
|
+
message: () => isNot ? `Expected tab order to be invalid, but no violations were found (${result.sequence.length} focusable elements checked).` : `Expected a valid tab order, found ${issueCount} issue(s):
|
|
27
|
+
` + audit(root, { ...options, format: "text" }).violations
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
export {
|
|
32
|
+
audit
|
|
33
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@out-of-order/vitest",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "toHaveValidTabOrder() matcher for Vitest Browser Mode.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"a11y",
|
|
7
|
+
"accessibility",
|
|
8
|
+
"browser",
|
|
9
|
+
"matcher",
|
|
10
|
+
"tab-order",
|
|
11
|
+
"vitest"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Bart Spaans",
|
|
15
|
+
"homepage": "https://github.com/spaansba/out-of-order#readme",
|
|
16
|
+
"bugs": "https://github.com/spaansba/out-of-order/issues",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/spaansba/out-of-order.git",
|
|
20
|
+
"directory": "packages/vitest"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@out-of-order/core": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@vitest/browser": "^2.1.0",
|
|
44
|
+
"playwright": "^1.47.0",
|
|
45
|
+
"tsup": "^8.2.0",
|
|
46
|
+
"typescript": "^5.5.0",
|
|
47
|
+
"vitest": "^2.1.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"vitest": ">=2.0.0"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"test": "vitest run"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { expect } from "vitest";
|
|
2
|
+
import { audit, type AuditOptions, type AuditResult } from "@out-of-order/core";
|
|
3
|
+
|
|
4
|
+
/** Accept an Element or a Document/DocumentFragment as the assertion target. */
|
|
5
|
+
function resolveRoot(received: unknown): ParentNode {
|
|
6
|
+
if (
|
|
7
|
+
received instanceof Element ||
|
|
8
|
+
received instanceof Document ||
|
|
9
|
+
received instanceof DocumentFragment
|
|
10
|
+
) {
|
|
11
|
+
return received;
|
|
12
|
+
}
|
|
13
|
+
throw new Error(
|
|
14
|
+
`toHaveValidTabOrder() expects an Element, Document, or DocumentFragment, received: ${String(
|
|
15
|
+
received,
|
|
16
|
+
)}`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
expect.extend({
|
|
21
|
+
toHaveValidTabOrder(received: unknown, options?: AuditOptions) {
|
|
22
|
+
const root = resolveRoot(received);
|
|
23
|
+
const result: AuditResult = audit(root, options);
|
|
24
|
+
const { isNot } = this;
|
|
25
|
+
|
|
26
|
+
const issueCount = result.violations.reduce(
|
|
27
|
+
(total, violation) => total + violation.issues.length,
|
|
28
|
+
0,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
pass: result.valid,
|
|
33
|
+
actual: result.violations,
|
|
34
|
+
message: () =>
|
|
35
|
+
isNot
|
|
36
|
+
? `Expected tab order to be invalid, but no violations were found ` +
|
|
37
|
+
`(${result.sequence.length} focusable elements checked).`
|
|
38
|
+
: `Expected a valid tab order, found ${issueCount} issue(s):\n` +
|
|
39
|
+
audit(root, { ...options, format: "text" }).violations,
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
interface TabOrderMatchers<Ret = unknown> {
|
|
45
|
+
/**
|
|
46
|
+
* Assert the focusable elements within the target form a valid tab order. Must
|
|
47
|
+
* run in a real browser (Vitest Browser Mode); the checks rely on CSS layout.
|
|
48
|
+
*/
|
|
49
|
+
toHaveValidTabOrder(options?: AuditOptions): Ret;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare module "vitest" {
|
|
53
|
+
// eslint-disable-next-line id-length -- type param must match vitest's `Assertion<T>` for declaration merging
|
|
54
|
+
interface Assertion<T = any> extends TabOrderMatchers<T> {}
|
|
55
|
+
interface AsymmetricMatchersContaining extends TabOrderMatchers {}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { audit };
|
|
59
|
+
export type { AuditOptions, AuditResult } from "@out-of-order/core";
|