1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-09 13:47:13 +02:00
unleash.unleash/src/test-setup.ts
Christopher Kolstad b681702b77
task: migrate tests to vitest
Vitest Pros:
* Automated failing test comments on github PRs
* A nice local UI with incremental testing when changing files (`yarn
test:ui`)
* Also nicely supported in all major IDEs, click to run test works (so
we won't miss what we had with jest).
* Works well with ESM

Vitest Cons:
* The ESBuild transformer vitest uses takes a little longer to transform
than our current SWC/jest setup, however, it is possible to setup SWC as
the transformer for vitest as well (though it only does one transform,
so we're paying ~7-10 seconds instead of ~ 2-3 seconds in transform
phase).
* Exposes how slow our tests are (tongue in cheek here)
2025-05-16 11:19:10 +02:00

51 lines
1.8 KiB
TypeScript

import postgresPkg from 'pg';
const { Client } = postgresPkg;
import {
migrateDb,
getDbConfig,
testDbPrefix,
type IUnleashConfig,
} from 'unleash-server';
let initializationPromise: Promise<void> | null = null;
const initializeTemplateDb = (db: IUnleashConfig['db']): Promise<void> => {
if (!initializationPromise) {
initializationPromise = (async () => {
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
if (!testDBTemplateName) {
throw new Error(
'Env variable TEST_DB_TEMPLATE_NAME is not set',
);
}
const client = new Client(db);
await client.connect();
console.log(`Initializing template database ${testDBTemplateName}`);
// first clean up databases from previous runs
const result = await client.query(
`select datname from pg_database where datname like '${testDbPrefix}%';`,
);
await Promise.all(
result.rows.map((row) => {
return client.query(`DROP DATABASE ${row.datname}`);
}),
);
await client.query(`DROP DATABASE IF EXISTS ${testDBTemplateName}`);
await client.query(`CREATE DATABASE ${testDBTemplateName}`);
await client.end();
await migrateDb({
db: { ...db, database: testDBTemplateName },
});
console.log(`Template database ${testDBTemplateName} migrated`);
})();
}
return initializationPromise;
};
export default async function globalSetup() {
process.env.TZ = 'UTC';
process.env.TEST_DB_TEMPLATE_NAME = 'unleash_template_db';
await initializeTemplateDb(getDbConfig());
}