Skip to content

Commit

Permalink
bugfix(core): Allow circular structures for dynamic module nestjs#678
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Oct 3, 2018
1 parent 65bb7e7 commit 29b03e5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';
import { Test } from '@nestjs/testing';
import { CircularModule } from '../src/circular-structure-dynamic-module/circular.module';
import { InputService } from '../src/circular-structure-dynamic-module/input.service';

describe('Circular structure for dynamic modules', () => {
it('should resolve circular structure with dynamic modules', async () => {
const builder = Test.createTestingModule({
imports: [CircularModule.forRoot()],
});
const testingModule = await builder.compile();
const inputService = testingModule.get<InputService>(InputService);

expect(inputService).to.be.instanceof(InputService);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DynamicModule } from '@nestjs/common';
import { InputService } from './input.service';

export class CircularModule {
static forRoot(): DynamicModule {
const a = {
module: CircularModule,
providers: [
InputService,
],
b: null,
};
a.b = a;
return a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class InputService {
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"precommit": "lint-staged",
"test": "nyc --require ts-node/register mocha packages/**/*.spec.ts --reporter spec",
"integration-test": "mocha integration/**/*.spec.ts --reporter spec --require ts-node/register",
"integration-test": "mocha integration/injector/**/*.spec.ts --reporter spec --require ts-node/register",
"lint": "tslint -p tsconfig.json -c tslint.json \"packages/**/*.ts\" -e \"*.spec.ts\"",
"format": "prettier **/**/*.ts --ignore-path ./.prettierignore --write && git status",
"build": "gulp build && gulp move",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/injector/module-token-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DynamicModule } from '@nestjs/common';
import { SHARED_MODULE_METADATA } from '@nestjs/common/constants';
import { Type } from '@nestjs/common/interfaces/type.interface';
import * as hash from 'object-hash';
import safeStringify from 'fast-safe-stringify';

export class ModuleTokenFactory {
public create(
Expand All @@ -22,7 +23,9 @@ export class ModuleTokenFactory {
public getDynamicMetadataToken(
dynamicModuleMetadata: Partial<DynamicModule> | undefined,
): string {
return dynamicModuleMetadata ? JSON.stringify(dynamicModuleMetadata) : '';
// Uses safeStringify instead of JSON.stringify
// to support circular dynamic modules
return dynamicModuleMetadata ? safeStringify(dynamicModuleMetadata) : '';
}

public getModuleName(metatype: Type<any>): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/injector/module-token-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import * as hash from 'object-hash';
import { SingleScope } from '../../../common';
import { ModuleTokenFactory } from '../../injector/module-token-factory';
import safeStringify from 'fast-safe-stringify';

describe('ModuleTokenFactory', () => {
let factory: ModuleTokenFactory;
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('ModuleTokenFactory', () => {
expect(token).to.be.deep.eq(
hash({
module: Module.name,
dynamic: JSON.stringify({
dynamic: safeStringify({
components: [{}],
}),
scope: [Module.name],
Expand Down

0 comments on commit 29b03e5

Please sign in to comment.