From 37c762a4d98a07a7cda87bfa0ca01814a14f21f7 Mon Sep 17 00:00:00 2001 From: joonashak Date: Wed, 6 Nov 2024 17:17:21 +0200 Subject: [PATCH] Export mocking module instead of mounting via env var --- dev/package.json | 2 +- dev/src/app.module.ts | 2 ++ dev/src/some/some.module.ts | 2 ++ dev/src/some/some.service.ts | 6 +++++- lib/src/api/clone-bay-mocking-module.ts | 10 ++++++++++ lib/src/api/clone-bay-mocking.service.ts | 16 ++++++++++++++++ lib/src/clone-bay.module.ts | 2 -- lib/src/conditional-modules.spec.ts | 14 -------------- lib/src/conditional-modules.ts | 9 --------- lib/src/index.ts | 2 ++ lib/src/mocking/mocking.module.ts | 1 + 11 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 lib/src/api/clone-bay-mocking-module.ts create mode 100644 lib/src/api/clone-bay-mocking.service.ts delete mode 100644 lib/src/conditional-modules.spec.ts delete mode 100644 lib/src/conditional-modules.ts diff --git a/dev/package.json b/dev/package.json index 0712748..419df0b 100644 --- a/dev/package.json +++ b/dev/package.json @@ -9,7 +9,7 @@ "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", - "start:dev": "npm run install:local && UNSAFE_MOCKING_ENABLED=true nest start --watch --preserveWatchOutput", + "start:dev": "npm run install:local && nest start --watch --preserveWatchOutput", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", diff --git a/dev/src/app.module.ts b/dev/src/app.module.ts index f95f786..86cf98f 100644 --- a/dev/src/app.module.ts +++ b/dev/src/app.module.ts @@ -1,5 +1,6 @@ import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default"; import { + CloneBayMockingModule, CloneBayModule, CloneBayResolversModule, CloneBaySsoModule, @@ -40,6 +41,7 @@ import { SomeModule } from "./some/some.module"; CloneBaySsoModule, CloneBayResolversModule, SomeModule, + CloneBayMockingModule, ], controllers: [AppController], providers: [AppService], diff --git a/dev/src/some/some.module.ts b/dev/src/some/some.module.ts index e65c316..f4d003b 100644 --- a/dev/src/some/some.module.ts +++ b/dev/src/some/some.module.ts @@ -1,5 +1,6 @@ import { CloneBayEsiApiService, + CloneBayMockingModule, CloneBayUserService, } from "@joonashak/nestjs-clone-bay"; import { Module } from "@nestjs/common"; @@ -13,6 +14,7 @@ import { SomeService } from "./some.service"; @Module({ imports: [ MongooseModule.forFeature([{ name: Some.name, schema: SomeSchema }]), + CloneBayMockingModule, ], providers: [ SomeService, diff --git a/dev/src/some/some.service.ts b/dev/src/some/some.service.ts index 693fb4d..5967752 100644 --- a/dev/src/some/some.service.ts +++ b/dev/src/some/some.service.ts @@ -1,4 +1,7 @@ -import { CloneBayEsiApiService } from "@joonashak/nestjs-clone-bay"; +import { + CloneBayEsiApiService, + CloneBayMockingService, +} from "@joonashak/nestjs-clone-bay"; import { Injectable } from "@nestjs/common"; import { InjectModel } from "@nestjs/mongoose"; import { Model } from "mongoose"; @@ -9,6 +12,7 @@ export class SomeService { constructor( @InjectModel(Some.name) private someModel: Model, private apiService: CloneBayEsiApiService, + private mockingService: CloneBayMockingService, ) {} async findAll() { diff --git a/lib/src/api/clone-bay-mocking-module.ts b/lib/src/api/clone-bay-mocking-module.ts new file mode 100644 index 0000000..fd9d2e8 --- /dev/null +++ b/lib/src/api/clone-bay-mocking-module.ts @@ -0,0 +1,10 @@ +import { Module } from "@nestjs/common"; +import { MockingModule } from "../mocking/mocking.module"; +import { CloneBayMockingService } from "./clone-bay-mocking.service"; + +@Module({ + imports: [MockingModule], + providers: [CloneBayMockingService], + exports: [CloneBayMockingService], +}) +export class CloneBayMockingModule {} diff --git a/lib/src/api/clone-bay-mocking.service.ts b/lib/src/api/clone-bay-mocking.service.ts new file mode 100644 index 0000000..0ebe737 --- /dev/null +++ b/lib/src/api/clone-bay-mocking.service.ts @@ -0,0 +1,16 @@ +import { Injectable } from "@nestjs/common"; +import { User } from "../entities/user/user.model"; +import { MockingService } from "../mocking/mocking.service"; + +@Injectable() +export class CloneBayMockingService { + constructor(private mockingService: MockingService) {} + + async createUser(user: Omit) { + return this.mockingService.createUser(user); + } + + async loginWithEveId(session: unknown, eveId: number) { + return this.mockingService.loginWithEveId(session, eveId); + } +} diff --git a/lib/src/clone-bay.module.ts b/lib/src/clone-bay.module.ts index 1645174..f083962 100644 --- a/lib/src/clone-bay.module.ts +++ b/lib/src/clone-bay.module.ts @@ -9,7 +9,6 @@ import { CLONE_BAY_MODULE_OPTIONS_TOKEN, CloneBayModuleDefinition, } from "./clone-bay.module-definition"; -import { conditionalModules } from "./conditional-modules"; import { ConfigModule } from "./config/config.module"; import { CharacterModule } from "./entities/character/character.module"; import { UserModule } from "./entities/user/user.module"; @@ -28,7 +27,6 @@ import { EsiModule } from "./esi/esi.module"; SsoModule, UserModule, AuthenticatedEsiApiModule, - ...conditionalModules(), ], exports: [ CLONE_BAY_MODULE_OPTIONS_TOKEN, diff --git a/lib/src/conditional-modules.spec.ts b/lib/src/conditional-modules.spec.ts deleted file mode 100644 index 1809b7f..0000000 --- a/lib/src/conditional-modules.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { conditionalModules } from "./conditional-modules"; -import { MockingModule } from "./mocking/mocking.module"; - -describe("Conditional module loader", () => { - it("Loads mocking module when it is enabled", () => { - process.env.UNSAFE_MOCKING_ENABLED = "true"; - expect(conditionalModules()).toContain(MockingModule); - }); - - it("Does not load mocking module if it is not enabled", () => { - process.env.UNSAFE_MOCKING_ENABLED = undefined; - expect(conditionalModules()).not.toContain(MockingModule); - }); -}); diff --git a/lib/src/conditional-modules.ts b/lib/src/conditional-modules.ts deleted file mode 100644 index d791c2e..0000000 --- a/lib/src/conditional-modules.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { ModuleDefinition } from "@nestjs/core/interfaces/module-definition.interface"; -import { MockingModule } from "./mocking/mocking.module"; - -export const conditionalModules = (): ModuleDefinition[] => { - if (process.env.UNSAFE_MOCKING_ENABLED === "true") { - return [MockingModule]; - } - return []; -}; diff --git a/lib/src/index.ts b/lib/src/index.ts index be4b9ba..c697eae 100644 --- a/lib/src/index.ts +++ b/lib/src/index.ts @@ -1,5 +1,7 @@ export { CloneBayConfigService } from "./api/clone-bay-config.service"; export { CloneBayEsiApiService } from "./api/clone-bay-esi-api.service"; +export { CloneBayMockingModule } from "./api/clone-bay-mocking-module"; +export { CloneBayMockingService } from "./api/clone-bay-mocking.service"; export { CloneBayResolversModule } from "./api/clone-bay-resolvers.module"; export { CloneBaySsoModule } from "./api/clone-bay-sso.module"; export { CloneBayUserService } from "./api/clone-bay-user.service"; diff --git a/lib/src/mocking/mocking.module.ts b/lib/src/mocking/mocking.module.ts index dc91dae..f2fc98b 100644 --- a/lib/src/mocking/mocking.module.ts +++ b/lib/src/mocking/mocking.module.ts @@ -5,6 +5,7 @@ import { MockingService } from "./mocking.service"; @Module({ providers: [MockingService], controllers: [MockingController], + exports: [MockingService], }) export class MockingModule implements OnModuleInit { private readonly logger = new Logger(MockingModule.name);