Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build unique schema name based on jest worker id
- Loading branch information
Showing
1 changed file
with
6 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
} |