Skip to content

Commit

Permalink
Allow database name to be passed in to acceptance test
Browse files Browse the repository at this point in the history
  • Loading branch information
iainsproat committed Sep 6, 2024
1 parent aa2e3f9 commit 549f2e9
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/preview-service-acceptance.yml
Original file line number Diff line number Diff line change
@@ -103,6 +103,7 @@ jobs:
run: yarn test:acceptance
env:
NODE_ENV: test
TEST_DB: preview_service_test
# note that the host is localhost, but the port is the port mapped to the postgres service
PG_CONNECTION_STRING: postgres://preview_service_test:preview_service_test@localhost:5432/preview_service_test
OUTPUT_FILE_PATH: /tmp/preview-service-output.png
17 changes: 12 additions & 5 deletions packages/preview-service/tests/hooks/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -15,10 +15,12 @@ declare module 'vitest' {
}
}

const dbName = `preview_service_${cryptoRandomString({
length: 10,
type: 'alphanumeric'
})}`.toLocaleLowerCase() //postgres will automatically lower case new db names
const dbName =
process.env.TEST_DB || // in the acceptance tests we need to use a database name that is known prior to the test running
`preview_service_${cryptoRandomString({
length: 10,
type: 'alphanumeric'
})}`.toLocaleLowerCase() //postgres will automatically lower case new db names

/**
* Global setup hook
@@ -28,12 +30,17 @@ const dbName = `preview_service_${cryptoRandomString({
export async function setup({ provide }: GlobalSetupContext) {
logger.info('🏃🏻‍♀️‍➡️ Running vitest setup global hook')
const superUserDbClient = getTestDb()
await superUserDbClient.raw(`CREATE DATABASE ${dbName}
const dbAlreadyExists = await superUserDbClient
.select('pg_database')
.where('datname', dbName)
if (!dbAlreadyExists.length) {
await superUserDbClient.raw(`CREATE DATABASE ${dbName}
WITH
OWNER = preview_service_test
ENCODING = 'UTF8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;`)
}
await superUserDbClient.destroy() // need to explicitly close the connection in clients to prevent hanging tests

// this provides the dbName to all tests, and can be accessed via inject('dbName'). NB: The test extensions already implement this, so use a test extension.

0 comments on commit 549f2e9

Please sign in to comment.