From 7427e29fb374db3448ddfd6ddeba033ae207b0fb Mon Sep 17 00:00:00 2001 From: MG Date: Thu, 18 Jun 2020 23:11:59 +0200 Subject: [PATCH] fix: detection of empty modules in mock process closes #142 --- lib/mock-module/mock-module.ts | 7 +++-- tests/issue-142/test.spec.ts | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/issue-142/test.spec.ts diff --git a/lib/mock-module/mock-module.ts b/lib/mock-module/mock-module.ts index a3553c2eb5..bf2e4419e4 100644 --- a/lib/mock-module/mock-module.ts +++ b/lib/mock-module/mock-module.ts @@ -7,6 +7,7 @@ import { flatten, getMockedNgDefOf, isNgDef, + isNgInjectionToken, isNgModuleDefWithProviders, Mock, MockOf, @@ -160,7 +161,7 @@ const NEVER_MOCK: Array> = [CommonModule, ApplicationModule]; // tslint:disable-next-line:cyclomatic-complexity function MockNgModuleDef(ngModuleDef: NgModule, ngModule?: Type): [boolean, NgModule] { - let changed = false; + let changed = !ngMocksUniverse.flags.has('skipMock'); const mockedModuleDef: NgModule = {}; const { bootstrap = [], @@ -204,7 +205,9 @@ function MockNgModuleDef(ngModuleDef: NgModule, ngModule?: Type): [boolean, mockedDef = MockProvider(def); } - resolutions.set(provider, mockedDef); + if (!isNgInjectionToken(provider) || def !== mockedDef) { + resolutions.set(provider, mockedDef); + } changed = changed || mockedDef !== def; return multi && typeof mockedDef === 'object' ? { ...mockedDef, multi } : mockedDef; }; diff --git a/tests/issue-142/test.spec.ts b/tests/issue-142/test.spec.ts new file mode 100644 index 0000000000..2c92ee86d8 --- /dev/null +++ b/tests/issue-142/test.spec.ts @@ -0,0 +1,47 @@ +import { Component, Inject, InjectionToken, NgModule } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { MockModule } from 'ng-mocks'; + +export const TARGET_TOKEN = new InjectionToken('TARGET_TOKEN'); + +@NgModule() +export class TargetModule { + public static forRoot() { + return { + ngModule: TargetModule, + providers: [ + { + multi: true, + provide: TARGET_TOKEN, + useValue: true, + }, + ], + }; + } + + public readonly targetToken: boolean = false; + + constructor(@Inject(TARGET_TOKEN) targetToken: boolean) { + this.targetToken = targetToken; + } +} + +@Component({ + selector: 'target', + template: 'target', +}) +export class TargetComponent {} + +describe('issue-142', () => { + beforeEach(() => + TestBed.configureTestingModule({ + declarations: [TargetComponent], + imports: [MockModule(TargetModule.forRoot())], + }).compileComponents() + ); + + it('test', () => { + const fixture = TestBed.createComponent(TargetComponent); + expect(fixture).toBeDefined(); + }); +});