Skip to content

Commit

Permalink
fix: mocking imports after declarations
Browse files Browse the repository at this point in the history
closes #178
  • Loading branch information
satanTime committed Aug 8, 2020
1 parent d9643d4 commit ab3aa6f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ function MockNgModuleDef(ngModuleDef: NgModule, ngModule?: Type<any>): [boolean,
return mockedDef;
};

if (imports && imports.length) {
mockedModuleDef.imports = flatten(imports).map(resolve);
}

if (declarations && declarations.length) {
mockedModuleDef.declarations = flatten(declarations).map(resolve);
}
Expand All @@ -266,6 +262,11 @@ function MockNgModuleDef(ngModuleDef: NgModule, ngModule?: Type<any>): [boolean,
.filter(provider => provider);
}

// mock of imports should be the latest step before exports to ensure that everything has been mocked already
if (imports && imports.length) {
mockedModuleDef.imports = flatten(imports).map(resolve);
}

// Default exports.
if (exports && exports.length) {
mockedModuleDef.exports = flatten(exports).map(resolve);
Expand Down
51 changes: 51 additions & 0 deletions tests/issue-178/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Component, NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { MockBuilder, MockRender } from 'ng-mocks';

@Component({
selector: 'app-hello',
template: 'app-hello',
})
class HelloComponent {}

@Component({
selector: 'app-hello2',
template: 'app-hello2',
})
class Hello2Component {}

const routesHello: Routes = [
{
component: HelloComponent,
path: '',
},
{
component: Hello2Component,
path: '',
},
];

@NgModule({
exports: [RouterModule],
imports: [RouterModule.forChild(routesHello)],
})
class HelloRoutingModule {}

@NgModule({
declarations: [HelloComponent, Hello2Component],
exports: [HelloComponent, Hello2Component],
imports: [HelloRoutingModule],
})
class HelloModule {}

describe('issue-178', () => {
beforeEach(
() => MockBuilder(HelloComponent, HelloModule).replace(RouterModule, RouterTestingModule) // <- causes the issue
);

it('should create the component', () => {
const fixture = MockRender(HelloComponent);
expect(fixture.point.componentInstance).toBeTruthy();
});
});

0 comments on commit ab3aa6f

Please sign in to comment.