Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to setup vitest integration #103

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/alfa-jest/docs/dependency-graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/alfa-vitest/config/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"extends": "../../../config/api-extractor.json",
"mainEntryPointFilePath": "<projectFolder>/dist/index.d.ts"
}
52 changes: 52 additions & 0 deletions packages/alfa-vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "http://json.schemastore.org/package",
"name": "@siteimprove/alfa-vitest",
"homepage": "https://alfa.siteimprove.com",
"version": "0.74.2",
"license": "MIT",
"description": "Assertion integrations for the Vitest testing framework",
"repository": {
"type": "git",
"url": "github:Siteimprove/alfa-integrations",
"directory": "packages/alfa-vitest"
},
"bugs": "https://github.com/siteimprove/alfa/issues",
"engines": {
"node": ">=20.0.0"
},
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
],
"scripts": {
"test": "vitest"
},
"dependencies": {
"@siteimprove/alfa-act": "^0.93.8",
"@siteimprove/alfa-assert": "workspace:^",
"@siteimprove/alfa-future": "^0.93.8",
"@siteimprove/alfa-hash": "^0.93.8",
"@siteimprove/alfa-mapper": "^0.93.8",
"vitest": "^2.1.4"
},
"peerDependencies": {
"@siteimprove/alfa-act": "^0.93.8",
"@siteimprove/alfa-future": "^0.93.8",
"@siteimprove/alfa-hash": "^0.93.8",
"@siteimprove/alfa-mapper": "^0.93.8"
},
"publishConfig": {
"access": "public",
"registry": "https://npm.pkg.github.com/"
},
"devDependencies": {
"@siteimprove/alfa-device": "^0.93.8",
"@siteimprove/alfa-dom": "^0.93.8",
"@siteimprove/alfa-http": "^0.93.8",
"@siteimprove/alfa-rules": "^0.93.8",
"@siteimprove/alfa-web": "^0.93.8"
}
}
1 change: 1 addition & 0 deletions packages/alfa-vitest/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./vitest.js";
7 changes: 7 additions & 0 deletions packages/alfa-vitest/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../tsconfig.json",
"compilerOptions": { "outDir": "../dist" },
"files": ["./index.ts", "./vitest.ts"],
"references": [{ "path": "../../alfa-assert" }]
}
57 changes: 57 additions & 0 deletions packages/alfa-vitest/src/vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Question, Rule } from "@siteimprove/alfa-act";
import type { Handler } from "@siteimprove/alfa-assert";
import { Asserter } from "@siteimprove/alfa-assert";
import type { Future } from "@siteimprove/alfa-future";
import type { Hashable } from "@siteimprove/alfa-hash";
import type { Mapper } from "@siteimprove/alfa-mapper";

import { expect } from "vitest";

interface CustomMatchers<R = unknown> {
toBeAccessible: () => R;
}

declare module "vitest" {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}

/**
* @public
*/
export namespace Vitest {
export function createPlugin<
I,
J,
T extends Hashable,
Q extends Question.Metadata = {},
S = T
>(
transform: Mapper<I, Future.Maybe<J>>,
rules: Iterable<Rule<J, T, Q, S>>,
handlers: Iterable<Handler<J, T, Q, S>> = [],
options: Asserter.Options<J, T, Q, S> = {}
): void {
const asserter = Asserter.of(rules, handlers, options);

expect.extend({
async toBeAccessible(value: I) {
const input = await transform(value);

const result = await asserter.expect(input).to.be.accessible();

const message = result.isOk() ? result.get() : result.getErrUnsafe();

return {
pass: result.isOk(),
message: () =>
this.utils.matcherHint("toBeAccessible", "received", "", {
isNot: this.isNot,
}) +
" but " +
message,
};
},
});
}
}
12 changes: 12 additions & 0 deletions packages/alfa-vitest/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": ".",
"jsx": "react-jsx",
"jsxImportSource": "@siteimprove/alfa-dom",
"types": ["vitest"]
},
"files": ["./vitest.spec.tsx"],
"references": [{ "path": "../src" }]
}
37 changes: 37 additions & 0 deletions packages/alfa-vitest/test/vitest.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Device } from "@siteimprove/alfa-device";
import { h } from "@siteimprove/alfa-dom";
import { Request, Response } from "@siteimprove/alfa-http";
import { Rules } from "@siteimprove/alfa-rules";
import { Page } from "@siteimprove/alfa-web";

import { expect } from "vitest";

import { Vitest } from "../dist/vitest.js";

Vitest.createPlugin((page: Page) => page, [Rules.get("R12").getUnsafe()]);

describe(".createPlugin adds a .toBeAccessible method", () => {
const page = Page.of(
Request.empty(),
Response.empty(),
h.document([<button>Hello World</button>]),
Device.standard()
);

it("should have a .toBeAccessible method", async () => {
await expect(page).toBeAccessible();
});
});

describe(".createPlugin adds a .not.toBeAccessible method", () => {
const page = Page.of(
Request.empty(),
Response.empty(),
h.document([<button></button>]),
Device.standard()
);

it("should have a .not.toBeAccessible method", async () => {
await expect(page).not.toBeAccessible();
});
});
5 changes: 5 additions & 0 deletions packages/alfa-vitest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "../tsconfig.json",
"references": [{ "path": "./src" }, { "path": "./test" }]
}
1 change: 1 addition & 0 deletions packages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{ "path": "alfa-selenium" },
{ "path": "alfa-test-utils" },
{ "path": "alfa-unexpected" },
{ "path": "alfa-vitest" },
{ "path": "alfa-vue" },
{ "path": "alfa-webdriver" }
]
Expand Down
Loading
Loading