Skip to content

Commit

Permalink
build unique schema name based on jest worker id
Browse files Browse the repository at this point in the history
  • Loading branch information
selimb committed Mar 16, 2023
1 parent 2029e06 commit f9258b2
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions tests/helpers/fast-prisma-tests/unique-schema.ts
@@ -1,22 +1,12 @@
import crypto from 'crypto'
import path from 'path'

import { PROJECT_DIR } from './constants'

export const TEST_PATH = expect.getState().testPath
const JEST_WORKER_ID_ENV = 'JEST_WORKER_ID'

/**
* Returns a (probably) unique postgres schema name for the current test file.
* Returns a unique postgres schema name for the current jest worker.
*/
export function buildUniquePgSchemaName(): string {
if (!TEST_PATH) {
throw new Error('Missing test path.')
const workerId = process.env[JEST_WORKER_ID_ENV]
if (!workerId) {
throw new Error(`Missing '${JEST_WORKER_ID_ENV}'.`)
}
const relpath = path.relative(PROJECT_DIR, TEST_PATH)
// Schema names are restricted to 63 characters
const lengthMax = 63
if (relpath.length <= lengthMax) return relpath
const pathHash = crypto.createHash('sha1').update(relpath).digest('hex')
const uniqueName = `${relpath}-${pathHash.slice(0, 8)}`
return uniqueName.slice(uniqueName.length - lengthMax)
return `jest-${workerId}`
}

0 comments on commit f9258b2

Please sign in to comment.