@neon-pixels/playwright-preset 0.2.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/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
 
@@ -73,6 +78,53 @@ Building the config also copies `NUXT_CLERK_SECRET_KEY` and `NUXT_PUBLIC_CLERK_P
73
78
 
74
79
  To change anything, override it in your own config: spread the result and replace the key.
75
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
+
76
128
  ## License
77
129
 
78
130
  MIT
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neon-pixels/playwright-preset",
3
- "version": "0.2.0",
3
+ "version": "0.3.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.3.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.3.0",
46
57
  "@playwright/test": "^1.65.1"
47
58
  },
48
59
  "scripts": {