@ac-kit/vitest-config 0.1.1 → 0.3.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,32 @@
1
1
  # @ac-kit/vitest-config
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1242374: `defineProject` now defaults `testTimeout` to 30 s and `hookTimeout` to 60 s,
8
+ instead of Vitest's 5 s and 10 s, so sound tests do not time out on a loaded CI
9
+ runner. A project's own `test` options still override both.
10
+
11
+ ## 0.2.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 34e8cfa: Run a package's `*.browser.test.ts` files in a real browser with
16
+ `defineBrowserConfig` from `@ac-kit/vitest-config/browser`: headless Chromium
17
+ through Playwright by default (its full build, `channel: "chromium"` unless you
18
+ set it), `VITEST_BROWSERS=chromium,firefox,webkit` to choose the engines, and
19
+ your own server-side `commands` for trusted input. It sets no GPU flags:
20
+ Playwright's `launchOptions` (`args` such as GPU flags) are given to every
21
+ engine, and `projects` runs the same tests once per named entry, each with its
22
+ own `provide` and `launchOptions` over the shared ones.
23
+
24
+ `@ac-kit/vitest-config/browser-commands` exports `browserCommands`, the
25
+ server-side commands that give a browser test trusted input: `trustedMouse`
26
+ (move, down, up, wheel), `trustedKey`, and, in Chromium, `dispatchTouch` and
27
+ `synthesizePinch`. Pass it as `commands` to `defineBrowserConfig`; the exported
28
+ `TrustedBrowserCommands` types them for `vitest/browser`'s `commands`.
29
+
3
30
  ## 0.1.1
4
31
 
5
32
  ### Patch Changes
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # @ac-kit/vitest-config
2
2
 
3
3
  A shareable Vitest configuration for `@ac-kit` packages: Node environment,
4
- type-checking enabled.
4
+ type-checking enabled, and timeouts sized for a loaded CI runner (30 s a test,
5
+ 60 s a hook) rather than Vitest's 5 s and 10 s.
5
6
 
6
7
  ## Usage
7
8
 
@@ -12,3 +13,55 @@ export default defineProject({
12
13
  // project-specific overrides
13
14
  });
14
15
  ```
16
+
17
+ ## Browser tests
18
+
19
+ `@ac-kit/vitest-config/browser` runs `src/**/*.browser.test.ts` in a real
20
+ browser through Playwright, headless. Exclude those files
21
+ from the package's `vitest.config.ts` (`**/*.browser.test.ts`) so `yarn test`
22
+ never runs them, and put the preset in `vitest.browser.config.ts`:
23
+
24
+ ```ts
25
+ import { defineBrowserConfig } from "@ac-kit/vitest-config/browser";
26
+
27
+ export default defineBrowserConfig({
28
+ // include: ["src/**/*.browser.test.ts"],
29
+ // commands: { trustedMouse: async (context, input) => {} },
30
+ });
31
+ ```
32
+
33
+ Chromium alone runs by default; `VITEST_BROWSERS=chromium,firefox,webkit` picks
34
+ other engines (their Playwright builds must be installed). `@vitest/browser-playwright`
35
+ and `playwright` are optional peer dependencies, loaded only by this subpath: a
36
+ package that runs browser tests declares them in its own `devDependencies`.
37
+ `commands` are
38
+ server-side functions the tests call through `vitest/browser`'s `commands`, with
39
+ the Playwright `page` in their context. `@ac-kit/vitest-config/browser-commands`
40
+ exports ready-made ones for trusted input: `trustedMouse` (move, down, up,
41
+ wheel), `trustedKey` and, in Chromium, `dispatchTouch` and `synthesizePinch`;
42
+ `defineBrowserConfig({ commands: browserCommands })`. Type `commands` from
43
+ `vitest/browser` in the test file with `declare module "vitest/browser" {
44
+ interface BrowserCommands extends TrustedBrowserCommands {} }`, importing
45
+ `TrustedBrowserCommands` from the same module as a type (it cannot reach the
46
+ package's own copy of Vitest itself).
47
+
48
+ `launchOptions` is Playwright's `LaunchOptions` for the run, given to every
49
+ engine: `args` carry flags such as GPU or software-rendering switches. Chromium
50
+ runs with `channel: "chromium"` (its full build, not the headless shell) unless
51
+ you set `channel`, whichever other engines run beside it. `projects` runs the same tests
52
+ once per entry, each a named project (`--project <name>`) with its own `provide`
53
+ (read in a test with `inject()`) and `launchOptions` merged over the shared ones:
54
+
55
+ ```ts
56
+ export default defineBrowserConfig({
57
+ launchOptions: { args: ["--use-angle=swiftshader"] },
58
+ projects: [
59
+ { name: "software", provide: { hardware: false } },
60
+ {
61
+ name: "hardware",
62
+ provide: { hardware: true },
63
+ launchOptions: { args: ["--enable-unsafe-webgpu"] },
64
+ },
65
+ ],
66
+ });
67
+ ```
@@ -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,73 @@
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
+ /** A CDP session of the page with touch emulation on. */
11
+ async function openSession(context) {
12
+ const created = await context.context.newCDPSession(context.page);
13
+ await created.send("Emulation.setTouchEmulationEnabled", {
14
+ enabled: true,
15
+ maxTouchPoints: 5,
16
+ });
17
+
18
+ return created;
19
+ }
20
+
21
+ function session(context) {
22
+ let existing = sessions.get(context.page);
23
+ if (existing === undefined) {
24
+ existing = openSession(context);
25
+ sessions.set(context.page, existing);
26
+ }
27
+
28
+ return existing;
29
+ }
30
+
31
+ export const browserCommands = {
32
+ trustedMouse: async (context, input) => {
33
+ const shift = await origin(context);
34
+ const mouse = context.page.mouse;
35
+ await mouse.move(shift.x + input.x, shift.y + input.y);
36
+ if (input.action === "down") {
37
+ await mouse.down({ button: input.button ?? "left" });
38
+ } else if (input.action === "up") {
39
+ await mouse.up({ button: input.button ?? "left" });
40
+ } else if (input.action === "wheel") {
41
+ await mouse.wheel(input.deltaX ?? 0, input.deltaY ?? 0);
42
+ }
43
+ },
44
+ trustedKey: async (context, input) => {
45
+ if (input.action === "down") {
46
+ await context.page.keyboard.down(input.key);
47
+ } else {
48
+ await context.page.keyboard.up(input.key);
49
+ }
50
+ },
51
+ dispatchTouch: async (context, input) => {
52
+ const shift = await origin(context);
53
+ const cdp = await session(context);
54
+ await cdp.send("Input.dispatchTouchEvent", {
55
+ type: input.type,
56
+ touchPoints: input.points.map((point) => ({
57
+ x: shift.x + point.x,
58
+ y: shift.y + point.y,
59
+ id: point.id,
60
+ })),
61
+ });
62
+ },
63
+ synthesizePinch: async (context, input) => {
64
+ const shift = await origin(context);
65
+ const cdp = await session(context);
66
+ await cdp.send("Input.synthesizePinchGesture", {
67
+ x: shift.x + input.x,
68
+ y: shift.y + input.y,
69
+ scaleFactor: input.scaleFactor,
70
+ relativeSpeed: input.relativeSpeed ?? 800,
71
+ });
72
+ },
73
+ };
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/lib.js CHANGED
@@ -6,6 +6,9 @@ export function defineProject(options) {
6
6
  test: {
7
7
  environment: "node",
8
8
  fsModuleCache: true,
9
+ // Vitest's 5 s and 10 s defaults fail sound tests on a loaded CI runner.
10
+ testTimeout: 30_000,
11
+ hookTimeout: 60_000,
9
12
  ...options?.test,
10
13
  },
11
14
  });
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.3.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.2",
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
  }