Skip to content

Commit

Permalink
added config service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larryrider committed Feb 13, 2024
1 parent cda41f0 commit da8907a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/services/config.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'chai';
import crypto from 'crypto';
import { ConfigService } from '../../src/services/config.service';

import { config } from 'dotenv';
config();

const env = Object.assign({}, process.env);

describe('Config service', () => {
beforeEach(() => {
process.env = env;
});

after(() => {
process.env = env;
});

it('When an env property is requested, then the get method return its value', async () => {
const envKey = 'APP_CRYPTO_SECRET';
const envValue = crypto.randomBytes(8).toString('hex');
process.env[envKey] = envValue;

const newEnvValue = ConfigService.instance.get(envKey);
expect(newEnvValue).to.equal(envValue);
});

it('When an env property that do not have value is requested, then an error is thrown', async () => {
const envKey = 'APP_CRYPTO_SECRET';
process.env = {};

try {
ConfigService.instance.get(envKey);
expect(false).to.be.true; //should throw error
} catch (err) {
const error = err as Error;
expect(error.message).to.equal(`Config key ${envKey} was not found in process.env`);
}
});
});

0 comments on commit da8907a

Please sign in to comment.