@ac-kit/vitest-config 0.1.1 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # @ac-kit/vitest-config
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 34e8cfa: Run a package's `*.browser.test.ts` files in a real browser with
8
+ `defineBrowserConfig` from `@ac-kit/vitest-config/browser`: headless Chromium
9
+ through Playwright by default (its full build, `channel: "chromium"` unless you
10
+ set it), `VITEST_BROWSERS=chromium,firefox,webkit` to choose the engines, and
11
+ your own server-side `commands` for trusted input. It sets no GPU flags:
12
+ Playwright's `launchOptions` (`args` such as GPU flags) are given to every
13
+ engine, and `projects` runs the same tests once per named entry, each with its
14
+ own `provide` and `launchOptions` over the shared ones.
15
+
16
+ `@ac-kit/vitest-config/browser-commands` exports `browserCommands`, the
17
+ server-side commands that give a browser test trusted input: `trustedMouse`
18
+ (move, down, up, wheel), `trustedKey`, and, in Chromium, `dispatchTouch` and
19
+ `synthesizePinch`. Pass it as `commands` to `defineBrowserConfig`; the exported
20
+ `TrustedBrowserCommands` types them for `vitest/browser`'s `commands`.
21
+
3
22
  ## 0.1.1
4
23
 
5
24
  ### Patch Changes
package/README.md CHANGED
@@ -12,3 +12,55 @@ export default defineProject({
12
12
  // project-specific overrides
13
13
  });
14
14
  ```
15
+
16
+ ## Browser tests
17
+
18
+ `@ac-kit/vitest-config/browser` runs `src/**/*.browser.test.ts` in a real
19
+ browser through Playwright, headless. Exclude those files
20
+ from the package's `vitest.config.ts` (`**/*.browser.test.ts`) so `yarn test`
21
+ never runs them, and put the preset in `vitest.browser.config.ts`:
22
+
23
+ ```ts
24
+ import { defineBrowserConfig } from "@ac-kit/vitest-config/browser";
25
+
26
+ export default defineBrowserConfig({
27
+ // include: ["src/**/*.browser.test.ts"],
28
+ // commands: { trustedMouse: async (context, input) => {} },
29
+ });
30
+ ```
31
+
32
+ Chromium alone runs by default; `VITEST_BROWSERS=chromium,firefox,webkit` picks
33
+ other engines (their Playwright builds must be installed). `@vitest/browser-playwright`
34
+ and `playwright` are optional peer dependencies, loaded only by this subpath: a
35
+ package that runs browser tests declares them in its own `devDependencies`.
36
+ `commands` are
37
+ server-side functions the tests call through `vitest/browser`'s `commands`, with
38
+ the Playwright `page` in their context. `@ac-kit/vitest-config/browser-commands`
39
+ exports ready-made ones for trusted input: `trustedMouse` (move, down, up,
40
+ wheel), `trustedKey` and, in Chromium, `dispatchTouch` and `synthesizePinch`;
41
+ `defineBrowserConfig({ commands: browserCommands })`. Type `commands` from
42
+ `vitest/browser` in the test file with `declare module "vitest/browser" {
43
+ interface BrowserCommands extends TrustedBrowserCommands {} }`, importing
44
+ `TrustedBrowserCommands` from the same module as a type (it cannot reach the
45
+ package's own copy of Vitest itself).
46
+
47
+ `launchOptions` is Playwright's `LaunchOptions` for the run, given to every
48
+ engine: `args` carry flags such as GPU or software-rendering switches. Chromium
49
+ runs with `channel: "chromium"` (its full build, not the headless shell) unless
50
+ you set `channel`, whichever other engines run beside it. `projects` runs the same tests
51
+ once per entry, each a named project (`--project <name>`) with its own `provide`
52
+ (read in a test with `inject()`) and `launchOptions` merged over the shared ones:
53
+
54
+ ```ts
55
+ export default defineBrowserConfig({
56
+ launchOptions: { args: ["--use-angle=swiftshader"] },
57
+ projects: [
58
+ { name: "software", provide: { hardware: false } },
59
+ {
60
+ name: "hardware",
61
+ provide: { hardware: true },
62
+ launchOptions: { args: ["--enable-unsafe-webgpu"] },
63
+ },
64
+ ],
65
+ });
66
+ ```
@@ -0,0 +1,56 @@
1
+ export type BrowserPoint = { readonly x: number; readonly y: number };
2
+
3
+ export type TrustedMouseInput = BrowserPoint & {
4
+ readonly action: "move" | "down" | "up" | "wheel";
5
+ readonly deltaX?: number;
6
+ readonly deltaY?: number;
7
+ readonly button?: "left" | "right" | "middle";
8
+ };
9
+
10
+ export type TrustedKeyInput = {
11
+ readonly action: "down" | "up";
12
+ readonly key: string;
13
+ };
14
+
15
+ export type TouchInput = {
16
+ readonly type: "touchStart" | "touchMove" | "touchEnd" | "touchCancel";
17
+ readonly points: readonly (BrowserPoint & { readonly id: number })[];
18
+ };
19
+
20
+ export type PinchInput = BrowserPoint & {
21
+ readonly scaleFactor: number;
22
+ readonly relativeSpeed?: number;
23
+ };
24
+
25
+ /**
26
+ * What a test calls through `vitest/browser`'s `commands`. The package that
27
+ * runs the tests merges it into its own copy of Vitest (this one cannot reach
28
+ * it):
29
+ *
30
+ * ```ts
31
+ * declare module "vitest/browser" {
32
+ * interface BrowserCommands extends TrustedBrowserCommands {}
33
+ * }
34
+ * ```
35
+ */
36
+ export interface TrustedBrowserCommands {
37
+ /**
38
+ * Trusted mouse input through the browser's input pipeline, at client
39
+ * coordinates of the test page.
40
+ */
41
+ trustedMouse: (input: TrustedMouseInput) => Promise<void>;
42
+ trustedKey: (input: TrustedKeyInput) => Promise<void>;
43
+ /** Chromium only: `Input.dispatchTouchEvent` with touch emulation on. */
44
+ dispatchTouch: (input: TouchInput) => Promise<void>;
45
+ /** Chromium only: `Input.synthesizePinchGesture`. */
46
+ synthesizePinch: (input: PinchInput) => Promise<void>;
47
+ }
48
+
49
+ /**
50
+ * The `commands` of `defineBrowserConfig` that give a test trusted input:
51
+ * {@link TrustedBrowserCommands}.
52
+ */
53
+ export declare const browserCommands: Record<
54
+ keyof TrustedBrowserCommands,
55
+ (context: unknown, ...payload: never[]) => Promise<void>
56
+ >;
@@ -0,0 +1,70 @@
1
+ const sessions = new WeakMap();
2
+
3
+ /** Where the test page's origin is in the browser window. */
4
+ async function origin(context) {
5
+ const box = await context.iframe.owner().boundingBox();
6
+
7
+ return { x: box?.x ?? 0, y: box?.y ?? 0 };
8
+ }
9
+
10
+ function session(context) {
11
+ let existing = sessions.get(context.page);
12
+ if (existing === undefined) {
13
+ existing = (async () => {
14
+ const created = await context.context.newCDPSession(context.page);
15
+ await created.send("Emulation.setTouchEmulationEnabled", {
16
+ enabled: true,
17
+ maxTouchPoints: 5,
18
+ });
19
+
20
+ return created;
21
+ })();
22
+ sessions.set(context.page, existing);
23
+ }
24
+
25
+ return existing;
26
+ }
27
+
28
+ export const browserCommands = {
29
+ trustedMouse: async (context, input) => {
30
+ const shift = await origin(context);
31
+ const mouse = context.page.mouse;
32
+ await mouse.move(shift.x + input.x, shift.y + input.y);
33
+ if (input.action === "down") {
34
+ await mouse.down({ button: input.button ?? "left" });
35
+ } else if (input.action === "up") {
36
+ await mouse.up({ button: input.button ?? "left" });
37
+ } else if (input.action === "wheel") {
38
+ await mouse.wheel(input.deltaX ?? 0, input.deltaY ?? 0);
39
+ }
40
+ },
41
+ trustedKey: async (context, input) => {
42
+ if (input.action === "down") {
43
+ await context.page.keyboard.down(input.key);
44
+ } else {
45
+ await context.page.keyboard.up(input.key);
46
+ }
47
+ },
48
+ dispatchTouch: async (context, input) => {
49
+ const shift = await origin(context);
50
+ const cdp = await session(context);
51
+ await cdp.send("Input.dispatchTouchEvent", {
52
+ type: input.type,
53
+ touchPoints: input.points.map((point) => ({
54
+ x: shift.x + point.x,
55
+ y: shift.y + point.y,
56
+ id: point.id,
57
+ })),
58
+ });
59
+ },
60
+ synthesizePinch: async (context, input) => {
61
+ const shift = await origin(context);
62
+ const cdp = await session(context);
63
+ await cdp.send("Input.synthesizePinchGesture", {
64
+ x: shift.x + input.x,
65
+ y: shift.y + input.y,
66
+ scaleFactor: input.scaleFactor,
67
+ relativeSpeed: input.relativeSpeed ?? 800,
68
+ });
69
+ },
70
+ };
package/browser.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { LaunchOptions } from "playwright";
2
+ import type { defineConfig } from "vitest/config";
3
+ import type { BrowserCommand } from "vitest/node";
4
+
5
+ export type BrowserName = "chromium" | "firefox" | "webkit";
6
+
7
+ /** One named configuration of a run, such as one GPU setup of a graphics test. */
8
+ export type BrowserProjectOptions = {
9
+ /** The project's name, as `--project` takes it. */
10
+ readonly name: string;
11
+ /** Values the tests read through `inject()`: `test.provide` of this project. */
12
+ readonly provide?: Record<string, unknown>;
13
+ /** Added to (and overriding) the config's `launchOptions`, for this project. */
14
+ readonly launchOptions?: LaunchOptions;
15
+ };
16
+
17
+ export type BrowserConfigOptions = {
18
+ /** Default `["src/**\/*.browser.test.ts"]`. */
19
+ readonly include?: string[];
20
+ /** Default: `VITEST_BROWSERS` (comma separated), else Chromium alone. */
21
+ readonly browsers?: readonly BrowserName[];
22
+ /**
23
+ * Playwright's launch options for the run, `args` such as GPU flags included,
24
+ * given to every engine. Chromium's `channel` defaults to `"chromium"` (the
25
+ * full build, not the headless shell) whichever other engines run.
26
+ */
27
+ readonly launchOptions?: LaunchOptions;
28
+ /**
29
+ * Runs the same tests once per entry, each as a named project with its own
30
+ * `provide` and launch options. Without it the config is one project.
31
+ */
32
+ readonly projects?: readonly BrowserProjectOptions[];
33
+ /** Server-side commands the tests call, with access to the Playwright page. */
34
+ readonly commands?: Record<string, BrowserCommand<never[]>>;
35
+ /** Other `test` options. */
36
+ readonly test?: Record<string, unknown>;
37
+ };
38
+
39
+ /** Real-browser tests through Playwright, headless. */
40
+ declare const defineBrowserConfig: (
41
+ options?: BrowserConfigOptions,
42
+ ) => ReturnType<typeof defineConfig>;
43
+ export { defineBrowserConfig };
package/browser.js ADDED
@@ -0,0 +1,69 @@
1
+ import { playwright } from "@vitest/browser-playwright";
2
+ import { defineConfig, defineProject } from "vitest/config";
3
+
4
+ const DEFAULT_BROWSERS = ["chromium"];
5
+
6
+ /*
7
+ * `VITEST_BROWSERS=chromium,firefox,webkit` picks the engines of a run; the
8
+ * default is Chromium alone, which every Playwright install has.
9
+ */
10
+ function browsersOf(options) {
11
+ if (options?.browsers !== undefined) {
12
+ return options.browsers;
13
+ }
14
+ const fromEnvironment = process.env.VITEST_BROWSERS?.split(",")
15
+ .map((name) => name.trim())
16
+ .filter((name) => name !== "");
17
+
18
+ return fromEnvironment !== undefined && fromEnvironment.length > 0
19
+ ? fromEnvironment
20
+ : DEFAULT_BROWSERS;
21
+ }
22
+
23
+ function testOf(options, project) {
24
+ const browsers = browsersOf(options);
25
+ const launchOptions = {
26
+ ...options?.launchOptions,
27
+ ...project?.launchOptions,
28
+ };
29
+ // Chromium runs its full build rather than its headless shell, whatever
30
+ // other engines run beside it (they have no such channel): its instance
31
+ // takes its own provider, the same Playwright one with the channel added.
32
+ const chromiumProvider = playwright({
33
+ launchOptions: { channel: "chromium", ...launchOptions },
34
+ });
35
+
36
+ return {
37
+ include: options?.include ?? ["src/**/*.browser.test.ts"],
38
+ ...options?.test,
39
+ ...(project === undefined ? {} : { name: project.name }),
40
+ ...(project?.provide === undefined ? {} : { provide: project.provide }),
41
+ browser: {
42
+ enabled: true,
43
+ headless: true,
44
+ screenshotFailures: false,
45
+ // Playwright takes the launch options per provider, not per instance
46
+ provider: playwright({ launchOptions }),
47
+ instances: browsers.map((browser) =>
48
+ browser === "chromium"
49
+ ? { browser, provider: chromiumProvider }
50
+ : { browser },
51
+ ),
52
+ commands: options?.commands,
53
+ },
54
+ };
55
+ }
56
+
57
+ export function defineBrowserConfig(options) {
58
+ if (options?.projects === undefined) {
59
+ return defineConfig({ test: testOf(options, undefined) });
60
+ }
61
+
62
+ return defineConfig({
63
+ test: {
64
+ projects: options.projects.map((project) =>
65
+ defineProject({ test: testOf(options, project) }),
66
+ ),
67
+ },
68
+ });
69
+ }
package/package.json CHANGED
@@ -1,23 +1,27 @@
1
1
  {
2
2
  "name": "@ac-kit/vitest-config",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Shareable Vitest configuration for @ac-kit packages: Node environment, type-checking enabled.",
5
5
  "keywords": [
6
6
  "config",
7
7
  "testing",
8
8
  "vitest"
9
9
  ],
10
- "homepage": "https://github.com/anthochamp/node-essentials/tree/main/packages/vitest-config#readme",
10
+ "homepage": "https://git.grenadine.io/anthochamp/node-essentials/src/branch/main/packages/vitest-config#readme",
11
11
  "bugs": {
12
- "url": "https://github.com/anthochamp/node-essentials/issues"
12
+ "url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
13
13
  },
14
14
  "license": "BSD-3-Clause",
15
15
  "author": "Anthony Champagne <dev@anthonychampagne.fr>",
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "git+https://github.com/anthochamp/node-essentials.git",
18
+ "url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
19
19
  "directory": "packages/vitest-config"
20
20
  },
21
+ "files": [
22
+ "*.js",
23
+ "*.d.ts"
24
+ ],
21
25
  "type": "module",
22
26
  "main": "./lib.js",
23
27
  "exports": {
@@ -25,6 +29,14 @@
25
29
  "types": "./lib.d.ts",
26
30
  "default": "./lib.js"
27
31
  },
32
+ "./browser": {
33
+ "types": "./browser.d.ts",
34
+ "default": "./browser.js"
35
+ },
36
+ "./browser-commands": {
37
+ "types": "./browser-commands.d.ts",
38
+ "default": "./browser-commands.js"
39
+ },
28
40
  "./cli": {
29
41
  "types": "./cli.d.ts",
30
42
  "default": "./cli.js"
@@ -37,6 +49,25 @@
37
49
  "publishConfig": {
38
50
  "access": "public"
39
51
  },
52
+ "scripts": {
53
+ "test": "yarn run -T vitest run"
54
+ },
55
+ "devDependencies": {
56
+ "@vitest/browser-playwright": "5.1.1",
57
+ "playwright": "1.65.1"
58
+ },
59
+ "peerDependencies": {
60
+ "@vitest/browser-playwright": "^5.1.1",
61
+ "playwright": "^1.65.1"
62
+ },
63
+ "peerDependenciesMeta": {
64
+ "@vitest/browser-playwright": {
65
+ "optional": true
66
+ },
67
+ "playwright": {
68
+ "optional": true
69
+ }
70
+ },
40
71
  "engines": {
41
72
  "node": ">=24"
42
73
  }