@argos-ci/vitest 0.2.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 Smooth Code
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ <p align="center">
2
+ <a href="https://argos-ci.com/?utm_source=github&utm_medium=logo" target="_blank">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/argos-ci/argos/main/resources/logos/github-readme-logo-dark.png">
5
+ <img alt="Argos" src="https://raw.githubusercontent.com/argos-ci/argos/main/resources/logos/github-readme-logo-light.png" width="360" height="70">
6
+ </picture>
7
+ </a>
8
+ </p>
9
+
10
+ <p align="center"><strong>The open source visual testing platform for AI-native engineering teams.</strong></p>
11
+
12
+ # Official Argos Vitest integration
13
+
14
+ [![npm version](https://img.shields.io/npm/v/@argos-ci/vitest.svg)](https://www.npmjs.com/package/@argos-ci/vitest)
15
+ [![npm dm](https://img.shields.io/npm/dm/@argos-ci/vitest.svg)](https://www.npmjs.com/package/@argos-ci/vitest)
16
+ [![npm dt](https://img.shields.io/npm/dt/@argos-ci/vitest.svg)](https://www.npmjs.com/package/@argos-ci/vitest)
17
+
18
+ Capture Argos screenshots directly from your [Vitest browser tests](https://vitest.dev/guide/browser/).
19
+
20
+ Visit the [Vitest SDK documentation](https://argos-ci.com/docs/sdks-reference/vitest) for guides, API and more.
21
+
22
+ ## Installation
23
+
24
+ Install the package:
25
+
26
+ ```sh
27
+ npm install --save-dev @argos-ci/vitest
28
+ ```
29
+
30
+ `argosSnapshot` runs in any Vitest test — browser or Node — and needs nothing else.
31
+
32
+ To capture screenshots with `argosScreenshot`, run your tests in
33
+ [Vitest browser mode](https://vitest.dev/guide/browser/) with the
34
+ [Playwright provider](https://vitest.dev/guide/browser/playwright) and install the
35
+ following peer dependencies:
36
+
37
+ ```sh
38
+ npm install --save-dev vitest @vitest/browser @vitest/browser-playwright playwright
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ Register the plugin in your Vitest config:
44
+
45
+ ```ts
46
+ // vitest.config.ts
47
+ import { defineConfig } from "vitest/config";
48
+ import { playwright } from "@vitest/browser-playwright";
49
+ import { argosVitestPlugin } from "@argos-ci/vitest/plugin";
50
+
51
+ export default defineConfig({
52
+ plugins: [
53
+ argosVitestPlugin({
54
+ // Upload the screenshots to Argos at the end of the run.
55
+ uploadToArgos: process.env.CI === "true",
56
+ }),
57
+ ],
58
+ test: {
59
+ browser: {
60
+ enabled: true,
61
+ headless: true,
62
+ provider: playwright(),
63
+ instances: [{ browser: "chromium" }],
64
+ },
65
+ },
66
+ });
67
+ ```
68
+
69
+ Then take screenshots from your browser tests:
70
+
71
+ ```ts
72
+ import { test } from "vitest";
73
+ import { render } from "vitest-browser-react";
74
+ import { argosScreenshot } from "@argos-ci/vitest";
75
+ import { Button } from "./Button";
76
+
77
+ test("Button", async () => {
78
+ render(<Button>Click me</Button>);
79
+ await argosScreenshot("button");
80
+ });
81
+ ```
82
+
83
+ ## Snapshots
84
+
85
+ `argosSnapshot` captures a snapshot of any value — not just a screenshot — and
86
+ uploads it to Argos to diff across builds, mimicking
87
+ [Vitest snapshots](https://vitest.dev/guide/snapshot). Unlike `argosScreenshot`,
88
+ it does not need a browser and works in **both** browser and Node tests.
89
+
90
+ ```ts
91
+ import { test } from "vitest";
92
+ import { argosSnapshot } from "@argos-ci/vitest";
93
+
94
+ test("API response", async () => {
95
+ const user = await fetchUser();
96
+ // Objects are serialized with `@vitest/pretty-format`, strings are written
97
+ // verbatim.
98
+ await argosSnapshot("user", user);
99
+ });
100
+ ```
101
+
102
+ Use the `extension` option to control how Argos renders and diffs the snapshot,
103
+ and `tag` to attach tags:
104
+
105
+ ```ts
106
+ await argosSnapshot("config", JSON.stringify(config, null, 2), {
107
+ extension: ".json",
108
+ tag: "config",
109
+ });
110
+ ```
111
+
112
+ Snapshots are written to the same folder as screenshots and uploaded by the
113
+ reporter when `uploadToArgos` is enabled.
114
+
115
+ ## Links
116
+
117
+ - [Official SDK Docs](https://argos-ci.com/docs/sdks-reference/vitest)
118
+ - [Quickstart](https://argos-ci.com/docs/quickstart/vitest-quickstart)
119
+ - [Discord](https://argos-ci.com/discord)
@@ -0,0 +1,175 @@
1
+ import { ArgosAttachment } from "@argos-ci/playwright";
2
+ import { StabilizationPluginOptions, ViewportOption } from "@argos-ci/browser";
3
+
4
+ //#region src/options.d.ts
5
+ /**
6
+ * Options passed when calling `argosScreenshot` from a browser test.
7
+ *
8
+ * These options cross the Vitest browser/node RPC boundary, so they must be
9
+ * JSON-serializable. Non-serializable options (`beforeScreenshot`,
10
+ * `afterScreenshot`, a `Locator`/`ElementHandle` `element`, …) can only be set
11
+ * on the plugin via {@link ArgosVitestPluginOptions}.
12
+ */
13
+ interface VitestScreenshotOptions {
14
+ /**
15
+ * String selector of the element to take a screenshot of.
16
+ * A `Locator`/`ElementHandle` cannot be used here because it can't be
17
+ * serialized across the browser/node boundary — set it on the plugin instead.
18
+ */
19
+ element?: string;
20
+ /**
21
+ * Viewports to take screenshots of.
22
+ * Implemented by resizing the Vitest iframe (Playwright's native `viewports`
23
+ * option does not work on a frame).
24
+ */
25
+ viewports?: ViewportOption[];
26
+ /**
27
+ * Capture the full page instead of fitting the screenshot to the content.
28
+ * - `false` (default): the iframe grows to fit the content in both
29
+ * dimensions, so nothing is clipped.
30
+ * - `true`: keep the viewport width and grow the height (Playwright-style
31
+ * full page).
32
+ * @default false
33
+ */
34
+ fullPage?: boolean;
35
+ /**
36
+ * Custom CSS evaluated during the screenshot process.
37
+ */
38
+ argosCSS?: string;
39
+ /**
40
+ * Sensitivity threshold between 0 and 1.
41
+ * The higher the threshold, the less sensitive the diff will be.
42
+ * @default 0.5
43
+ */
44
+ threshold?: number;
45
+ /**
46
+ * Tag or array of tags to attach to the screenshot.
47
+ */
48
+ tag?: string | string[];
49
+ /**
50
+ * Capture an ARIA snapshot along with the screenshot.
51
+ * @default false
52
+ */
53
+ ariaSnapshot?: boolean;
54
+ /**
55
+ * Disable hover effects by moving the mouse to the top-left corner.
56
+ * @default true
57
+ */
58
+ disableHover?: boolean;
59
+ /**
60
+ * Wait for the UI to stabilize before taking the screenshot.
61
+ * Set to `false` to disable stabilization or pass an object to customize it.
62
+ * @default true
63
+ */
64
+ stabilize?: boolean | StabilizationPluginOptions;
65
+ }
66
+ /**
67
+ * Options passed when calling `argosSnapshot`.
68
+ *
69
+ * `argosSnapshot` serializes any value to a file that Argos picks up and diffs,
70
+ * mimicking {@link https://vitest.dev/guide/snapshot Vitest snapshots}. Unlike
71
+ * `argosScreenshot`, it does not need a browser and works both in Vitest browser
72
+ * tests and in plain Node tests.
73
+ *
74
+ * These options must be JSON-serializable so they can cross the Vitest
75
+ * browser/node RPC boundary — the only exception is `serialize`, which is
76
+ * applied on the test side *before* the value is sent to Node.
77
+ */
78
+ interface VitestSnapshotOptions {
79
+ /**
80
+ * Folder where the snapshot is written.
81
+ *
82
+ * In Node tests this defaults to `"./screenshots"`. In browser tests it
83
+ * defaults to the plugin `root` and can be overridden per call.
84
+ * @default "./screenshots"
85
+ */
86
+ root?: string;
87
+ /**
88
+ * Extension of the snapshot file. It also determines how Argos renders and
89
+ * diffs the snapshot (e.g. `.txt`, `.json`, `.yml`, `.html`, `.md`).
90
+ * @default ".txt"
91
+ */
92
+ extension?: string;
93
+ /**
94
+ * Tag or array of tags to attach to the snapshot.
95
+ */
96
+ tag?: string | string[];
97
+ /**
98
+ * Custom serializer used when `content` is not already a string.
99
+ * Defaults to `@vitest/pretty-format` (the serializer Vitest itself uses).
100
+ */
101
+ serialize?: (content: unknown) => string;
102
+ }
103
+ /**
104
+ * Subset of {@link VitestSnapshotOptions} that can cross the Vitest
105
+ * browser/node RPC boundary (everything but `serialize`, which is applied
106
+ * before the value is sent to Node).
107
+ */
108
+ type SerializableSnapshotOptions = Omit<VitestSnapshotOptions, "serialize">;
109
+ //#endregion
110
+ //#region src/index.d.ts
111
+ declare module "vitest/browser" {
112
+ interface BrowserCommands {
113
+ argosScreenshot: (name: string, options?: VitestScreenshotOptions) => Promise<ArgosAttachment[]>;
114
+ argosSnapshot: (name: string, content: string, options?: SerializableSnapshotOptions) => Promise<ArgosAttachment[]>;
115
+ }
116
+ }
117
+ /**
118
+ * Take an Argos screenshot in a Vitest browser test.
119
+ *
120
+ * Requires the {@link https://www.npmjs.com/package/@argos-ci/vitest Argos Vitest plugin}
121
+ * to be registered in your Vitest config.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * import { render } from "vitest-browser-react";
126
+ * import { argosScreenshot } from "@argos-ci/vitest";
127
+ *
128
+ * test("Button", async () => {
129
+ * render(<Button>Click me</Button>);
130
+ * await argosScreenshot("button");
131
+ * });
132
+ * ```
133
+ *
134
+ * @param name - Unique name of the screenshot.
135
+ * @param options - Serializable screenshot options.
136
+ * @returns The attachments captured, or an empty array outside of Vitest.
137
+ */
138
+ declare function argosScreenshot(name: string, options?: VitestScreenshotOptions): Promise<ArgosAttachment[]>;
139
+ /**
140
+ * Take an Argos snapshot of any serializable value, mimicking
141
+ * {@link https://vitest.dev/guide/snapshot Vitest snapshots}.
142
+ *
143
+ * Unlike {@link argosScreenshot}, this does not need a browser: it serializes
144
+ * the value (strings verbatim, everything else via `@vitest/pretty-format`) to a
145
+ * file that Argos picks up and diffs across builds. It works both in Vitest
146
+ * browser tests and in plain Node tests.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * import { argosSnapshot } from "@argos-ci/vitest";
151
+ *
152
+ * test("API response", async () => {
153
+ * const data = await fetchUser();
154
+ * await argosSnapshot("user", data);
155
+ * });
156
+ * ```
157
+ *
158
+ * @param name - Unique name of the snapshot.
159
+ * @param content - The value to snapshot. Strings are written as-is; any other
160
+ * value is serialized.
161
+ * @param options - Snapshot options.
162
+ * @returns The attachments written, or an empty array outside of Vitest.
163
+ */
164
+ declare function argosSnapshot(name: string, content: unknown, options?: VitestSnapshotOptions): Promise<ArgosAttachment[]>;
165
+ /**
166
+ * Check if we are running in a Vitest environment.
167
+ */
168
+ declare function checkIsVitestEnv(): Promise<boolean>;
169
+ /**
170
+ * Check if we are running in Vitest browser mode.
171
+ * Vitest sets this global in the browser runner.
172
+ */
173
+ declare function checkIsBrowserEnv(): boolean;
174
+ //#endregion
175
+ export { type VitestScreenshotOptions, type VitestSnapshotOptions, argosScreenshot, argosSnapshot, checkIsBrowserEnv, checkIsVitestEnv };