Skip to content

Commit 9f1a020

Browse files
angeloashmoreclaude
andcommitted
test: add E2E tests for login, logout, whoami, and init commands
Introduces a real E2E test infrastructure that authenticates against Prismic, creates a temporary repository per test run, and exercises the CLI binary directly. Adds --no-browser flag for headless login flows and a MODE=test build mode to disable telemetry during tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 077b5ac commit 9f1a020

24 files changed

Lines changed: 477 additions & 64 deletions

.env.test.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
###############################################################################
2+
# The following environment variables are used to run tests.
3+
# Create a specific account for testing to avoid issues.
4+
###############################################################################
5+
6+
# The email address for your Prismic account.
7+
E2E_PRISMIC_EMAIL=
8+
# The password to your Prismic account.
9+
E2E_PRISMIC_PASSWORD=

.github/workflows/prerelease-canary.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
publish:
1414
if: github.repository_owner == 'prismicio'
1515
runs-on: ubuntu-latest
16+
env:
17+
MODE: production
1618
steps:
1719
- uses: actions/checkout@v6
1820
- uses: actions/setup-node@v6

.github/workflows/prerelease-pr.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ jobs:
1111
publish:
1212
if: github.repository_owner == 'prismicio'
1313
runs-on: ubuntu-latest
14+
env:
15+
MODE: production
1416
steps:
1517
- uses: actions/checkout@v6
1618
- uses: actions/setup-node@v6

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ on:
1414
jobs:
1515
release-please:
1616
runs-on: ubuntu-latest
17+
env:
18+
MODE: production
1719
steps:
1820
- uses: googleapis/release-please-action@v4
1921
id: release

.github/workflows/validate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616

1717
build:
1818
runs-on: ubuntu-latest
19+
env:
20+
MODE: production
1921
steps:
2022
- uses: actions/checkout@v6
2123
- uses: actions/setup-node@v6

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
"prepare": "npm run build",
3131
"lint": "oxlint --deny-warnings",
3232
"types": "tsc --noEmit",
33-
"unit": "vitest run --coverage",
33+
"unit": "MODE=test tsdown --logLevel=silent && vitest run --coverage",
3434
"unit:watch": "vitest watch",
35-
"test": "npm run lint && npm run types && npm run unit && npm run build"
35+
"test": "npm run lint && npm run types && npm run unit"
3636
},
3737
"devDependencies": {
3838
"@prismicio/types-internal": "3.16.1",
@@ -46,20 +46,21 @@
4646
"oxfmt": "^0.24.0",
4747
"oxlint": "1.39.0",
4848
"prismic-ts-codegen": "^0.1.28",
49+
"tinyexec": "^1.0.2",
4950
"tinyglobby": "^0.2.15",
5051
"tsdown": "0.19.0",
5152
"typescript": "5.9.3",
5253
"vitest": "4.0.17",
5354
"zod": "^4.3.6"
5455
},
55-
"engines": {
56-
"node": ">=20"
57-
},
5856
"devEngines": {
5957
"runtime": {
6058
"name": "node",
6159
"version": ">=24",
6260
"onFail": "warn"
6361
}
62+
},
63+
"engines": {
64+
"node": ">=20"
6465
}
6566
}

src/commands/init.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ USAGE
2828
2929
FLAGS
3030
-r, --repo string Repository name
31+
--no-browser Skip opening the browser automatically during login
3132
-h, --help Show help for command
3233
3334
EXAMPLES
@@ -43,6 +44,7 @@ export async function init(): Promise<void> {
4344
options: {
4445
help: { type: "boolean", short: "h" },
4546
repo: { type: "string", short: "r" },
47+
"no-browser": { type: "boolean" },
4648
},
4749
});
4850

@@ -87,9 +89,13 @@ export async function init(): Promise<void> {
8789
console.info("Not logged in. Starting login...");
8890
const { email } = await createLoginSession({
8991
onReady: (url) => {
90-
console.info("Opening browser to complete login...");
91-
console.info(`If the browser doesn't open, visit: ${url}`);
92-
openBrowser(url);
92+
if (values["no-browser"]) {
93+
console.info(`Open this URL to log in: ${url}`);
94+
} else {
95+
console.info("Opening browser to complete login...");
96+
console.info(`If the browser doesn't open, visit: ${url}`);
97+
openBrowser(url);
98+
}
9399
},
94100
});
95101
console.info(`Logged in as ${email}`);

src/commands/login.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ USAGE
1010
prismic login [flags]
1111
1212
FLAGS
13-
-h, --help Show help for command
13+
--no-browser Skip opening the browser automatically
14+
-h, --help Show help for command
1415
1516
LEARN MORE
1617
Use \`prismic <command> --help\` for more information about a command.
@@ -19,7 +20,10 @@ LEARN MORE
1920
export async function login(): Promise<void> {
2021
const { values } = parseArgs({
2122
args: process.argv.slice(3),
22-
options: { help: { type: "boolean", short: "h" } },
23+
options: {
24+
help: { type: "boolean", short: "h" },
25+
"no-browser": { type: "boolean" },
26+
},
2327
});
2428

2529
if (values.help) {
@@ -29,9 +33,13 @@ export async function login(): Promise<void> {
2933

3034
const { email } = await createLoginSession({
3135
onReady: (url) => {
32-
console.info("Opening browser to complete login...");
33-
console.info(`If the browser doesn't open, visit: ${url}`);
34-
openBrowser(url);
36+
if (values["no-browser"]) {
37+
console.info(`Open this URL to log in: ${url}`);
38+
} else {
39+
console.info("Opening browser to complete login...");
40+
console.info(`If the browser doesn't open, visit: ${url}`);
41+
openBrowser(url);
42+
}
3543
},
3644
});
3745

src/env.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ const Env = z.object({
77
MODE: z.string(),
88
DEV: z.stringbool(),
99
PROD: z.stringbool(),
10+
TEST: z.stringbool(),
1011
PRISMIC_SENTRY_DSN: z._default(z.httpUrl(), DEFAULT_PRISMIC_SENTRY_DSN),
1112
PRISMIC_SENTRY_ENVIRONMENT: z.optional(z.string()),
1213
PRISMIC_SENTRY_ENABLED: z.optional(z.stringbool()),
1314
PRISMIC_HOST: z.optional(z.string()),
1415
});
1516

1617
export const env = z.parse(Env, {
17-
MODE: process.env.MODE,
18-
DEV: JSON.stringify(process.env.MODE !== "production"),
19-
PROD: JSON.stringify(process.env.MODE === "production"),
2018
...process.env,
19+
MODE: process.env.MODE,
20+
DEV: process.env.DEV,
21+
PROD: process.env.PROD,
22+
TEST: process.env.TEST,
2123
});

0 commit comments

Comments
 (0)