Skip to content

Commit 1256877

Browse files
Merge branch 'feature/application-config-injectable' of https://github.com/BrunnerLivio/nest into BrunnerLivio-feature/application-config-injectable
2 parents 499c0b1 + f076ac0 commit 1256877

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { expect } from 'chai';
3+
import { CoreInjectablesModule } from '../src/core-injectables/core-injectables.module';
4+
import { ApplicationConfig, ModuleRef } from '@nestjs/core';
5+
6+
describe('Core Injectables', () => {
7+
let testingModule: TestingModule;
8+
9+
beforeEach(async () => {
10+
const builder = Test.createTestingModule({
11+
imports: [CoreInjectablesModule],
12+
});
13+
testingModule = await builder.compile();
14+
});
15+
16+
it('should provide ApplicationConfig as core injectable', () => {
17+
const applicationConfig = testingModule.get<ApplicationConfig>(
18+
ApplicationConfig,
19+
);
20+
21+
applicationConfig.setGlobalPrefix('/api');
22+
23+
expect(applicationConfig).to.not.be.undefined;
24+
expect(applicationConfig.getGlobalPrefix()).to.be.eq('/api');
25+
});
26+
27+
it('should provide ModuleRef as core injectable', () => {
28+
const moduleRef = testingModule.get<ModuleRef>(ModuleRef);
29+
expect(moduleRef).to.not.be.undefined;
30+
});
31+
32+
it('should provide the current Module as provider', () => {
33+
const module = testingModule.get<CoreInjectablesModule>(CoreInjectablesModule);
34+
expect(module).to.not.be.undefined;
35+
expect(module.constructor.name).to.be.eq('CoreInjectablesModule');
36+
});
37+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Module } from '@nestjs/common';
2+
3+
@Module({})
4+
export class CoreInjectablesModule {}

packages/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from './injector';
1414
export * from './middleware';
1515
export * from './nest-application';
1616
export * from './nest-application-context';
17+
export * from './application-config';
1718
export { NestFactory } from './nest-factory';
1819
export * from './router';
1920
export * from './services';

packages/core/injector/module.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { UnknownExportException } from '../errors/exceptions/unknown-export.exce
2727
import { NestContainer } from './container';
2828
import { InstanceWrapper } from './instance-wrapper';
2929
import { ModuleRef } from './module-ref';
30+
import { ApplicationConfig } from '../application-config';
3031

3132
interface ProviderName {
3233
name?: string | symbol;
@@ -116,6 +117,7 @@ export class Module {
116117
public addCoreProviders(container: NestContainer) {
117118
this.addModuleAsProvider();
118119
this.addModuleRef();
120+
this.addApplicationConfig();
119121
}
120122

121123
public addModuleRef() {
@@ -145,6 +147,19 @@ export class Module {
145147
);
146148
}
147149

150+
public addApplicationConfig() {
151+
this._providers.set(
152+
ApplicationConfig.name,
153+
new InstanceWrapper({
154+
name: ApplicationConfig.name,
155+
metatype: null,
156+
isResolved: true,
157+
instance: this.container.applicationConfig,
158+
host: this,
159+
}),
160+
);
161+
}
162+
148163
public addInjectable<T extends Injectable>(
149164
injectable: Type<T>,
150165
host?: Type<T>,

0 commit comments

Comments
 (0)