diff --git a/src/app-module.ts b/src/app-module.ts index 730ae0d..a48abec 100644 --- a/src/app-module.ts +++ b/src/app-module.ts @@ -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 { } diff --git a/src/config.ts b/src/config.ts index 5fd7a2f..44ad0b1 100644 --- a/src/config.ts +++ b/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"; @@ -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 || {}; } @@ -28,6 +37,7 @@ export class ConfigService { } } +@Global() @Module({ providers: [{ provide: ConfigService, @@ -35,4 +45,14 @@ export class ConfigService { }], exports: [ConfigService], }) -export class ConfigModule {} +export class ConfigModule { } + +@Global() +@Module({ + providers: [{ + provide: ConfigService, + useValue: ConfigService.forTesting(), + }], + exports: [ConfigService], +}) +export class ConfigTestingModule { } diff --git a/test/app-controller-test.ts b/test/app-controller-test.ts index e61d82e..ee7f473 100644 --- a/test/app-controller-test.ts +++ b/test/app-controller-test.ts @@ -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(); }); diff --git a/test/app-service-test.ts b/test/app-service-test.ts index f645713..77804dc 100644 --- a/test/app-service-test.ts +++ b/test/app-service-test.ts @@ -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(); }); diff --git a/test/config-test.ts b/test/config-test.ts index e2ce58d..ebd259d 100644 --- a/test/config-test.ts +++ b/test/config-test.ts @@ -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; + }); });