ConfigModel: Returns error if option of config not set
Overview
These tests validate nestjs-mod ConfigModel: configuration transformation, input validation, and error contract on invalid parameters.
What We're Testing
- Validation Logic: Ensures
@IsNotEmpty()decorator on config properties works correctly - Error Handling: Verifies that missing required options throw proper validation errors
- ConfigModel Integration: Tests the integration between
ConfigModel,ConfigModelProperty, andconfigTransformutility
GitHub Reference
- File: utils.spec.ts
- Lines: 10-48
Setup Code
The test sets up a configuration model with a required field:
import { DynamicModule } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { IsNotEmpty } from 'class-validator';
import { ConfigModel, ConfigModelProperty } from './decorators';
import { configTransform } from './utils';
@ConfigModel()
class AppConfig {
@ConfigModelProperty()
@IsNotEmpty()
option!: string;
}
@Module({ providers: [AppConfig] })
class AppModule {
static forRoot(config: Partial<AppConfig>): DynamicModule {
return {
module: AppModule,
providers: [
{
provide: `${AppConfig.name}_loader`,
useFactory: async (emptyAppConfig: AppConfig) => {
if (config.constructor !== Object) {
Object.setPrototypeOf(emptyAppConfig, config);
}
const obj = await configTransform({
model: AppConfig,
data: config,
});
Object.assign(emptyAppConfig, obj.data);
},
inject: [AppConfig],
},
],
};
}
}
Test Code
it('should return error if option of config not set', async () => {
await expect(
Test.createTestingModule({
imports: [AppModule.forRoot({})],
}).compile(),
).rejects.toHaveProperty('errors.0.constraints.isNotEmpty', 'option should not be empty');
});
What This Test Does
- Creates a ConfigModel with a required
optionfield decorated with@IsNotEmpty() - Attempts to initialize the module with an empty configuration
{} - Expects the compilation to fail with a validation error
- Verifies the error message contains the correct validation constraint:
'option should not be empty'
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.