From 610cbdc7eea2efa08308d25dc974bfffa214084a Mon Sep 17 00:00:00 2001 From: Isaac Datlof Date: Fri, 2 Feb 2018 16:35:53 -0500 Subject: [PATCH] fix: add testbed test that exposed now fixed issue --- karma.conf.ts | 11 +++++++-- lib/mock-module.spec.ts | 50 +++++++++++++++++++++++++++++++++++++++-- lib/mock-module.ts | 24 +++++++++++++++----- lib/test-fixtures.ts | 10 +++++++-- tslint.json | 5 +++++ 5 files changed, 89 insertions(+), 11 deletions(-) diff --git a/karma.conf.ts b/karma.conf.ts index 557450af0c..19909580f7 100644 --- a/karma.conf.ts +++ b/karma.conf.ts @@ -4,9 +4,16 @@ module.exports = (config: any) => { config.set({ autoWatch: false, - browsers: ['Chrome'], + browsers: ['ChromeHeadless'], colors: true, files: [ + 'node_modules/zone.js/dist/zone.js', + 'node_modules/zone.js/dist/long-stack-trace-zone.js', + 'node_modules/zone.js/dist/proxy.js', + 'node_modules/zone.js/dist/sync-test.js', + 'node_modules/zone.js/dist/jasmine-patch.js', + 'node_modules/zone.js/dist/async-test.js', + 'node_modules/zone.js/dist/fake-async-test.js', { pattern: 'lib/**/*.ts' } ], frameworks: ['jasmine', 'karma-typescript'], @@ -16,7 +23,7 @@ module.exports = (config: any) => { '**/*.ts': ['karma-typescript'] }, reporters: ['dots', 'karma-typescript', 'kjhtml'], - singleRun: false, + singleRun: true, karmaTypescriptConfig: { compilerOptions: { diff --git a/lib/mock-module.spec.ts b/lib/mock-module.spec.ts index 24f34db8d4..77c0d9526d 100644 --- a/lib/mock-module.spec.ts +++ b/lib/mock-module.spec.ts @@ -1,8 +1,54 @@ -import { ParentModule } from './test-fixtures'; +import { Component } from '@angular/core'; +import { async, ComponentFixture, getTestBed, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { + BrowserDynamicTestingModule, + platformBrowserDynamicTesting +} from '@angular/platform-browser-dynamic/testing'; + +import { ExampleComponent, ParentModule } from './test-fixtures'; import { MockModule } from './mock-module'; +@Component({ + selector: 'component-subject', + template: ` + + + {{ test | examplePipe }} + ` +}) +class ComponentSubject { + test = 'test'; +} + describe('MockModule', () => { + let fixture: ComponentFixture; + + getTestBed().initTestEnvironment( + BrowserDynamicTestingModule, + platformBrowserDynamicTesting() + ); + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ + ComponentSubject + ], + imports: [ + MockModule(ParentModule) + ], + }) + .compileComponents() + .then(() => { + fixture = TestBed.createComponent(ComponentSubject); + fixture.detectChanges(); + }); + })); + it('should do stuff', () => { - MockModule(ParentModule); + const mockedComponent = fixture.debugElement + .query(By.css('example-component')) + .componentInstance as ExampleComponent; + expect(mockedComponent).not.toBeNull(); }); }); diff --git a/lib/mock-module.ts b/lib/mock-module.ts index e24dea21f6..ad026e5d41 100644 --- a/lib/mock-module.ts +++ b/lib/mock-module.ts @@ -1,4 +1,4 @@ -import { Type } from '@angular/core'; +import { NgModule, Type } from '@angular/core'; import { MockComponent } from 'mock-component'; import { MockDirective } from 'mock-directive'; import { MockPipe } from 'mock-pipe'; @@ -18,14 +18,28 @@ const mockProvider = (provider: any) => ({ provide: provider, useValue: {} }); -export function MockModule(module: Type): Type { +class MockedModule {} + +export function MockModule(module: Type): any { + return NgModule(MockIt(module))(MockedModule); +} + +function MockIt(module: any): any { const mockedModule = { declarations: [] as any[], + exports: [] as any[], providers: [] as any[] }; const declarations = (module as any).__annotations__[0].declarations || []; + const exports = (module as any).__annotations__[0].exports || []; const imports = (module as any).__annotations__[0].imports || []; const providers = (module as any).__annotations__[0].providers || []; - mockedModule.declarations = [...imports.map(MockModule), - ...declarations.map(mockDeclaration)]; + mockedModule.declarations = [...declarations.map(mockDeclaration)]; + mockedModule.exports = exports; mockedModule.providers = providers.map(mockProvider); - return (mockedModule as any) as Type; + imports.reduce((acc: any, im: any) => { + const result = MockIt(im); + acc.declarations.push(...result.declarations); + acc.providers.push(...result.providers); + acc.exports.push(...result.declarations); + }, mockedModule); + return mockedModule; } diff --git a/lib/test-fixtures.ts b/lib/test-fixtures.ts index 42bea939e0..6c676be864 100644 --- a/lib/test-fixtures.ts +++ b/lib/test-fixtures.ts @@ -1,6 +1,6 @@ import { NgModule, Component, Injectable, Directive, Pipe, PipeTransform } from '@angular/core'; -@Directive({selector: 'example-directive'}) +@Directive({selector: '[example-directive]'}) export class ExampleDirective {} @Pipe({name: 'examplePipe'}) @@ -17,6 +17,12 @@ export class ExampleService { } } +@Component({ + selector: 'example-private-component', + template: 'Private thing' +}) +export class ExamplePrivateComponent { } + @Component({ selector: 'example-component', template: 'My Example' @@ -24,7 +30,7 @@ export class ExampleService { export class ExampleComponent { } @NgModule({ - declarations: [ ExampleComponent, ExamplePipe, ExampleDirective ], + declarations: [ ExamplePrivateComponent, ExampleComponent, ExamplePipe, ExampleDirective ], exports: [ ExampleComponent, ExamplePipe, ExampleDirective ], providers: [ ExampleService ] }) diff --git a/tslint.json b/tslint.json index 6e6cd8e305..0ef073113f 100644 --- a/tslint.json +++ b/tslint.json @@ -1,6 +1,7 @@ { "extends": "tslint:all", "rules": { + "align": false, "comment-format": false, "completed-docs": false, "interface-name": false, @@ -10,11 +11,15 @@ "newline-per-chained-call": false, "no-any": false, "no-empty": false, + "no-floating-promises": false, "no-implicit-dependencies": false, "no-import-side-effect": false, "no-magic-numbers": false, + "no-unnecessary-type-assertion": false, "no-parameter-properties": false, + "no-shadowed-variable": false, "no-submodule-imports": false, + "no-unsafe-any": false, "only-arrow-functions": false, "ordered-imports": false, "prefer-function-over-method": false,