@neon-pixels/playwright-preset 0.0.0 → 0.2.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 +21 -0
- package/README.md +78 -0
- package/clerk.d.ts +46 -0
- package/clerk.js +85 -0
- package/index.d.ts +14 -0
- package/index.js +58 -0
- package/package.json +51 -1
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,78 @@
|
|
|
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`.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
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
|
+
|
|
26
|
+
### Base
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// playwright.config.ts
|
|
30
|
+
import { defineConfig } from "@playwright/test";
|
|
31
|
+
import { createPlaywrightConfig } from "@neon-pixels/playwright-preset";
|
|
32
|
+
|
|
33
|
+
export default defineConfig({
|
|
34
|
+
...createPlaywrightConfig({ command: "pnpm dev --port 3000", port: 3000 }),
|
|
35
|
+
testDir: "./e2e",
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`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.
|
|
40
|
+
|
|
41
|
+
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
|
+
|
|
43
|
+
### Clerk
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
// playwright.config.ts
|
|
47
|
+
import { defineConfig } from "@playwright/test";
|
|
48
|
+
import { createClerkPlaywrightConfig } from "@neon-pixels/playwright-preset/clerk";
|
|
49
|
+
|
|
50
|
+
export default defineConfig({
|
|
51
|
+
...createClerkPlaywrightConfig({
|
|
52
|
+
command: "pnpm dev --port 3000",
|
|
53
|
+
port: 3000,
|
|
54
|
+
}),
|
|
55
|
+
testDir: "./e2e",
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// e2e/auth.setup.ts
|
|
61
|
+
import { defineClerkAuthSetup } from "@neon-pixels/playwright-preset/clerk";
|
|
62
|
+
|
|
63
|
+
defineClerkAuthSetup({
|
|
64
|
+
emailAddress: "e2e-test@example.com",
|
|
65
|
+
signInPath: "/login",
|
|
66
|
+
protectedPath: "/dashboard",
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
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.
|
|
71
|
+
|
|
72
|
+
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
|
+
|
|
74
|
+
To change anything, override it in your own config: spread the result and replace the key.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
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/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,51 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@neon-pixels/playwright-preset",
|
|
3
|
+
"version": "0.2.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
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"index.js",
|
|
25
|
+
"index.d.ts",
|
|
26
|
+
"clerk.js",
|
|
27
|
+
"clerk.d.ts"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": "^22.17.0 || ^24.0.0 || >=26.0.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@clerk/testing": "^2.0.0",
|
|
37
|
+
"@playwright/test": "^1.50.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"@clerk/testing": {
|
|
41
|
+
"optional": true
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@clerk/testing": "^2.3.6",
|
|
46
|
+
"@playwright/test": "^1.65.1"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
50
|
+
}
|
|
51
|
+
}
|