Skip to content

Commit

Permalink
test coverage for config
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Sep 12, 2024
1 parent 28531c5 commit 3770da9
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 247 deletions.
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" npx jest --pass-with-no-tests "$1"
NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" npx jest --pass-with-no-tests --runInBand "$1"
73 changes: 38 additions & 35 deletions src/extensions/configuration.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,27 @@ export function Configuration({
> = (project: Project, property: Property) => TBlackHole;

//#region Methods
function InjectedDefinitions() {
return new Proxy({} as TInjectedConfig, {
function InjectedDefinitions(): TInjectedConfig {
const out = {} as Record<string, object>;
return new Proxy(out as TInjectedConfig, {
get(_, project: keyof TInjectedConfig) {
return internal.utils.object.get(configuration, project) ?? {};
},
getOwnPropertyDescriptor(_, project: string) {
return {
configurable: false,
enumerable: true,
value: internal.utils.object.get(configuration, project) ?? {},
writable: false,
};
has(_, key: keyof TInjectedConfig) {
Object.keys(configuration).forEach(
(key) => (out[key as keyof typeof out] ??= {}),
);
return Object.keys(configuration).includes(key);
},
ownKeys() {
Object.keys(configuration).forEach(
(key) => (out[key as keyof typeof out] ??= {}),
);
return Object.keys(configuration);
},
set() {
return false;
},
});
}

Expand All @@ -81,6 +86,27 @@ export function Configuration({
event.emit(EVENT_CONFIGURATION_UPDATED, project, property);
}

function validateConfig() {
// * validate
// - ensure all required properties have been defined
configDefinitions.forEach((definitions, project) => {
Object.keys(definitions).forEach((key) => {
const config = [project, key].join(".");
if (
definitions[key].required &&
is.undefined(internal.utils.object.get(configuration, config))
) {
// ruh roh
throw new BootstrapException(
context,
"REQUIRED_CONFIGURATION_MISSING",
`Configuration property ${config} is not defined`,
);
}
});
});
}

// #MARK: Initialize
async function Initialize<
S extends ServiceMap,
Expand All @@ -91,18 +117,11 @@ export function Configuration({
([ConfigLoaderEnvironment, ConfigLoaderFile] as ConfigLoader[]);

const start = Date.now();
// * sanity check
if (!application) {
throw new BootstrapException(
context,
"NO_APPLICATION",
"Cannot load configuration without having defined an application",
);
}

// * were configs disabled?
if (is.empty(configLoaders)) {
logger.warn({ name: Initialize }, `no config loaders defined`);
validateConfig();
return `${Date.now() - start}ms`;
}

Expand All @@ -117,24 +136,8 @@ export function Configuration({
deepExtend(configuration, merge);
});

// * validate
// - ensure all required properties have been defined
configDefinitions.forEach((definitions, project) => {
Object.keys(definitions).forEach((key) => {
const config = [project, key].join(".");
if (
definitions[key].required &&
is.undefined(internal.utils.object.get(configuration, config))
) {
// ruh roh
throw new BootstrapException(
context,
"REQUIRED_CONFIGURATION_MISSING",
`Configuration property ${config} is not defined`,
);
}
});
});
validateConfig();

return `${Date.now() - start}ms`;
}

Expand Down
6 changes: 3 additions & 3 deletions src/extensions/wiring.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ async function bootstrap<
STATS.PreInit = await runPreInit(internal);
// - Pull in user configurations
logger.debug({ name: bootstrap }, "loading configuration");
STATS.Configure =
await BOILERPLATE(internal)?.configuration?.[INITIALIZE](application);
const config = BOILERPLATE(internal)?.configuration;
STATS.Configure = await config?.[INITIALIZE](application);
// - Run through other events in order
logger.debug(
{ name: bootstrap },
Expand Down Expand Up @@ -477,7 +477,7 @@ async function teardown(internal: InternalDefinition, logger: ILogger) {
} catch (error) {
// ! oof
// eslint-disable-next-line no-console
console.error(
global.console.error(
{ error },
"error occurred during teardown, some lifecycle events may be incomplete",
);
Expand Down
Loading

0 comments on commit 3770da9

Please sign in to comment.