forked from safe-global/safe-client-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Injectable IConfigurationService (#41)
- IConfigurationService is an injectable class which provides an interface which can be used to read from the loaded configuration - The default implementation provided uses ConfigService from Nest which reads values from configuration.ts - Adds a TestConfigurationModule which includes a FakeConfigurationService – this FakeConfigurationService can be used to override environment variables in a test setup.
- Loading branch information
Showing
15 changed files
with
229 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/common/config/__tests__/fake.configuration.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { FakeConfigurationService } from './fake.configuration.service'; | ||
|
||
describe('FakeConfigurationService', () => { | ||
let configurationService: FakeConfigurationService; | ||
|
||
beforeEach(async () => { | ||
configurationService = new FakeConfigurationService(); | ||
}); | ||
|
||
it(`Setting key should store its value`, async () => { | ||
configurationService.set('aaa', 'bbb'); | ||
|
||
const result = configurationService.get('aaa'); | ||
|
||
expect(configurationService.keyCount()).toBe(1); | ||
expect(result).toBe('bbb'); | ||
}); | ||
|
||
it(`Retrieving unknown key should return undefined`, async () => { | ||
configurationService.set('aaa', 'bbb'); | ||
|
||
const result = configurationService.get('unknown_key'); | ||
|
||
expect(result).toBe(undefined); | ||
}); | ||
|
||
it(`Retrieving unknown key should throw`, async () => { | ||
configurationService.set('aaa', 'bbb'); | ||
|
||
const result = () => { | ||
configurationService.getOrThrow('unknown_key'); | ||
}; | ||
|
||
expect(result).toThrow(Error('No value set for key unknown_key')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { IConfigurationService } from '../configuration.service.interface'; | ||
|
||
export class FakeConfigurationService implements IConfigurationService { | ||
private configuration: Record<string, any> = {}; | ||
|
||
set(key: string, value: any) { | ||
this.configuration[key] = value; | ||
} | ||
|
||
keyCount(): number { | ||
return Object.keys(this.configuration).length; | ||
} | ||
|
||
get<T>(key: string): T | undefined { | ||
return this.configuration[key] as T; | ||
} | ||
|
||
getOrThrow<T>(key: string): T { | ||
const value = this.configuration[key]; | ||
if (value === undefined) { | ||
throw Error(`No value set for key ${key}`); | ||
} | ||
|
||
return value as T; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { IConfigurationService } from '../configuration.service.interface'; | ||
import { FakeConfigurationService } from './fake.configuration.service'; | ||
|
||
/** | ||
* {@link fakeConfigurationService} should be used in a test setup. | ||
* | ||
* It provides the ability to set a specific configuration key to any value. | ||
* | ||
* {@link fakeConfigurationService} is available only when a module imports | ||
* {@link TestConfigurationModule} | ||
*/ | ||
export const fakeConfigurationService = new FakeConfigurationService(); | ||
|
||
/** | ||
* The {@link TestConfigurationModule} should be used whenever you want to | ||
* override the values provided by {@link NestConfigurationService} | ||
* | ||
* Example: | ||
* Test.createTestingModule({ imports: [ModuleA, TestConfigurationModule]}).compile(); | ||
* | ||
* This will create a TestModule which uses the implementation of ModuleA but | ||
* overrides the real Configuration Module with a fake one – {@link fakeConfigurationService} | ||
*/ | ||
@Global() | ||
@Module({ | ||
providers: [ | ||
{ provide: IConfigurationService, useValue: fakeConfigurationService }, | ||
], | ||
exports: [IConfigurationService], | ||
}) | ||
export class TestConfigurationModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ConfigurationModule } from './configuration.module'; | ||
|
||
describe('ConfigurationModule', () => { | ||
it(`ConfigurationModule is successfully created`, async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [ConfigurationModule], | ||
}).compile(); | ||
|
||
const app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
await app.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { IConfigurationService } from './configuration.service.interface'; | ||
import { NestConfigurationService } from './nest.configuration.service'; | ||
import configuration from './entities/configuration'; | ||
|
||
/** | ||
* A {@link Global} Module which provides local configuration support via {@link IConfigurationService} | ||
* Feature Modules don't need to import this module directly in order to inject | ||
* the {@link IConfigurationService}. | ||
* | ||
* This module should be included in the "root" application module | ||
*/ | ||
@Global() | ||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [configuration], | ||
}), | ||
], | ||
providers: [ | ||
{ provide: IConfigurationService, useClass: NestConfigurationService }, | ||
], | ||
exports: [IConfigurationService], | ||
}) | ||
export class ConfigurationModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const IConfigurationService = Symbol('IConfigurationService'); | ||
|
||
export interface IConfigurationService { | ||
get<T>(key: string): T | undefined; | ||
getOrThrow<T>(key: string): T; | ||
} |
File renamed without changes.
Oops, something went wrong.