-
Notifications
You must be signed in to change notification settings - Fork 0
[R1] fix(test): create Better Auth tables in test setup for fresh DBs #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,31 @@ | |
| */ | ||
| import "reflect-metadata"; | ||
| import { AppDataSource } from "./data-source.js"; | ||
| import { AddBetterAuthTables1775574200000 } from "./migrations/1775574200000-AddBetterAuthTables.js"; | ||
| import { beforeAll, afterAll } from "vitest"; | ||
|
|
||
| beforeAll(async () => { | ||
| if (!AppDataSource.isInitialized) { | ||
| await AppDataSource.initialize(); | ||
|
|
||
| // synchronize creates schema for entities listed in data-source.ts but | ||
| // does NOT create migration-managed tables. Better Auth's session, | ||
| // account, and verification tables are created via a migration (they're | ||
| // not TypeORM entities). On a fresh DB (e.g., CI), those tables don't | ||
| // exist — any test path that touches Better Auth (admin invite flows, | ||
| // session writes) throws "relation does not exist", which can cascade | ||
| // into unhandled rejections that crash the vitest worker. | ||
| const qr = AppDataSource.createQueryRunner(); | ||
| try { | ||
| const exists = await qr.query( | ||
| `SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'verification' LIMIT 1` | ||
| ); | ||
| if (exists.length === 0) { | ||
| await new AddBetterAuthTables1775574200000().up(qr); | ||
| } | ||
| } finally { | ||
| await qr.release(); | ||
| } | ||
|
Comment on lines
+23
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This manual migration check is susceptible to race conditions when running tests in parallel (Vitest's default mode). Multiple worker processes may simultaneously detect the missing table and attempt to run the migration, leading to |
||
| } | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of executing raw SQL against
information_schema, use the built-inqueryRunner.hasTable()method. It is more idiomatic for TypeORM and automatically handles schema and dialect specifics.