Expose E2E store via build mode

This commit is contained in:
Jinjing 2026-06-10 17:23:01 -07:00
parent 855b2fb09c
commit 215094ea14
4 changed files with 14 additions and 9 deletions

View File

@ -1,3 +0,0 @@
# Why: enables window.__store in the renderer build so E2E tests can read
# Zustand state directly instead of fragile DOM scraping.
VITE_EXPOSE_STORE=true

View File

@ -2,15 +2,23 @@ import { createE2EConfig } from '../shared/e2e-config'
const preloadEnv = (
import.meta as ImportMeta & {
env?: { VITE_EXPOSE_STORE?: boolean }
env?: { MODE?: string; VITE_EXPOSE_STORE?: boolean | string }
}
).env
function isEnvFlagEnabled(value: boolean | string | undefined): boolean {
return value === true || value === 'true'
}
// Why: `--mode e2e` must be enough for manual rebuilds used with SKIP_BUILD=1;
// keeping this out of a root .env file makes the test-only toggle less visible.
const exposeStore = preloadEnv?.MODE === 'e2e' || isEnvFlagEnabled(preloadEnv?.VITE_EXPOSE_STORE)
// Why: preload is the renderer's audited bridge into Electron startup state.
// Renderer code should consume a typed config object from this bridge instead
// of reading test-only env vars directly.
export const preloadE2EConfig = createE2EConfig({
headless: process.env.ORCA_E2E_HEADLESS === '1',
exposeStore: preloadEnv?.VITE_EXPOSE_STORE,
exposeStore,
userDataDir: process.env.ORCA_E2E_USER_DATA_DIR ?? null
})

View File

@ -2,7 +2,7 @@
## Build the App With `--mode e2e` Before Running Tests
E2E tests read Zustand state via `window.__store`. That global is only assigned when the renderer is built with `VITE_EXPOSE_STORE=true`, which is set by `.env.e2e` and only applied when you pass `--mode e2e` to `electron-vite build`. A plain `pnpm build` or `pnpm build:electron-vite` produces an `out/` tree **without** the store exposed, so reusing it with `SKIP_BUILD=1` makes every spec hang on `waitForFunction(() => Boolean(window.__store))` and time out at 30s.
E2E tests read Zustand state via `window.__store`. That global is only assigned when the preload bundle is built in `e2e` mode, which is applied when you pass `--mode e2e` to `electron-vite build`. A plain `pnpm build` or `pnpm build:electron-vite` produces an `out/` tree **without** the store exposed, so reusing it with `SKIP_BUILD=1` makes every spec hang on `waitForFunction(() => Boolean(window.__store))` and time out at 30s.
- Default path: `pnpm run test:e2e``globalSetup` runs `electron-vite build --mode e2e` for you.
- Fast iteration: `pnpm exec electron-vite build --mode e2e` once, then `SKIP_BUILD=1 pnpm run test:e2e …`.

View File

@ -28,11 +28,11 @@ export default function globalSetup(): void {
if (process.env.SKIP_BUILD && existsSync(outMain)) {
console.log('[e2e] SKIP_BUILD set and out/main/index.js exists — skipping build')
} else {
// Why: --mode e2e loads .env.e2e which sets VITE_EXPOSE_STORE=true. This
// makes window.__store available in the renderer build so tests can read
// Zustand state directly instead of fragile DOM scraping.
// Why: --mode e2e is the build-time signal that exposes window.__store;
// the explicit env var keeps older local overrides working too.
console.log('[e2e] Building Electron app with electron-vite build --mode e2e...')
execSync('npx electron-vite build --mode e2e', {
env: { ...process.env, VITE_EXPOSE_STORE: 'true' },
cwd: root,
stdio: 'inherit',
// Why: Windows renderer builds can exceed 120s on local/CI hosts even