diff --git a/lib/common/ng-mocks-universe.ts b/lib/common/ng-mocks-universe.ts index 2006c8601e..a4ce87ab08 100644 --- a/lib/common/ng-mocks-universe.ts +++ b/lib/common/ng-mocks-universe.ts @@ -11,7 +11,7 @@ getGlobal().ngMocksUniverse = getGlobal().ngMocksUniverse || { cacheDeclarations: new Map(), cacheProviders: new Map(), config: new Map(), - flags: new Set(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider']), + flags: new Set(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider', 'correctModuleExports']), global: new Map(), touches: new Set | InjectionToken>(), }; diff --git a/lib/mock-helper/mock-helper.reset.ts b/lib/mock-helper/mock-helper.reset.ts index 70e0978219..ee513f6601 100644 --- a/lib/mock-helper/mock-helper.reset.ts +++ b/lib/mock-helper/mock-helper.reset.ts @@ -6,6 +6,12 @@ export default (): void => { ngMocksUniverse.cacheProviders = new Map(); ngMocksUniverse.config = new Map(); ngMocksUniverse.global = new Map(); - ngMocksUniverse.flags = new Set(['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider']); + ngMocksUniverse.flags = new Set([ + 'cacheModule', + 'cacheComponent', + 'cacheDirective', + 'cacheProvider', + 'correctModuleExports', + ]); ngMocksUniverse.touches = new Set(); }; diff --git a/lib/mock-module/mock-module.spec.fixtures.ts b/lib/mock-module/mock-module.spec.fixtures.ts index 514c89a513..6d5f39dc66 100644 --- a/lib/mock-module/mock-module.spec.fixtures.ts +++ b/lib/mock-module/mock-module.spec.fixtures.ts @@ -54,6 +54,7 @@ export class ExampleConsumerComponent {} export class ChildModule {} @NgModule({ + exports: [ChildModule], imports: [ChildModule], }) export class ParentModule {} @@ -69,11 +70,13 @@ export class SameImports1Module {} export class SameImports2Module {} @NgModule({ + exports: [ChildModule], imports: [ChildModule], }) export class LogicNestedModule {} @NgModule({ + exports: [ChildModule, LogicNestedModule], imports: [ChildModule, LogicNestedModule], }) export class LogicRootModule {} diff --git a/tests/correct-module-exports-11/test.spec.ts b/tests/correct-module-exports-11/test.spec.ts new file mode 100644 index 0000000000..ef63f130d6 --- /dev/null +++ b/tests/correct-module-exports-11/test.spec.ts @@ -0,0 +1,75 @@ +// tslint:disable:no-console + +import { Component, NgModule } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { MockBuilder, MockModule, MockRender, ngMocks } from 'ng-mocks'; + +@Component({ + selector: 'internal', + template: 'internal', +}) +class InternalComponent {} + +@Component({ + selector: 'external', + template: 'external', +}) +class ExternalComponent {} + +@NgModule({ + declarations: [InternalComponent, ExternalComponent], + exports: [ExternalComponent], +}) +class TargetModule {} + +describe('correct-module-exports-11:proper', () => { + // Thanks Ivy, it doesn't throw an error. + let backupWarn: typeof console.warn; + let backupError: typeof console.error; + + beforeAll(() => { + backupWarn = console.warn; + backupError = console.error; + console.error = console.warn = (...args: any[]) => { + throw new Error(args.join(' ')); + }; + }); + + afterAll(() => { + console.error = backupError; + console.warn = backupWarn; + }); + + beforeEach(() => + TestBed.configureTestingModule({ + imports: [MockModule(TargetModule)], + }).compileComponents() + ); + + it('fails on not exported module', () => { + expect(() => MockRender(InternalComponent)).toThrowError(/'internal' is not a known element/); + }); + + it('renders an exported module', () => { + const fixture = MockRender(ExternalComponent); + expect(fixture.nativeElement.innerHTML).toEqual(''); + }); +}); + +describe('correct-module-exports-11:guts', () => { + beforeEach(() => TestBed.configureTestingModule(ngMocks.guts(null, TargetModule)).compileComponents()); + + it('renders an internal module', () => { + const fixture = MockRender(InternalComponent); + expect(fixture.nativeElement.innerHTML).toEqual(''); + }); +}); + +describe('correct-module-exports-11:builder', () => { + beforeEach(() => MockBuilder(null, TargetModule)); + + it('renders an internal module', () => { + const fixture = MockRender(InternalComponent); + expect(fixture.nativeElement.innerHTML).toEqual(''); + }); +}); diff --git a/tests/issue-222/common-module.spec.ts b/tests/issue-222/common-module.spec.ts index 1a2e55bec9..017adf4a89 100644 --- a/tests/issue-222/common-module.spec.ts +++ b/tests/issue-222/common-module.spec.ts @@ -11,6 +11,7 @@ class TargetComponent {} @NgModule({ declarations: [TargetComponent], + exports: [TargetComponent], imports: [CommonModule], }) class TargetModule {}