From 88798f0ac425174b70398e86107d863c9aaf0a48 Mon Sep 17 00:00:00 2001 From: Nguyen Huu Nguyen Y Date: Mon, 29 Jun 2026 22:21:44 +0700 Subject: [PATCH] fix(test): unblock nuxt-env setupNuxt hang via session fixture The @thecodeorigin/auth module ships a global route middleware that redirects unauthenticated users to an external sign-in URL. During `setupNuxt()` the app's boot navigation (`/` -> `/dashboard`) triggered that external redirect, which never resolves in the @nuxt/test-utils runtime env and hung the beforeAll hook for the full timeout. Every nuxt-env test file (cookie-consent, sidebar-search, notifications-bell, support-modal) timed out at 120s. Register a logged-in `/api/_auth/session` fixture for the nuxt project so the auth boot plugin resolves to an authenticated admin and the middleware lets navigation through. The app now boots in ~14s. Guarded so files that opt into `happy-dom` (no test-utils runtime app) skip registration. Co-Authored-By: Claude Opus 4.8 --- test/setup/nuxt-auth-session.ts | 28 ++++++++++++++++++++++++++++ vitest.config.ts | 1 + 2 files changed, 29 insertions(+) create mode 100644 test/setup/nuxt-auth-session.ts diff --git a/test/setup/nuxt-auth-session.ts b/test/setup/nuxt-auth-session.ts new file mode 100644 index 00000000..35502798 --- /dev/null +++ b/test/setup/nuxt-auth-session.ts @@ -0,0 +1,28 @@ +import { registerEndpoint } from '@nuxt/test-utils/runtime' + +// The @thecodeorigin/auth module ships an async boot plugin (`0.session`) that +// fetches `/api/_auth/session`, plus a global route middleware that redirects +// unauthenticated users to the (external) sign-in URL. During `setupNuxt()` the +// app's initial navigation (`/` → `/dashboard`) would trigger that external +// redirect, which never resolves in the test env and hangs the setup hook. +// +// Registering a logged-in session here makes the boot plugin resolve to an +// authenticated admin, so the middleware lets navigation through and the app +// boots cleanly. `systemRole: 'admin'` grants `manage all` via the ability +// plugin, so ability-gated components/pages render in tests. +// +// Some files in the nuxt project opt out via `// @vitest-environment happy-dom`, +// where the test-utils runtime app (`window.__app`) is absent — skip there. +if (typeof window !== 'undefined' && '__app' in window) { + registerEndpoint('/api/_auth/session', () => ({ + user: { + id: 'test-user', + email: 'test@example.com', + name: 'Test User', + }, + systemRole: 'admin', + abilities: [], + impersonator: null, + organizations: [], + })) +} diff --git a/vitest.config.ts b/vitest.config.ts index 775ec1c3..8fbf821a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -63,6 +63,7 @@ export default defineConfig({ }, }, hookTimeout: 120000, + setupFiles: ['./test/setup/nuxt-auth-session.ts'], }, }), ],