Skip to content

Commit

Permalink
fix(core): preventing recursion of self pointers #3095
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jul 14, 2022
1 parent 365a49c commit 793a3c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ const generateTouches = (moduleDef: Partial<Record<dependencyKeys, any>>, touche
if (!Object.prototype.hasOwnProperty.call(def, '__ngMocksTouches')) {
const local = new Set<any>();
const meta = coreReflectMeta(def);
coreDefineProperty(def, '__ngMocksTouches', local, false);
if (meta) {
generateTouches(meta, local);
}
coreDefineProperty(def, '__ngMocksTouches', local, false);
}

mapValues(def.__ngMocksTouches, touches);
Expand Down
43 changes: 43 additions & 0 deletions tests/issue-3095/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Directive } from '@angular/core';
import { TestBed } from '@angular/core/testing';

import { MockRender } from 'ng-mocks';

class RecursiveDirective {}
Directive({
selector: 'recursive',
providers: [
{
provide: RecursiveDirective,
useExisting: RecursiveDirective,
},
],
})(RecursiveDirective);

@Directive({
selector: 'target',
providers: [
{
provide: RecursiveDirective,
useValue: null,
},
],
})
class TargetDirective {}

// When ng-mocks generates touches, it can meet recursions when a declaration provides itself in own providers.
// In this case, ng-mocks shouldn't cause infinity loop.
describe('issue-3095', () => {
beforeEach(() =>
TestBed.configureTestingModule({
declarations: [TargetDirective],
}).compileComponents(),
);

it('renders directive', () => {
const fixture = MockRender(TargetDirective);
expect(() =>
fixture.point.injector.get(RecursiveDirective),
).not.toThrow();
});
});

0 comments on commit 793a3c5

Please sign in to comment.