@neon-pixels/playwright-preset 0.2.0 → 0.4.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/README.md CHANGED
@@ -14,14 +14,19 @@ Playwright is a peer dependency, so install it yourself. For the Clerk entry poi
14
14
  pnpm add -D @clerk/testing
15
15
  ```
16
16
 
17
- Only repos that import the `/clerk` entry point need `@clerk/testing`.
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
+ ```
18
22
 
19
23
  ## Usage
20
24
 
21
- | Entry point | What it does |
22
- | -------------------------------------- | ----------------------------------------------------------------------------- |
23
- | `@neon-pixels/playwright-preset` | Base config: reporter, CI retries, trace on first retry, Chromium, web server |
24
- | `@neon-pixels/playwright-preset/clerk` | Base config plus a Clerk sign-in `setup` project that saves the browser state |
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 |
25
30
 
26
31
  ### Base
27
32
 
@@ -40,6 +45,32 @@ export default defineConfig({
40
45
 
41
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.
42
47
 
48
+ ### Overriding the web server and base URL
49
+
50
+ Both `createPlaywrightConfig` and `createClerkPlaywrightConfig` return a config where `webServer` is a single object and `use` is always defined, so you can spread either one to change a single key without restating the rest:
51
+
52
+ ```ts
53
+ const base = createPlaywrightConfig({
54
+ command: "pnpm dev --port 3000",
55
+ port: 3000,
56
+ });
57
+
58
+ export default defineConfig({
59
+ ...base,
60
+ webServer: { ...base.webServer, reuseExistingServer: false },
61
+ });
62
+ ```
63
+
64
+ The web server `url` and `baseURL` are always `http://localhost:<port>`. If your server binds only `127.0.0.1` and does not answer on `localhost`, override both so Playwright waits on, and browses to, the same address:
65
+
66
+ ```ts
67
+ export default defineConfig({
68
+ ...base,
69
+ use: { ...base.use, baseURL: "http://127.0.0.1:3000" },
70
+ webServer: { ...base.webServer, url: "http://127.0.0.1:3000" },
71
+ });
72
+ ```
73
+
43
74
  ### Clerk
44
75
 
45
76
  ```ts
@@ -71,7 +102,54 @@ The config adds a `setup` project that runs any file ending in `auth.setup.ts`.
71
102
 
72
103
  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.
73
104
 
74
- To change anything, override it in your own config: spread the result and replace the key.
105
+ To change anything, override it in your own config: spread the result and replace the key. See [Overriding the web server and base URL](#overriding-the-web-server-and-base-url).
106
+
107
+ ### Consent
108
+
109
+ 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).
110
+
111
+ ```ts
112
+ // playwright.config.ts
113
+ import { defineConfig } from "@playwright/test";
114
+ import { createPlaywrightConfig } from "@neon-pixels/playwright-preset";
115
+ import { consentDeclinedStorageState } from "@neon-pixels/playwright-preset/consent";
116
+
117
+ const base = createPlaywrightConfig({
118
+ command: "pnpm dev --port 3000",
119
+ port: 3000,
120
+ });
121
+
122
+ export default defineConfig({
123
+ ...base,
124
+ use: {
125
+ ...base.use,
126
+ storageState: consentDeclinedStorageState({
127
+ app: "my-site", // the `app` you pass to createConsent
128
+ origin: "http://localhost:3000", // only the origin of the URL is used
129
+ }),
130
+ },
131
+ testDir: "./e2e",
132
+ });
133
+ ```
134
+
135
+ `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.
136
+
137
+ Specs about consent itself start with no stored choice:
138
+
139
+ ```ts
140
+ import { test } from "@playwright/test";
141
+ import { consentUnsetStorageState } from "@neon-pixels/playwright-preset/consent";
142
+
143
+ test.describe("consent banner", () => {
144
+ test.use({ storageState: consentUnsetStorageState() });
145
+
146
+ test("shows on a first visit", async ({ page }) => {
147
+ // ...
148
+ });
149
+ });
150
+ ```
151
+
152
+ A project has one `storageState`, so this replaces a saved sign-in file rather than merging with it.
75
153
 
76
154
  ## License
77
155
 
package/clerk.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- import type { Page, PlaywrightTestConfig } from "@playwright/test";
2
- import type { PlaywrightPresetOptions } from "./index.js";
1
+ import type { Page } from "@playwright/test";
2
+ import type {
3
+ PlaywrightPresetConfig,
4
+ PlaywrightPresetOptions,
5
+ } from "./index.js";
3
6
 
4
7
  /** Where the setup project writes the signed-in browser state. */
5
8
  export declare const AUTH_STATE_FILE: string;
@@ -38,7 +41,7 @@ export declare function applyClerkEnvironment(
38
41
 
39
42
  export declare function createClerkPlaywrightConfig(
40
43
  options: PlaywrightPresetOptions,
41
- ): PlaywrightTestConfig;
44
+ ): PlaywrightPresetConfig;
42
45
 
43
46
  /** Registers the "authenticate" setup test. Call it from `e2e/auth.setup.ts`. */
44
47
  export declare function defineClerkAuthSetup(
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 CHANGED
@@ -9,6 +9,24 @@ export interface PlaywrightPresetOptions {
9
9
  env?: Record<string, string>;
10
10
  }
11
11
 
12
+ /** A single web server config object (never the array form). */
13
+ export type PlaywrightPresetWebServer = Exclude<
14
+ NonNullable<PlaywrightTestConfig["webServer"]>,
15
+ unknown[]
16
+ >;
17
+
18
+ /**
19
+ * The preset's config. `webServer` is one object and `use` is defined, so
20
+ * `{ ...base.webServer, reuseExistingServer: false }` typechecks.
21
+ */
22
+ export type PlaywrightPresetConfig = Omit<
23
+ PlaywrightTestConfig,
24
+ "use" | "webServer"
25
+ > & {
26
+ use: NonNullable<PlaywrightTestConfig["use"]>;
27
+ webServer: PlaywrightPresetWebServer;
28
+ };
29
+
12
30
  export declare function createPlaywrightConfig(
13
31
  options: PlaywrightPresetOptions,
14
- ): PlaywrightTestConfig;
32
+ ): PlaywrightPresetConfig;
package/index.js CHANGED
@@ -28,7 +28,7 @@ function assertValidCommand(command) {
28
28
 
29
29
  /**
30
30
  * @param {import("./index.js").PlaywrightPresetOptions} options
31
- * @returns {import("@playwright/test").PlaywrightTestConfig}
31
+ * @returns {import("./index.js").PlaywrightPresetConfig}
32
32
  */
33
33
  export function createPlaywrightConfig(options) {
34
34
  const { command, port, env } = options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neon-pixels/playwright-preset",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Shared Playwright configuration for Neon Pixels projects.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -18,13 +18,19 @@
18
18
  "types": "./clerk.d.ts",
19
19
  "default": "./clerk.js"
20
20
  },
21
+ "./consent": {
22
+ "types": "./consent.d.ts",
23
+ "default": "./consent.js"
24
+ },
21
25
  "./package.json": "./package.json"
22
26
  },
23
27
  "files": [
24
28
  "index.js",
25
29
  "index.d.ts",
26
30
  "clerk.js",
27
- "clerk.d.ts"
31
+ "clerk.d.ts",
32
+ "consent.js",
33
+ "consent.d.ts"
28
34
  ],
29
35
  "publishConfig": {
30
36
  "access": "public"
@@ -34,15 +40,20 @@
34
40
  },
35
41
  "peerDependencies": {
36
42
  "@clerk/testing": "^2.0.0",
43
+ "@neon-pixels/consent": "^0.4.0",
37
44
  "@playwright/test": "^1.50.0"
38
45
  },
39
46
  "peerDependenciesMeta": {
40
47
  "@clerk/testing": {
41
48
  "optional": true
49
+ },
50
+ "@neon-pixels/consent": {
51
+ "optional": true
42
52
  }
43
53
  },
44
54
  "devDependencies": {
45
55
  "@clerk/testing": "^2.3.6",
56
+ "@neon-pixels/consent": "0.4.0",
46
57
  "@playwright/test": "^1.65.1"
47
58
  },
48
59
  "scripts": {