EnvModel: Returns error if option of env not set
Overview
These tests validate nestjs-mod EnvModel: environment variable reading, required field validation, and DI value propagation into services.
What We're Testing
- Application Bootstrap: Ensures bootstrapNestApplication validates environment models
- Exit Code Handling: Verifies the process exits with code 1 on validation failure
- EnvModel Integration: Tests environment validation in full application context
- Process Exit Mocking: Confirms proper handling of process.exit during tests
GitHub Reference
- File: utils.spec.ts
- Lines: 40-62
Setup Code
The test mocks process.exit to prevent actual process termination during testing:
import { Injectable } from '@nestjs/common';
import { IsNotEmpty } from 'class-validator';
import { EnvModel, EnvModelProperty } from '../env-model/decorators';
import { DefaultNestApplicationInitializer } from '../modules/system/default-nest-application/default-nest-application-initializer';
import { createNestModule } from '../nest-module/utils';
import { bootstrapNestApplication } from './utils';
// Mock process.exit
let originalExit: any;
let exitStatus: any;
beforeAll(() => {
originalExit = process.exit;
process.exit = (status) => {
exitStatus = status;
return null as never;
};
});
afterAll(() => {
process.exit = originalExit;
});
afterEach(() => {
exitStatus = null;
});
Test Code
it('should return error if option of env not set', async () => {
@EnvModel()
class AppEnv {
@EnvModelProperty()
@IsNotEmpty()
option!: string;
}
const { AppModule } = createNestModule({
moduleName: 'AppModule',
environmentsModel: AppEnv,
});
await bootstrapNestApplication({
project: { name: 'TestApp', description: 'Test application' },
modules: {
system: [DefaultNestApplicationInitializer.forRoot()],
feature: [AppModule.forRoot()],
},
});
expect(exitStatus).toEqual(1);
});
What This Test Does
- Mocks process.exit to capture exit codes without terminating the test process
- Creates an EnvModel with a required
optionfield - Creates a NestModule using
createNestModulewith the EnvModel - Attempts to bootstrap the application without setting the required environment variable
- Expects the bootstrap to trigger
process.exit(1)due to validation failure - Verifies exit code is 1, indicating application failed to start
Key Points
- This test locks the behavior contract of the target nestjs-mod block in this scenario.
- Assertions protect integration boundaries and reduce regression risk when internals evolve.