Skip to content

Commit d896e15

Browse files
committed
test(device): load BrowserStack config from the root .env file
Running the device suite meant exporting BROWSERSTACK_* by hand each time. The config now loads the repo root's `.env` (gitignored; the entries are documented in `.env.sample`, the repo's one sample file) — dotenv parsing accepts its shell-style `export KEY=value` lines, so the same file keeps working for `source`. Real environment variables take precedence, so CI is unaffected, and the missing-credentials error points at the file.
1 parent 1f18e6c commit d896e15

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

.env.sample

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
export NX_SELF_HOSTED_REMOTE_CACHE_SERVER=https://cache.nickthesick.com
2-
export NX_SELF_HOSTED_REMOTE_CACHE_ACCESS_TOKEN=g8@ucL8em4*Z9TKXDY9OEX@!upf^Nz9
2+
export NX_SELF_HOSTED_REMOTE_CACHE_ACCESS_TOKEN=g8@ucL8em4*Z9TKXDY9OEX@!upf^Nz9
3+
4+
# BrowserStack credentials for the real-device suite (`pnpm run test:device`).
5+
# From browserstack.com/accounts/profile. Without them the suite skips itself.
6+
export BROWSERSTACK_USERNAME=
7+
export BROWSERSTACK_ACCESS_KEY=
8+
9+
# Optional, also for the device suite: where the devices load the app from
10+
# (default: the local dev server started with `pnpm run dev`), and a device
11+
# subset — substring of an id in tests/device/devices.ts.
12+
# export DEVICE_TEST_TARGET=http://127.0.0.1:5173
13+
# export DEVICE_FILTER=android

tests/device/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pnpm run dev
2121
BROWSERSTACK_USERNAME=... BROWSERSTACK_ACCESS_KEY=... pnpm run test:device
2222
```
2323

24+
Instead of exporting the variables each time, copy the repo root's
25+
`.env.sample` to `.env` (gitignored) and fill in the BrowserStack entries —
26+
the config loads it, with real environment variables taking precedence.
27+
2428
Environment knobs:
2529

2630
| Variable | Purpose |

tests/device/lib/webdriver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class DeviceSession {
3636
const auth = browserStackCredentials();
3737
if (!auth) {
3838
throw new Error(
39-
"BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY must be set",
39+
"BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY must be set " +
40+
"(exported, or in the repo root .env — see .env.sample)",
4041
);
4142
}
4243
// Device allocation occasionally hiccups; one retry absorbs it.

tests/device/vitest.config.mts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
import { defineConfig } from "vite-plus";
1+
import path from "node:path";
2+
import { defineConfig, loadEnv } from "vite-plus";
3+
4+
// The suite reads its configuration from the environment; the repo root's
5+
// `.env` (copied from `.env.sample`) works too. Loaded here because vitest
6+
// does not load env files into `process.env` on its own — dotenv parsing
7+
// accepts the sample's shell-style `export KEY=value` lines. Real environment
8+
// variables win over the file.
9+
const fileEnv = loadEnv("", path.resolve(import.meta.dirname, "../.."), "");
10+
for (const key of [
11+
"BROWSERSTACK_USERNAME",
12+
"BROWSERSTACK_ACCESS_KEY",
13+
"DEVICE_TEST_TARGET",
14+
"DEVICE_FILTER",
15+
]) {
16+
if (process.env[key] === undefined && fileEnv[key] !== undefined) {
17+
process.env[key] = fileEnv[key];
18+
}
19+
}
220

321
/**
422
* Real-device suite (BrowserStack). Not part of the workspace projects on

0 commit comments

Comments
 (0)