From bdcbcb0f422c3c9ee3e3fc383d4d2fcc1d54861f Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Fri, 24 Jul 2020 23:37:05 -0700 Subject: [PATCH] fix(all): fixes OgmaCoreModule not configured Due to how static class members are created in JavaScript and ran at import time rather than call time `OgmaCoreModule.Deferred` was immediately calling `OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0)` which was causing Jest and the Node REPL to throw an error on import of `@ogma/nestjs-module`. This has been remedied and verified by unit tests, integration tests, and manual tests with imports from the REPL itself. fix #106 --- .../nestjs-module/src/ogma-core.module.ts | 2 +- packages/nestjs-module/src/ogma.module.ts | 10 +++--- packages/nestjs-module/test/app.module.ts | 14 ++++++++ .../nestjs-module/test/app.service.spec.ts | 36 +++++++++++++++++++ packages/nestjs-module/test/app.service.ts | 12 +++++++ 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 packages/nestjs-module/test/app.module.ts create mode 100644 packages/nestjs-module/test/app.service.spec.ts create mode 100644 packages/nestjs-module/test/app.service.ts diff --git a/packages/nestjs-module/src/ogma-core.module.ts b/packages/nestjs-module/src/ogma-core.module.ts index 0bcc760fe..bd9db4da5 100644 --- a/packages/nestjs-module/src/ogma-core.module.ts +++ b/packages/nestjs-module/src/ogma-core.module.ts @@ -91,5 +91,5 @@ export class OgmaCoreModule extends createConfigurableDynamicRootModule< WebsocketInterceptorService, ], }) { - static Deferred = OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0); + // static Deferred = OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0); } diff --git a/packages/nestjs-module/src/ogma.module.ts b/packages/nestjs-module/src/ogma.module.ts index 61d1e52f9..d495d68c3 100644 --- a/packages/nestjs-module/src/ogma.module.ts +++ b/packages/nestjs-module/src/ogma.module.ts @@ -1,12 +1,12 @@ import { AsyncModuleConfig } from '@golevelup/nestjs-modules'; import { DynamicModule, Module, Provider } from '@nestjs/common'; -import { OgmaModuleOptions } from './interfaces'; +import { OgmaModuleOptions, Type } from './interfaces'; import { createLoggerProviders } from './ogma.provider'; import { OgmaCoreModule } from './ogma-core.module'; @Module({ - imports: [OgmaCoreModule.Deferred], - exports: [OgmaCoreModule], + /* imports: [OgmaCoreModule.Deferred], + exports: [OgmaCoreModule], */ }) export class OgmaModule { static forRoot(options: OgmaModuleOptions): DynamicModule { @@ -25,10 +25,10 @@ export class OgmaModule { * * @param context string context for the OgmaService to use in logging */ - static forFeature(context: string | (() => any)): DynamicModule { + static forFeature(context: string | (() => any) | Type): DynamicModule { const providers: Provider[] = createLoggerProviders(context); return { - imports: [OgmaCoreModule.Deferred], + imports: [OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0)], module: OgmaModule, providers, exports: providers, diff --git a/packages/nestjs-module/test/app.module.ts b/packages/nestjs-module/test/app.module.ts new file mode 100644 index 000000000..4ff7b40ed --- /dev/null +++ b/packages/nestjs-module/test/app.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { OgmaModule } from '../src'; +import { AppService } from './app.service'; + +@Module({ + imports: [ + OgmaModule.forRoot({ + interceptor: false, + }), + OgmaModule.forFeature(AppService), + ], + providers: [AppService], +}) +export class AppModule {} diff --git a/packages/nestjs-module/test/app.service.spec.ts b/packages/nestjs-module/test/app.service.spec.ts new file mode 100644 index 000000000..859adbfd6 --- /dev/null +++ b/packages/nestjs-module/test/app.service.spec.ts @@ -0,0 +1,36 @@ +import { AppService } from './app.service'; +import { Test } from '@nestjs/testing'; + +describe('AppService', () => { + let service: AppService; + let logger: { log: jest.Mock }; + + beforeEach(async () => { + const modRef = await Test.createTestingModule({ + providers: [ + AppService, + { + provide: 'OGMA_SERVICE:AppService', + useValue: { + log: jest.fn(), + }, + }, + ], + }).compile(); + service = modRef.get(AppService); + logger = modRef.get<{ log: jest.Mock }>('OGMA_SERVICE:AppService'); + }); + + it('should be truthy', () => { + expect(service).toBeTruthy(); + }); + + it('should return { hello: world }', () => { + expect(service.getHello()).toEqual({ hello: 'world' }); + expect(logger.log).toHaveBeenCalledWith('Say Hello'); + }); +}); + +process.on('unhandledRejection', (err) => { + throw new Error(err as any); +}); diff --git a/packages/nestjs-module/test/app.service.ts b/packages/nestjs-module/test/app.service.ts new file mode 100644 index 000000000..5371c6419 --- /dev/null +++ b/packages/nestjs-module/test/app.service.ts @@ -0,0 +1,12 @@ +import { Injectable } from '@nestjs/common'; +import { OgmaLogger, OgmaService } from '../src'; + +@Injectable() +export class AppService { + constructor(@OgmaLogger(AppService) private readonly logger: OgmaService) {} + + getHello() { + this.logger.log('Say Hello'); + return { hello: 'world' }; + } +}