Skip to content

Commit

Permalink
Merge branch '3-fix-config-service-spec-to-not-fail-when-schema-is-mo…
Browse files Browse the repository at this point in the history
…dified' into 'master'

Resolve "fix config service spec to not fail when schema is modified"

Closes #3

See merge request incubator/sms-api!2
  • Loading branch information
Hakier committed Jul 14, 2020
2 parents 7b6399d + f7a6f01 commit be8edaa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sms-api",
"version": "0.1.0",
"version": "0.1.1",
"description": "",
"author": "",
"private": true,
Expand Down
48 changes: 46 additions & 2 deletions src/config/config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as Joi from '@hapi/joi';
import { Logger } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { unlinkSync, writeFileSync } from 'fs';
import { ConfigService } from './config.service';

async function createTestingModule(configFilePath: string): Promise<TestingModule> {
async function createTestingModule(configFilePath: string, envVarsSchema?: Joi.ObjectSchema): Promise<TestingModule> {
(ConfigService as any).envVarsSchema = envVarsSchema || Joi.object({
NODE_ENV: Joi.string()
.pattern(/^development|production|test$/)
.default('development'),
PORT: Joi.number().default(3000),
CORS_ENABLED: Joi.boolean().required(),
}).unknown();

return Test.createTestingModule({
providers: [
{
Expand Down Expand Up @@ -73,7 +82,42 @@ describe('ConfigService', () => {
});
it('should report that config file does not exist and using only ENV', () => {
expect(logger.log).toHaveBeenCalledTimes(1);
expect(logger.log).toHaveBeenCalledWith(`Config file "${notExistingConfigFilePath}" does not exist or cannot be parsed. Using only ENV`, 'Config');
expect(logger.log).toHaveBeenCalledWith(
`Config file "${notExistingConfigFilePath}" does not exist or cannot be parsed. Using only ENV`,
'Config',
);
});
});
describe('when required option is not available in config file nor process ENV', () => {
let creteThrowingModule: () => Promise<TestingModule>;

beforeAll(async () => {
creteThrowingModule = async () => await createTestingModule('some_non-existing_config.env', Joi.object({
REQUIRED_CONFIG: Joi.string().required(),
}));
});

it('should throw from validateInput method with info about that option', async () => {
await expect(creteThrowingModule).rejects.toThrowError('Config validation error: "REQUIRED_CONFIG" is required');
});
});
describe('when described option is not matching pattern in config file nor process ENV', () => {
let creteThrowingModule: () => Promise<TestingModule>;

beforeAll(async () => {
process.env.SOME_PATTERN = 'dev';
creteThrowingModule = async () => await createTestingModule('some_non-existing_config.env', Joi.object({
SOME_PATTERN: Joi.string()
.pattern(/^development|production|test$/)
.default('development'),
}));
});

it('should throw from validateInput method with info about that option', async () => {
await expect(creteThrowingModule).rejects.toThrowError(
'Config validation error: "SOME_PATTERN" with value "dev" fails' +
' to match the required pattern: /^development|production|test$/'
);
});
});
});
3 changes: 2 additions & 1 deletion src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface EnvConfig {

@Injectable()
export class ConfigService {
private static envVarsSchema = envVarsSchema;
private readonly envConfig: EnvConfig;

constructor(private readonly logger: Logger, filePath: string) {
Expand Down Expand Up @@ -52,7 +53,7 @@ export class ConfigService {
* including the applied default values.
*/
private validateInput(envConfig: EnvConfig): EnvConfig {
const { error, value: validatedEnvConfig } = envVarsSchema.validate(envConfig);
const { error, value: validatedEnvConfig } = ConfigService.envVarsSchema.validate(envConfig);

if (error) {
throw new Error(`Config validation error: ${error.message}`);
Expand Down

0 comments on commit be8edaa

Please sign in to comment.