0

I have a separate module for env variable and there I validate them like bellow, and I want to write tests for this module but when I try write test it fail and I don't know why or what I do wrong test bellow.

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

import * as Joi from 'joi';

@Module({
  imports: [
    ConfigModule.forRoot({
      validationSchema: Joi.object({
        NODE_ENV: Joi.string()
          .valid('development', 'production', 'test')
          .default('development')
          .required(),
        PORT: Joi.number().default(5000).required(),
      }),
      isGlobal: true,
    }),
  ],
})
export class EnvModule {}
import { Test } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';

import { EnvModule } from './env.module';

describe('EnvModule', () => {
  let configService: ConfigService;

  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [EnvModule],
    }).compile();

    configService = moduleRef.get<ConfigService>(ConfigService);
  });

  it('should throw an error if NODE_ENV is not one of "development", "production", or "test"', () => {
    process.env.NODE_ENV = 'invalid_env';

    expect(() => new EnvModule()).toThrowError(
      'Validation error: "NODE_ENV" must be one of [development, production, test].',
    );

    process.env.NODE_ENV = 'test';
  });
});

I tried solve this problem to create Env module inside expect

0 Answers0