【问题标题】:Nestjs | e2e testing | "smuggle/inject" custom environment variables before ConfigModule triggers validation巢穴 | e2e 测试 |在 ConfigModule 触发验证之前“走私/注入”自定义环境变量
【发布时间】:2021-05-09 00:20:41
【问题描述】:

如何在envValidation 发生之前将我自己的测试环境变量“走私”到 ConfigModule/ConfigService 中? 这将有助于检查:

  • 函数envValidation 工作正常;
  • 在下游,应用程序根据设置的变量运行。
// AppModule.ts


import { validate as envValidate } from './configs/EnvValidation';


@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                type: configService.get('DATABASE_TYPE'),
                host: configService.get('DATABASE_HOST'),
                port: configService.get<number>('DATABASE_PORT'),
                username: configService.get('DATABASE_USERNAME'),
                password: configService.get('DATABASE_PASSWORD'),
                database: configService.get('DATABASE_NAME'),
                entities: [EntityA, EntityB],
                entityPrefix: "simple_auth_",
                synchronize: true,
            }  as ConnectionOptions),
            inject: [ConfigService],
        }),
        TypeOrmModule.forFeature([EntityA, EntityB]),
        ConfigModule.forRoot({
            cache: true,
            validate: envValidate
        })
    ],
    controllers: [
        // controllers...
    ],
    providers: [
        // providers...
    ],
})
export class AppModule {}
// EnvValidation.ts


class EnvironmentVariables {
    @IsIn(['mysql', 'postgres'])
    @IsDefined()
    @IsNotEmpty()
    DATABASE_TYPE: string;

    @IsDefined()
    @IsNotEmpty()
    DATABASE_HOST: string;

    @IsPort()
    @IsDefined()
    @IsNotEmpty()
    DATABASE_PORT: string;

// (...)
}


export function validate(config: Record<string, unknown>) {
    const validatedConfig = plainToClass(EnvironmentVariables, config, {
        enableImplicitConversion: true
    });
    const errors = validateSync(validatedConfig, {
        skipMissingProperties: false
    });

    if (errors.length > 0) {
        const msgs = errors.map((err) => Object.values(err.constraints)[0]);
        const errorMsg = msgs.join(', ');

        throw new Error(`Error loading environment variables. ${errorMsg}`);
    }

    return validatedConfig;
}
// e2e-spec.ts



describe('(e2e)', () => {
    beforeEach(async () => {});

    afterEach(async () => {});

    it('should pass while setting: DATABASE_TYPE=postgres, DATABASE_HOST=localhost, ...', async () => {

        const envVariablesToUse = {
            DATABASE_TYPE: 'postgres',
            DATABASE_HOST: 'localhost',
            // more variables for this test...
        }

        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [
                AppModule,
                // ??
            ]
        })
            .overrideModule(ConfigModule)
            .useValue(ConfigModule.forRoot({envVars: envVariablesToUse}))  // <-- Question 1: How to override the environment variables object with a custom one in order to test the rules defined in the EnvironmentVariables class and to test the implications of these configurations downstream?

            .overrideProvider(ConfigService) // <-- Question 2: How to override the ConfigService? May be handy to replace its behaviour(?)
            .useValue(MockConfigService)
            .compile();

        const app = moduleFixture.createNestApplication();

        let successfullyLoadedEnvVariables = true;
        try {
            await app.init();
        }
        catch(err){
            successfullyLoadedEnvVariables = false;
        }

        expect(successfullyLoadedEnvVariables).toBe(true);

        // Then, hit some endpoints to ensure the app is behaving as expected
    });
});

stackoverflow 填充:看起来您的帖子主要是代码;请添加更多详细信息。
stackoverflow padding:看起来你的帖子主要是代码;请添加更多详细信息。

【问题讨论】:

    标签: node.js typescript nestjs


    【解决方案1】:

    我找到的解决方案是将AppModule转换为Dynamic模块,并通过.register()方法中的process.env设置环境变量。

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 1970-01-01
      • 2021-05-14
      • 2019-11-02
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多