Skip to content

Commit

Permalink
Better story around modules structure
Browse files Browse the repository at this point in the history
  • Loading branch information
horia141 committed Jul 8, 2018
1 parent 78b732a commit 1650e5e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/app-module.ts
Expand Up @@ -2,11 +2,9 @@ import { Module } from "@nestjs/common";

import { AppController } from "./app-controller";
import { AppService } from "./app-service";
import { ConfigModule } from "./config";

@Module({
imports: [ConfigModule],
providers: [AppService],
controllers: [AppController],
})
export class AppModule {}
export class AppModule { }
28 changes: 24 additions & 4 deletions src/config.ts
@@ -1,4 +1,4 @@
import { Module } from "@nestjs/common";
import { Module, Global } from "@nestjs/common";
import * as dotenv from "dotenv";
import * as fs from "fs";

Expand All @@ -11,9 +11,18 @@ export class ConfigService {
return new ConfigService(envConfig);
}

private readonly envConfig: {[prop: string]: string};
/**
* In tests, an _empty_ config will be generated. Tests will have to
* fake the calls to various config values, just like they'd do for
* other dependencies.
*/
public static forTesting(): ConfigService {
return new ConfigService({});
}

private readonly envConfig: { [prop: string]: string };

constructor(envConfig?: {[prop: string]: string}) {
constructor(envConfig?: { [prop: string]: string }) {
this.envConfig = envConfig || {};
}

Expand All @@ -28,11 +37,22 @@ export class ConfigService {
}
}

@Global()
@Module({
providers: [{
provide: ConfigService,
useValue: ConfigService.fromFile(".env"),
}],
exports: [ConfigService],
})
export class ConfigModule {}
export class ConfigModule { }

@Global()
@Module({
providers: [{
provide: ConfigService,
useValue: ConfigService.forTesting(),
}],
exports: [ConfigService],
})
export class ConfigTestingModule { }
3 changes: 2 additions & 1 deletion test/app-controller-test.ts
Expand Up @@ -6,13 +6,14 @@ import * as sinon from "sinon";
import { AppController } from "../src/app-controller";
import { AppModule } from "../src/app-module";
import { AppService } from "../src/app-service";
import { ConfigTestingModule } from "../src/config";

describe("AppController", () => {
let module: TestingModule;

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

Expand Down
4 changes: 2 additions & 2 deletions test/app-service-test.ts
Expand Up @@ -6,14 +6,14 @@ import * as sinon from "sinon";
import { AppModule } from "../src/app-module";
import { AppService } from "../src/app-service";
import { Env } from "../src/common";
import { ConfigService } from "../src/config";
import { ConfigService, ConfigTestingModule } from "../src/config";

describe("AppService", () => {
let module: TestingModule;

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

Expand Down
7 changes: 7 additions & 0 deletions test/config-test.ts
Expand Up @@ -28,4 +28,11 @@ PORT=10001
expect(configService.env).to.eql(Env.Test);
expect(configService.port).to.eql(10001);
});

it("can be constructed for testing", () => {
const configService = ConfigService.forTesting();

expect(() => configService.env).to.throw;
expect(() => configService.port).to.throw;
});
});

0 comments on commit 1650e5e

Please sign in to comment.