-
Notifications
You must be signed in to change notification settings - Fork 15
use set up and tear down test db during backend tests #3038
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
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const base = require('./jest.config'); | ||
|
|
||
| module.exports = { | ||
| ...base, | ||
| testMatch: ['**/test/integration/**/*.test.[jt]s?(x)'], | ||
| }; | ||
|
Comment on lines
+3
to
+6
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { User } from '../../../../src/api/models/User'; | ||
| export const systemUser: Partial<User> = { | ||
| email: 'vivekfitkariwala@gmail.com', | ||
| firstName: 'Vivek', | ||
| lastName: 'Fitkariwala', | ||
| email: 'mockUser@mockmail.com', | ||
| firstName: 'Mock', | ||
| lastName: 'User', | ||
| imageUrl: 'https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1920,f_auto/A-Alamy-BXWK5E_vvmkuf.jpg', | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const { Client } = require('pg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const dotenv = require('dotenv'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = require('path'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = async function globalSetup() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dotenv.config({ path: path.join(process.cwd(), '.env.test') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| dotenv.config({ path: path.join(process.cwd(), '.env.test') }); | |
| dotenv.config({ path: path.join(__dirname, '../../../.env.test') }); |
Copilot
AI
Mar 19, 2026
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.
TYPEORM_DATABASE is used to determine which database to create, but it isn’t validated. Add guards to ensure it’s defined, not postgres, and matches a safe identifier pattern (e.g. /^[A-Za-z0-9_]+$/) before it’s later interpolated into CREATE DATABASE "${testDb}", to avoid targeting/dropping the wrong DB and to prevent identifier-injection via env vars.
| const testDb = process.env.TYPEORM_DATABASE; | |
| const testDb = process.env.TYPEORM_DATABASE; | |
| const dbNamePattern = /^[A-Za-z0-9_]+$/; | |
| if (!testDb) { | |
| throw new Error('TYPEORM_DATABASE environment variable must be defined for test setup'); | |
| } | |
| if (testDb.toLowerCase() === 'postgres') { | |
| throw new Error('TYPEORM_DATABASE must not be set to "postgres" for test setup'); | |
| } | |
| if (!dbNamePattern.test(testDb)) { | |
| throw new Error('TYPEORM_DATABASE contains invalid characters; only A-Za-z0-9_ are allowed'); | |
| } |
Copilot
AI
Mar 19, 2026
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.
With the current repo .env.test, TYPEORM_DATABASE is set to postgres, so this setup won’t create a separate test DB and teardown will attempt to drop postgres (which will error and is unsafe). Consider failing fast here when TYPEORM_DATABASE is postgres and instructing developers/CI to use a dedicated test DB name (e.g. upgrade_test).
| const testDb = process.env.TYPEORM_DATABASE; | |
| const testDb = process.env.TYPEORM_DATABASE; | |
| if (!testDb) { | |
| throw new Error( | |
| 'TYPEORM_DATABASE is not set in .env.test. Please configure a dedicated test database name, e.g. "upgrade_test".', | |
| ); | |
| } | |
| if (testDb === 'postgres') { | |
| throw new Error( | |
| 'Refusing to use "postgres" as the test database. Please set TYPEORM_DATABASE in .env.test to a dedicated test database name, e.g. "upgrade_test".', | |
| ); | |
| } |
Copilot
AI
Mar 19, 2026
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.
If a previous test run exits before globalTeardown (e.g., SIGKILL/CI timeout), the test DB will remain and globalSetup will currently reuse it without resetting state (it only creates if missing). To guarantee a clean run, consider dropping/recreating the DB in setup (after terminating connections) or otherwise clearing schema/data deterministically.
| const result = await client.query('SELECT 1 FROM pg_database WHERE datname = $1', [testDb]); | |
| if (result.rowCount === 0) { | |
| await client.query(`CREATE DATABASE "${testDb}"`); | |
| console.log(`Created test database: ${testDb}`); | |
| } | |
| // Ensure no active connections to the test database from previous runs | |
| await client.query( | |
| 'SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1 AND pid <> pg_backend_pid()', | |
| [testDb], | |
| ); | |
| // Drop and recreate the test database to guarantee a clean state | |
| await client.query(`DROP DATABASE IF EXISTS "${testDb}"`); | |
| await client.query(`CREATE DATABASE "${testDb}"`); | |
| console.log(`Recreated test database: ${testDb}`); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { Client } = require('pg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dotenv = require('dotenv'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = require('path'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = async function globalTeardown() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dotenv.config({ path: path.join(process.cwd(), '.env.test') }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+4
to
+6
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| module.exports = async function globalTeardown() { | |
| dotenv.config({ path: path.join(process.cwd(), '.env.test') }); | |
| const fs = require('fs'); | |
| function findEnvTest(startDir) { | |
| let dir = startDir; | |
| while (true) { | |
| const candidate = path.join(dir, '.env.test'); | |
| if (fs.existsSync(candidate)) { | |
| return candidate; | |
| } | |
| const parent = path.dirname(dir); | |
| if (parent === dir) { | |
| break; | |
| } | |
| dir = parent; | |
| } | |
| return null; | |
| } | |
| module.exports = async function globalTeardown() { | |
| const envPath = findEnvTest(__dirname); | |
| if (envPath) { | |
| dotenv.config({ path: envPath }); | |
| } else { | |
| dotenv.config(); | |
| } |
Copilot
AI
Mar 19, 2026
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.
TYPEORM_DATABASE is later interpolated into DROP DATABASE IF EXISTS "${testDb}" without validation. Add the same safety checks as in setup (defined, not postgres, safe identifier) before running termination/drop queries so a misconfigured env can’t drop an unintended database.
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.
globalSetup/globalTeardownare now configured in the base Jest config, which means they will run for unit tests and any Jest invocation that usesjest.config.js(e.g.,nps test.unit.run,nps test.coverage). This introduces an unexpected Postgres dependency for unit tests and can create/drop the test database even when running only unit tests. Move these hooks intojest.integration.config.js(or a dedicated integration-only config) and keep the base/unit config DB-free.