@neon-pixels/playwright-preset 0.0.0 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Neon Pixels Studio
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,130 @@
1
+ # @neon-pixels/playwright-preset
2
+
3
+ Shared [Playwright](https://playwright.dev) configuration for Neon Pixels projects.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add -D @playwright/test @neon-pixels/playwright-preset
9
+ ```
10
+
11
+ Playwright is a peer dependency, so install it yourself. For the Clerk entry point also install:
12
+
13
+ ```sh
14
+ pnpm add -D @clerk/testing
15
+ ```
16
+
17
+ Only repos that import the `/clerk` entry point need `@clerk/testing`. Only repos that import the `/consent` entry point need `@neon-pixels/consent`:
18
+
19
+ ```sh
20
+ pnpm add -D @neon-pixels/consent
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ | Entry point | What it does |
26
+ | ---------------------------------------- | ----------------------------------------------------------------------------- |
27
+ | `@neon-pixels/playwright-preset` | Base config: reporter, CI retries, trace on first retry, Chromium, web server |
28
+ | `@neon-pixels/playwright-preset/clerk` | Base config plus a Clerk sign-in `setup` project that saves the browser state |
29
+ | `@neon-pixels/playwright-preset/consent` | Browser storage with the analytics consent choice already declined, or none |
30
+
31
+ ### Base
32
+
33
+ ```ts
34
+ // playwright.config.ts
35
+ import { defineConfig } from "@playwright/test";
36
+ import { createPlaywrightConfig } from "@neon-pixels/playwright-preset";
37
+
38
+ export default defineConfig({
39
+ ...createPlaywrightConfig({ command: "pnpm dev --port 3000", port: 3000 }),
40
+ testDir: "./e2e",
41
+ });
42
+ ```
43
+
44
+ `command` starts your app and `port` is where it listens. Both are required: the preset has no default port, so parallel repos never collide by accident. The web server `url` and `baseURL` are `http://localhost:<port>`. Pass `env` to set environment variables for the server process.
45
+
46
+ The config reuses a running server locally, always starts a fresh one when `CI` is set, retries failed tests twice in CI, and records a trace on the first retry.
47
+
48
+ ### Clerk
49
+
50
+ ```ts
51
+ // playwright.config.ts
52
+ import { defineConfig } from "@playwright/test";
53
+ import { createClerkPlaywrightConfig } from "@neon-pixels/playwright-preset/clerk";
54
+
55
+ export default defineConfig({
56
+ ...createClerkPlaywrightConfig({
57
+ command: "pnpm dev --port 3000",
58
+ port: 3000,
59
+ }),
60
+ testDir: "./e2e",
61
+ });
62
+ ```
63
+
64
+ ```ts
65
+ // e2e/auth.setup.ts
66
+ import { defineClerkAuthSetup } from "@neon-pixels/playwright-preset/clerk";
67
+
68
+ defineClerkAuthSetup({
69
+ emailAddress: "e2e-test@example.com",
70
+ signInPath: "/login",
71
+ protectedPath: "/dashboard",
72
+ });
73
+ ```
74
+
75
+ The config adds a `setup` project that runs any file ending in `auth.setup.ts`. The `chromium` project depends on it and loads the saved state from `e2e/.auth/user.json` (exported as `AUTH_STATE_FILE`; add it to `.gitignore`). The path is relative to the directory you run Playwright from, so run it from the repo root that holds `e2e/`. The user must already exist in your Clerk instance.
76
+
77
+ Building the config also copies `NUXT_CLERK_SECRET_KEY` and `NUXT_PUBLIC_CLERK_PUBLISHABLE_KEY` onto the `CLERK_SECRET_KEY` and `CLERK_PUBLISHABLE_KEY` names that `@clerk/testing` reads, without overwriting values already set. `clerkSetup()` runs inside the setup test rather than in `globalSetup`, so the testing token is not inherited by your web server, where it would sign in every request.
78
+
79
+ To change anything, override it in your own config: spread the result and replace the key.
80
+
81
+ ### Consent
82
+
83
+ With analytics configured, the consent banner shows on every page and can cover the controls a spec is testing. `consentDeclinedStorageState` returns a Playwright `storageState` that already holds a declined choice, written under the storage key and record shape that [`@neon-pixels/consent`](../consent) reads (`<app>:analytics-consent`, with the package's full consent lifetime).
84
+
85
+ ```ts
86
+ // playwright.config.ts
87
+ import { defineConfig } from "@playwright/test";
88
+ import { createPlaywrightConfig } from "@neon-pixels/playwright-preset";
89
+ import { consentDeclinedStorageState } from "@neon-pixels/playwright-preset/consent";
90
+
91
+ const base = createPlaywrightConfig({
92
+ command: "pnpm dev --port 3000",
93
+ port: 3000,
94
+ });
95
+
96
+ export default defineConfig({
97
+ ...base,
98
+ use: {
99
+ ...base.use,
100
+ storageState: consentDeclinedStorageState({
101
+ app: "my-site", // the `app` you pass to createConsent
102
+ origin: "http://localhost:3000", // only the origin of the URL is used
103
+ }),
104
+ },
105
+ testDir: "./e2e",
106
+ });
107
+ ```
108
+
109
+ `app` and `origin` are required, and there is no default for either. The state only applies to that origin. Pass `now` (epoch milliseconds) to fix the time the choice is made at.
110
+
111
+ Specs about consent itself start with no stored choice:
112
+
113
+ ```ts
114
+ import { test } from "@playwright/test";
115
+ import { consentUnsetStorageState } from "@neon-pixels/playwright-preset/consent";
116
+
117
+ test.describe("consent banner", () => {
118
+ test.use({ storageState: consentUnsetStorageState() });
119
+
120
+ test("shows on a first visit", async ({ page }) => {
121
+ // ...
122
+ });
123
+ });
124
+ ```
125
+
126
+ A project has one `storageState`, so this replaces a saved sign-in file rather than merging with it.
127
+
128
+ ## License
129
+
130
+ MIT
package/clerk.d.ts ADDED
@@ -0,0 +1,46 @@
1
+ import type { Page, PlaywrightTestConfig } from "@playwright/test";
2
+ import type { PlaywrightPresetOptions } from "./index.js";
3
+
4
+ /** Where the setup project writes the signed-in browser state. */
5
+ export declare const AUTH_STATE_FILE: string;
6
+
7
+ export interface ClerkAuthSetupOptions {
8
+ /** Email of an existing Clerk user to sign in as. */
9
+ emailAddress: string;
10
+ /** Page that loads Clerk JS before sign-in, such as `/login`. */
11
+ signInPath: string;
12
+ /** Page that needs a session; reaching it confirms sign-in worked. */
13
+ protectedPath: string;
14
+ }
15
+
16
+ export interface ClerkTesting {
17
+ clerk: {
18
+ signIn(options: { page: Page; emailAddress: string }): Promise<void>;
19
+ };
20
+ clerkSetup(): Promise<unknown>;
21
+ }
22
+
23
+ /** The setup test body, exported so it can run against a fake Clerk client. */
24
+ export declare function authenticate(
25
+ options: ClerkAuthSetupOptions & {
26
+ page: Page;
27
+ clerkTesting?: ClerkTesting;
28
+ },
29
+ ): Promise<void>;
30
+
31
+ /**
32
+ * Copies the Nuxt Clerk variable names onto the names `@clerk/testing`
33
+ * reads. Never overwrites a value that is already set.
34
+ */
35
+ export declare function applyClerkEnvironment(
36
+ env?: Record<string, string | undefined>,
37
+ ): void;
38
+
39
+ export declare function createClerkPlaywrightConfig(
40
+ options: PlaywrightPresetOptions,
41
+ ): PlaywrightTestConfig;
42
+
43
+ /** Registers the "authenticate" setup test. Call it from `e2e/auth.setup.ts`. */
44
+ export declare function defineClerkAuthSetup(
45
+ options: ClerkAuthSetupOptions,
46
+ ): void;
package/clerk.js ADDED
@@ -0,0 +1,85 @@
1
+ import process from "node:process";
2
+ import { URL } from "node:url";
3
+ import { test as setup } from "@playwright/test";
4
+ import { clerk, clerkSetup } from "@clerk/testing/playwright";
5
+ import { createPlaywrightConfig } from "./index.js";
6
+
7
+ export const AUTH_STATE_FILE = "e2e/.auth/user.json";
8
+
9
+ const SETUP_PROJECT_NAME = "setup";
10
+ const SETUP_FILE_PATTERN = /auth\.setup\.[cm]?[jt]s$/;
11
+
12
+ // @clerk/testing reads the standard CLERK_* names; Nuxt apps use these.
13
+ const CLERK_ENV_ALIASES = [
14
+ ["NUXT_CLERK_SECRET_KEY", "CLERK_SECRET_KEY"],
15
+ ["NUXT_PUBLIC_CLERK_PUBLISHABLE_KEY", "CLERK_PUBLISHABLE_KEY"],
16
+ ];
17
+
18
+ export function applyClerkEnvironment(env = process.env) {
19
+ for (const [nuxtName, clerkName] of CLERK_ENV_ALIASES) {
20
+ if (env[clerkName] || !env[nuxtName]) {
21
+ continue;
22
+ }
23
+ env[clerkName] = env[nuxtName];
24
+ }
25
+ }
26
+
27
+ /** @param {import("./index.js").PlaywrightPresetOptions} options */
28
+ export function createClerkPlaywrightConfig(options) {
29
+ applyClerkEnvironment();
30
+ const base = createPlaywrightConfig(options);
31
+ const [browserProject] = base.projects ?? [];
32
+
33
+ return {
34
+ ...base,
35
+ projects: [
36
+ { name: SETUP_PROJECT_NAME, testMatch: SETUP_FILE_PATTERN },
37
+ {
38
+ ...browserProject,
39
+ dependencies: [SETUP_PROJECT_NAME],
40
+ testIgnore: SETUP_FILE_PATTERN,
41
+ use: { ...browserProject?.use, storageState: AUTH_STATE_FILE },
42
+ },
43
+ ],
44
+ };
45
+ }
46
+
47
+ /**
48
+ * @param {import("./clerk.js").ClerkAuthSetupOptions & {
49
+ * page: import("@playwright/test").Page;
50
+ * clerkTesting?: Pick<import("./clerk.js").ClerkTesting, "clerk" | "clerkSetup">;
51
+ * }} options
52
+ */
53
+ export async function authenticate(options) {
54
+ const {
55
+ page,
56
+ emailAddress,
57
+ signInPath,
58
+ protectedPath,
59
+ clerkTesting = { clerk, clerkSetup },
60
+ } = options;
61
+
62
+ // Called here, not in globalSetup, so CLERK_TESTING_TOKEN is not
63
+ // inherited by the web server, where it would authenticate every request.
64
+ await clerkTesting.clerkSetup();
65
+
66
+ await page.goto(signInPath);
67
+ await clerkTesting.clerk.signIn({ page, emailAddress });
68
+
69
+ // Compare the pathname so a signed-out redirect such as
70
+ // /login?redirect=/dashboard does not count as signed in.
71
+ await page.goto(protectedPath);
72
+ const landedPathname = new URL(page.url()).pathname;
73
+ if (landedPathname !== protectedPath) {
74
+ throw new Error(
75
+ `Clerk sign-in failed: expected ${protectedPath}, landed on ${landedPathname}.`,
76
+ );
77
+ }
78
+
79
+ await page.context().storageState({ path: AUTH_STATE_FILE });
80
+ }
81
+
82
+ /** @param {import("./clerk.js").ClerkAuthSetupOptions} options */
83
+ export function defineClerkAuthSetup(options) {
84
+ setup("authenticate", ({ page }) => authenticate({ page, ...options }));
85
+ }
package/consent.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ export interface ConsentStorageState {
2
+ cookies: [];
3
+ origins: {
4
+ origin: string;
5
+ localStorage: { name: string; value: string }[];
6
+ }[];
7
+ }
8
+
9
+ export interface ConsentDeclinedStorageStateOptions {
10
+ /** The `app` name the app passes to `createConsent`. It names the storage key. */
11
+ app: string;
12
+ /** Origin of the app under test, such as `http://localhost:3000`. */
13
+ origin: string;
14
+ /** Epoch milliseconds the choice is made at. Defaults to the current time. */
15
+ now?: number;
16
+ }
17
+
18
+ export declare function consentDeclinedStorageState(
19
+ options: ConsentDeclinedStorageStateOptions,
20
+ ): ConsentStorageState;
21
+
22
+ export declare function consentUnsetStorageState(): ConsentStorageState;
package/consent.js ADDED
@@ -0,0 +1,89 @@
1
+ import { URL } from "node:url";
2
+ import { CONSENT_LIFETIME_MS, consentStorageKey } from "@neon-pixels/consent";
3
+
4
+ const PAGE_PROTOCOLS = new Set(["http:", "https:"]);
5
+
6
+ /** @param {string} origin */
7
+ function invalidOriginError(origin) {
8
+ return new Error(
9
+ `@neon-pixels/playwright-preset: "origin" must be an http(s) URL such as "http://localhost:3000", received ${JSON.stringify(origin)}.`,
10
+ );
11
+ }
12
+
13
+ /** @param {string} origin */
14
+ function parseUrl(origin) {
15
+ try {
16
+ return new URL(origin);
17
+ } catch {
18
+ throw invalidOriginError(origin);
19
+ }
20
+ }
21
+
22
+ /** @param {string} origin */
23
+ function normalizeOrigin(origin) {
24
+ const url = parseUrl(origin);
25
+ // A value without a scheme, such as "localhost:3000", parses with an opaque origin and a non-page protocol.
26
+ if (!PAGE_PROTOCOLS.has(url.protocol)) {
27
+ throw invalidOriginError(origin);
28
+ }
29
+ return url.origin;
30
+ }
31
+
32
+ /** @param {string} app */
33
+ function assertValidApp(app) {
34
+ if (typeof app === "string" && app.trim() !== "") {
35
+ return;
36
+ }
37
+ throw new Error(
38
+ '@neon-pixels/playwright-preset: "app" must be a non-empty string.',
39
+ );
40
+ }
41
+
42
+ /** @param {number} now */
43
+ function assertValidNow(now) {
44
+ if (Number.isFinite(now)) {
45
+ return;
46
+ }
47
+ throw new Error(
48
+ '@neon-pixels/playwright-preset: "now" must be a finite number of epoch milliseconds.',
49
+ );
50
+ }
51
+
52
+ /**
53
+ * Browser storage with the analytics choice already made as declined, so the
54
+ * consent banner stays hidden. Assign it to `use.storageState`.
55
+ *
56
+ * @param {import("./consent.js").ConsentDeclinedStorageStateOptions} options
57
+ * @returns {import("./consent.js").ConsentStorageState}
58
+ */
59
+ export function consentDeclinedStorageState(options = {}) {
60
+ const { app, origin, now = Date.now() } = options;
61
+ assertValidApp(app);
62
+ assertValidNow(now);
63
+ const normalizedOrigin = normalizeOrigin(origin);
64
+ /** @type {import("@neon-pixels/consent").StoredConsent} */
65
+ const record = { choice: "denied", expiresAt: now + CONSENT_LIFETIME_MS };
66
+
67
+ return {
68
+ cookies: [],
69
+ origins: [
70
+ {
71
+ origin: normalizedOrigin,
72
+ localStorage: [
73
+ { name: consentStorageKey(app), value: JSON.stringify(record) },
74
+ ],
75
+ },
76
+ ],
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Browser storage with no consent choice stored, for specs that exercise the
82
+ * banner itself. Use it with `test.use({ storageState: ... })` to override a
83
+ * project-wide declined state.
84
+ *
85
+ * @returns {import("./consent.js").ConsentStorageState}
86
+ */
87
+ export function consentUnsetStorageState() {
88
+ return { cookies: [], origins: [] };
89
+ }
package/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { PlaywrightTestConfig } from "@playwright/test";
2
+
3
+ export interface PlaywrightPresetOptions {
4
+ /** Shell command that starts the app under test. */
5
+ command: string;
6
+ /** Port the app listens on. There is no default; every repo passes its own. */
7
+ port: number;
8
+ /** Environment variables for the web server process. */
9
+ env?: Record<string, string>;
10
+ }
11
+
12
+ export declare function createPlaywrightConfig(
13
+ options: PlaywrightPresetOptions,
14
+ ): PlaywrightTestConfig;
package/index.js ADDED
@@ -0,0 +1,58 @@
1
+ import process from "node:process";
2
+ import { devices } from "@playwright/test";
3
+
4
+ const CI_RETRIES = 2;
5
+ const LOCAL_RETRIES = 0;
6
+ const WEB_SERVER_TIMEOUT_MS = 60_000;
7
+ const MAX_PORT = 65_535;
8
+
9
+ /** @param {number} port */
10
+ function assertValidPort(port) {
11
+ if (Number.isInteger(port) && port > 0 && port <= MAX_PORT) {
12
+ return;
13
+ }
14
+ throw new Error(
15
+ `@neon-pixels/playwright-preset: "port" must be an integer from 1 to ${MAX_PORT}, received ${String(port)}.`,
16
+ );
17
+ }
18
+
19
+ /** @param {string} command */
20
+ function assertValidCommand(command) {
21
+ if (typeof command === "string" && command.trim() !== "") {
22
+ return;
23
+ }
24
+ throw new Error(
25
+ '@neon-pixels/playwright-preset: "command" must be a non-empty string.',
26
+ );
27
+ }
28
+
29
+ /**
30
+ * @param {import("./index.js").PlaywrightPresetOptions} options
31
+ * @returns {import("@playwright/test").PlaywrightTestConfig}
32
+ */
33
+ export function createPlaywrightConfig(options) {
34
+ const { command, port, env } = options;
35
+ assertValidCommand(command);
36
+ assertValidPort(port);
37
+
38
+ const isCi = Boolean(process.env.CI);
39
+ const baseURL = `http://localhost:${port}`;
40
+
41
+ return {
42
+ forbidOnly: isCi,
43
+ retries: isCi ? CI_RETRIES : LOCAL_RETRIES,
44
+ reporter: [isCi ? ["github"] : ["list"], ["html", { open: "never" }]],
45
+ use: {
46
+ baseURL,
47
+ trace: "on-first-retry",
48
+ },
49
+ projects: [{ name: "chromium", use: { ...devices["Desktop Chrome"] } }],
50
+ webServer: {
51
+ command,
52
+ url: baseURL,
53
+ reuseExistingServer: !isCi,
54
+ timeout: WEB_SERVER_TIMEOUT_MS,
55
+ ...(env ? { env } : {}),
56
+ },
57
+ };
58
+ }
package/package.json CHANGED
@@ -1 +1,62 @@
1
- {"name":"@neon-pixels/playwright-preset","version":"0.0.0","description":"Placeholder. The first real release follows.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/neonpixels-studio/packages.git"}}
1
+ {
2
+ "name": "@neon-pixels/playwright-preset",
3
+ "version": "0.3.0",
4
+ "description": "Shared Playwright configuration for Neon Pixels projects.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/neonpixels-studio/packages.git",
10
+ "directory": "packages/playwright-preset"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./index.d.ts",
15
+ "default": "./index.js"
16
+ },
17
+ "./clerk": {
18
+ "types": "./clerk.d.ts",
19
+ "default": "./clerk.js"
20
+ },
21
+ "./consent": {
22
+ "types": "./consent.d.ts",
23
+ "default": "./consent.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "files": [
28
+ "index.js",
29
+ "index.d.ts",
30
+ "clerk.js",
31
+ "clerk.d.ts",
32
+ "consent.js",
33
+ "consent.d.ts"
34
+ ],
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "engines": {
39
+ "node": "^22.17.0 || ^24.0.0 || >=26.0.0"
40
+ },
41
+ "peerDependencies": {
42
+ "@clerk/testing": "^2.0.0",
43
+ "@neon-pixels/consent": "^0.3.0",
44
+ "@playwright/test": "^1.50.0"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "@clerk/testing": {
48
+ "optional": true
49
+ },
50
+ "@neon-pixels/consent": {
51
+ "optional": true
52
+ }
53
+ },
54
+ "devDependencies": {
55
+ "@clerk/testing": "^2.3.6",
56
+ "@neon-pixels/consent": "0.3.0",
57
+ "@playwright/test": "^1.65.1"
58
+ },
59
+ "scripts": {
60
+ "typecheck": "tsc --noEmit -p tsconfig.json"
61
+ }
62
+ }