Skip to content

Commit

Permalink
fix(all): fixes OgmaCoreModule not configured
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jmcdo29 committed Jul 25, 2020
1 parent 6a6ae95 commit bdcbcb0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/nestjs-module/src/ogma-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ export class OgmaCoreModule extends createConfigurableDynamicRootModule<
WebsocketInterceptorService,
],
}) {
static Deferred = OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0);
// static Deferred = OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0);
}
10 changes: 5 additions & 5 deletions packages/nestjs-module/src/ogma.module.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<any>): DynamicModule {
const providers: Provider[] = createLoggerProviders(context);
return {
imports: [OgmaCoreModule.Deferred],
imports: [OgmaCoreModule.externallyConfigured(OgmaCoreModule, 0)],
module: OgmaModule,
providers,
exports: providers,
Expand Down
14 changes: 14 additions & 0 deletions packages/nestjs-module/test/app.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
36 changes: 36 additions & 0 deletions packages/nestjs-module/test/app.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
12 changes: 12 additions & 0 deletions packages/nestjs-module/test/app.service.ts
Original file line number Diff line number Diff line change
@@ -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' };
}
}

0 comments on commit bdcbcb0

Please sign in to comment.