Перейти к основному содержимому

EnvModel: Set env value and config value from forRoot

Overview

These tests validate nestjs-mod EnvModel: environment variable reading, required field validation, and DI value propagation into services.

What We Do And Verify

  • We verify how configTransform and ConfigModel/ConfigModelProperty decorators process input parameters.

  • We lock the validation contract and error shape expected by configuration consumers.

  • We confirm that modules/services receive properly prepared configuration values.

  • We explicitly validate the error contract: not only failure itself, but also error shape/content expected by module consumers.

  • We confirm correct lifecycle behavior in test environment: initialization, dependency readiness, and graceful shutdown of app/modules.

GitHub Reference

Setup Code

import {
import { DefaultTestNestApplicationCreate, DefaultTestNestApplicationInitializer } from '@nestjs-mod/testing';
import { join } from 'path';

describe('staticEnvironmentsModel', () => {
// full test in the block below

});

Test Code

it('set env value and config value from forRoot', async () => {
const WEBHOOK_MODULE = 'WebhookModule';
const WEBHOOK_ENV_PREFIX = 'webhook';

let useGuardsFromEnv: boolean | undefined | null = undefined;
let usePipesFromConfig: boolean | undefined | null = undefined;

@EnvModel()
class WebhookEnvironments {
@EnvModelProperty({
description: 'Use guards',
default: 'true',
transform: new BooleanTransformer(),
})
useGuards?: boolean | null;
}

@ConfigModel()
class WebhookConfiguration {
@ConfigModelProperty({
description: 'Use pipes',
default: true,
})
usePipes?: boolean | null;
}

const { WebhookModule } = createNestModule({
moduleName: WEBHOOK_MODULE,
moduleCategory: NestModuleCategory.feature,
staticEnvironmentsModel: WebhookEnvironments,
staticConfigurationModel: WebhookConfiguration,
wrapForRootAsync: (asyncModuleOptions) => {
if (!asyncModuleOptions) {
asyncModuleOptions = {};
}
const FomatterClass = getFeatureDotEnvPropertyNameFormatter(WEBHOOK_ENV_PREFIX);
Object.assign(asyncModuleOptions, {
environmentsOptions: {
propertyNameFormatters: [new FomatterClass()],
name: WEBHOOK_ENV_PREFIX,
},
});

return { asyncModuleOptions };
},
preWrapApplication: async (options) => {
useGuardsFromEnv = options.current.staticEnvironments?.useGuards;
usePipesFromConfig = options.current.staticConfiguration?.usePipes;
},
});

process.env['SERVER_WEBHOOK_USE_GUARDS'] = null as unknown as string;

await bootstrapNestApplication({
modules: {
system: [
ProjectUtils.forRoot({
staticConfiguration: {
applicationPackageJsonFile: join(__dirname, PACKAGE_JSON_FILE),
packageJsonFile: join(__dirname, 'root-' + PACKAGE_JSON_FILE),
nxProjectJsonFile: join(__dirname, PROJECT_JSON_FILE),
envFile: join(__dirname, 'static-environments-model-with-boolean.env'),
},
}),
DefaultTestNestApplicationCreate.forRoot(),
DefaultTestNestApplicationInitializer.forRoot(),
],
feature: [
WebhookModule.forRoot({
staticEnvironments: { useGuards: false },
staticConfiguration: { usePipes: false },
}),
],
},
});

expect(useGuardsFromEnv).toEqual(false);
expect(usePipesFromConfig).toEqual(false);
});