1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-10 01:19:53 +01:00
unleash.unleash/src/test/config/create-config.test.ts
Christopher Kolstad 240c6a77a1
Feat/options need types (#794)
feat: options are now typed

- This makes it easier to know what to send to unleash.start / unleash.create
- Using a Partial to instantiate the config, then melding it with defaults to get a config object with all fields set either to their defaults or to whatever is passed in.


Co-authored-by: Fredrik Strand Oseberg <fredrik.no@gmail.com>
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2021-04-22 10:07:10 +02:00

74 lines
2.1 KiB
TypeScript

import test from 'ava';
import createConfig from '../../lib/create-config';
import { IDBOption } from '../../lib/types/option';
test('Should use DATABASE_URL from env', t => {
const databaseUrl = 'postgres://u:p@localhost:5432/name';
delete process.env.NODE_ENV;
process.env.DATABASE_URL = databaseUrl;
const config = createConfig({});
t.is(config.db.host, 'localhost');
t.is(config.db.password, 'p');
t.is(config.db.user, 'u');
t.is(config.db.database, 'name');
t.is(config.db.schema, 'public');
});
test('Should use databaseURl from options', t => {
const databaseUrl = 'postgres://u:p@localhost:5432/name';
const config = createConfig({ databaseUrl });
t.is(config.db.host, 'localhost');
t.is(config.db.password, 'p');
t.is(config.db.user, 'u');
t.is(config.db.database, 'name');
t.is(config.db.schema, 'public');
});
test('Actual config values takes precedence over environment variables', t => {
process.env.DATABASE_URL = 'postgres://test:5432/name';
process.env.NODE_ENV = 'production';
const config = createConfig({
databaseUrl: 'postgres://test:1234/othername',
});
t.is(config.db.port, 1234);
t.is(config.db.database, 'othername');
});
test('should validate getLogger', t => {
const getLogger = () => {};
t.throws(() => {
// @ts-ignore
createConfig({ getLogger });
});
});
test('should allow setting pool size', t => {
const min = 4;
const max = 20;
const db: IDBOption = {
user: 'db_user',
password: 'db_password',
host: 'db-host',
port: 5432,
database: 'unleash',
driver: 'postgres',
schema: 'public',
pool: {
min,
max,
},
disableMigration: false,
};
const config = createConfig({ db });
t.is(config.db.pool.min, min);
t.is(config.db.pool.max, max);
t.is(config.db.driver, 'postgres');
});
test('Can set baseUriPath', t => {
const baseUriPath = 'some';
const config = createConfig({ server: { baseUriPath } });
t.is(config.server.baseUriPath, baseUriPath);
});