Search code examples
nestjsnestjs-config

Nestjs Config validation error: in test even that process.env set in code


App.module.ts:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';

@Module({
  imports: [
    ConfigModule.forRoot({
      validationSchema: Joi.object({
        SOME_ENV_VARIABLE: Joi.string().required(),
      }),
      isGlobal: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

app.e2e-spec.ts:

import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    process.env.SOME_ENV_VARIABLE = 'kuku';
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  it('/ (GET)', () => {
    return request(app.getHttpServer()).get('/').expect(200).expect('kuku');
  });
});

If I run this test regularly the test fails:

 npm run test:e2e

with the following error: Config validation error: "SOME_ENV_VARIABLE" is required

But if I run the test with env

export SOME_ENV_VARIABLE=kuku && npm run test:e2e

the test passes

I don't understand why

process.env.SOME_ENV_VARIABLE = 'kuku';

If I run the test without the validationSchema it works fine in both cases


Solution

  • As @moti mentioned the validate method is running at the Import time. So the only way to test it against multiple different scenarios is to require it on the fly during the test:

    Instead of:

    import { AppModule } from "../src/app.module";
    

    Do:

     beforeEach(() => {
        process.env.SOME_ENV_VARIABLE = 'kuku';
        jest.resetAllMocks();
        jest.requireActual("../src/app.module");
    });
    

    This way it will run with the overridden environment variable