Skip to main content

EnvModel: Returns option value from service of other module

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

/* eslint-disable no-useless-escape */
import { Injectable } from '@nestjs/common';
import { IsNotEmpty } from 'class-validator';
import { ConfigModel, ConfigModelProperty } from '../config-model/decorators';
import { EnvModel, EnvModelProperty } from '../env-model/decorators';
import {
import { DefaultNestApplicationInitializer } from '../modules/system/default-nest-application/default-nest-application-initializer';
import { DefaultNestApplicationListener } from '../modules/system/default-nest-application/default-nest-application-listener';
import { InjectableFeatureConfigurationType } from '../nest-module/types';
import { createNestModule, getNestModuleDecorators } from '../nest-module/utils';
import { bootstrapNestApplication } from './utils';

describe('NestJS application: Utils', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let originalExit: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let exitStatus: any;

beforeAll(() => {
originalExit = process.exit;
process.exit = (status) => {
exitStatus = status;
return null as never;
};
});

afterAll(() => {
process.exit = originalExit;
});

afterEach(() => {
exitStatus = null;
});

describe('NestJS application with env model', () => {

// full test in the block below
});

describe('NestJS application with config model', () => {

});
describe('NestJS application with anv and config model', () => {
});
describe('NestJS application with multi-providing options', () => {
});
describe('NestJS application get markdown of infrastructure', () => {
});
});

Test Code

it('should return option value from service of other module', async () => {
@EnvModel()
class App1Env {
@EnvModelProperty()
@IsNotEmpty()
option!: string;
}

@Injectable()
class App1Service {
constructor(private readonly appEnv: App1Env) {}

getEnv() {
return this.appEnv;
}
}

const { App1Module } = createNestModule({
moduleName: 'App1Module',
environmentsModel: App1Env,
sharedProviders: [App1Service],
});

@Injectable()
class App2Service {
constructor(private readonly appService: App1Service) {}

getEnv() {
return this.appService.getEnv();
}
}

const { App2Module } = createNestModule({
moduleName: 'App2Module',
imports: [App1Module.forFeature()],
providers: [App2Service],
});

process.env['TEST_APP_OPTION'] = 'value1';

const app = await bootstrapNestApplication({
project: { name: 'TestApp', description: 'Test application' },
modules: {
system: [DefaultNestApplicationInitializer.forRoot()],
feature: [App1Module.forRoot({}), App2Module.forRoot()],
},
});

const app2Service = app.get(App2Service);

expect(app2Service.getEnv()).toMatchObject({ option: 'value1' });
});