multiconf
Version:
Work with JSON configs
165 lines (131 loc) • 5.54 kB
JavaScript
// Import.
const assert = require('node:assert');
const test = require('node:test');
const Conf = require('../index');
// Constants.
const TEST_CONF_FILES_DIRECTORY = './tests/data';
const TEST_ENVIRONMENT_CONFIG_PREFIX = 'CONFIG_';
// Init conf.
const conf = Conf.get(TEST_CONF_FILES_DIRECTORY, TEST_ENVIRONMENT_CONFIG_PREFIX);
// Main tests.
test.describe('Check config objects.', () => {
// Test if only user-defined config exist.
test.it('Should contain key "1".', () => {
assert.strictEqual('1', conf.test1.key);
});
// Check if default and user-defined configs exists.
test.it('Should contain key "2".', () => {
assert.strictEqual('2', conf.test2.key);
});
// Check if only default config exist.
test.it('Should contain key "3-default".', () => {
assert.strictEqual('3-default', conf.test3.key);
});
// Check environment config.
test.it('Should contain key 44.', () => {
assert.strictEqual(44, conf.test4.key);
});
});
test.describe('Check default config objects.', () => {
const defaultConf = Conf.getDefault(TEST_CONF_FILES_DIRECTORY);
// Test if only user-defined config exist.
test.it('Should not contain test1.', () => {
assert.strictEqual(undefined, defaultConf.test1);
});
// Check if default and user-defined configs exists.
test.it('Should contain default key "2".', () => {
assert.strictEqual('2-default', defaultConf.test2.key);
});
// Check if only default config exist.
test.it('Should contain key "3-default".', () => {
assert.strictEqual('3-default', defaultConf.test3.key);
});
// Check environment config.
test.it('Should not contain test4.', () => {
assert.strictEqual(undefined, defaultConf.test4);
});
});
// Environment variable config tests
test.describe('Check environment variable configurations.', () => {
// Set environment variables for testing
// Note: CONFIG_TEST4 is already set in the npm script
// This test uses the already configured ENV var
test.it('Should load environment variable with numeric value', () => {
assert.strictEqual(44, conf.test4.key);
});
// Test case-insensitivity of config keys
test.it('Should provide case-insensitive access to environment variables', () => {
assert.strictEqual(44, conf.TEST4.key);
});
// Test with complex objects from environment variables
test.describe('Complex object handling from environment', () => {
// Create test conf with complex object env var
process.env.CONFIG_COMPLEX = '{"nested": {"value": 123}, "array": [1, 2, 3]}';
const complexConf = Conf.get(TEST_CONF_FILES_DIRECTORY, TEST_ENVIRONMENT_CONFIG_PREFIX);
test.it('Should parse nested objects correctly', () => {
assert.strictEqual(123, complexConf.complex.nested.value);
});
test.it('Should parse arrays correctly', () => {
assert.deepStrictEqual([1, 2, 3], complexConf.complex.array);
});
test.it('Should maintain original case of the env var after prefix removal', () => {
assert.strictEqual(true, 'COMPLEX' in complexConf);
});
// Clean up env var
delete process.env.CONFIG_COMPLEX;
});
// Test prefix detection
test.describe('Environment variable prefix behavior', () => {
// Set environment variables with different prefixes
process.env.DIFFERENT_PREFIX_TEST = '{"value": "wrong"}';
process.env.CONFIG_PREFIX_TEST = '{"value": "correct"}';
const prefixConf = Conf.get(TEST_CONF_FILES_DIRECTORY, TEST_ENVIRONMENT_CONFIG_PREFIX);
test.it('Should only include environment variables with correct prefix', () => {
assert.strictEqual('correct', prefixConf.PREFIX_TEST.value);
assert.strictEqual(undefined, prefixConf.DIFFERENT_PREFIX_TEST);
});
// Clean up env vars
delete process.env.DIFFERENT_PREFIX_TEST;
delete process.env.CONFIG_PREFIX_TEST;
});
// Test various data types via environment variables
test.describe('Data type handling from environment variables', () => {
process.env.CONFIG_BOOLEAN = 'true';
process.env.CONFIG_NUMBER = '42';
process.env.CONFIG_STRING = '"test string"';
process.env.CONFIG_NULL = 'null';
const typesConf = Conf.get(TEST_CONF_FILES_DIRECTORY, TEST_ENVIRONMENT_CONFIG_PREFIX);
test.it('Should parse boolean values correctly', () => {
assert.strictEqual(true, typesConf.boolean);
});
test.it('Should parse number values correctly', () => {
assert.strictEqual(42, typesConf.number);
});
test.it('Should parse string values correctly', () => {
assert.strictEqual('test string', typesConf.string);
});
test.it('Should parse null values correctly', () => {
assert.strictEqual(null, typesConf.null);
});
// Clean up env vars
delete process.env.CONFIG_BOOLEAN;
delete process.env.CONFIG_NUMBER;
delete process.env.CONFIG_STRING;
delete process.env.CONFIG_NULL;
});
// Test error handling for invalid JSON
test.describe('Error handling for invalid environment variables', () => {
test.it('Should throw an error for invalid JSON in environment variable', () => {
process.env.CONFIG_INVALID = '{not valid json}';
assert.throws(
() => Conf.get(TEST_CONF_FILES_DIRECTORY, TEST_ENVIRONMENT_CONFIG_PREFIX),
{
name: 'Error',
message: /Can\'t parse env config "CONFIG_INVALID"\./,
},
);
// Clean up
delete process.env.CONFIG_INVALID;
});
});
});