Skip to content
Snippets Groups Projects
configuration.service.spec.ts 2.33 KiB
Newer Older
import { Test } from '@nestjs/testing';
import { ConfigurationService } from './configuration.service';

describe('ConfigurationService', () => {
  let configurationService: ConfigurationService;

  process.env.NODE_ENV = 'local';

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

    configurationService = module.get<ConfigurationService>(ConfigurationService);
  });

  describe('initialisation', () => {
    const OLD_ENV = process.env;
    beforeEach(async () => {
      jest.resetModules(); // Most important - it clears the cache
      process.env = { ...OLD_ENV }; // Make a copy
      process.env.NODE_ENV = 'dev';
    });

    afterAll(() => {
      process.env = OLD_ENV; // Restore old environment
    });

    it('should be defined', () => {
      expect(configurationService).toBeDefined();
    });

    it('should init with dev conf', () => {
      process.env.NODE_ENV = 'dev';
      expect(configurationService.config.host).toBe('resin-dev.grandlyon.com');
    });
  });

  describe('initialisation production', () => {
    const OLD_ENV = process.env;
    beforeEach(async () => {
      jest.resetModules(); // Most important - it clears the cache
      process.env = { ...OLD_ENV }; // Make a copy
      process.env.NODE_ENV = 'production';
    });

    afterAll(() => {
      process.env = OLD_ENV; // Restore old environment
    });

    it('should be defined', () => {
      expect(configurationService).toBeDefined();
    });

    it('should init with prod conf', () => {
      process.env.NODE_ENV = 'production';
      expect(configurationService.config.host).toBe('resin.grandlyon.com');
    });
  });

  describe('validateUser', () => {
    it('should be local conf', () => {
      process.env.NODE_ENV = 'local';
      expect(configurationService.isLocalConf()).toBe(true);
    });
  });

  describe('get config', () => {
    it('should get config', () => {
      const config = configurationService.config;
      expect(Object.keys(config).length).toBe(9);
      expect(Object.keys(config)).toEqual(
        expect.arrayContaining([
          'url',
          'token',
          'host',
          'protocol',
          'port',
          'from',
          'from_name',
          'replyTo',
          'templates',
        ])
      );
    });
  });
});