angular testing with lazy loading modules routes -
i using routertestingmodule
, test
it('should navigate foo child path', fakeasync(() => { let router = testbed.get(router); let location = testbed.get(location); let fixture = testbed.createcomponent(appcomponent); router.initialnavigation(); router.navigatebyurl('/foo/child'); tick(); fixture.detectchanges(); expect(location.path()).tobe('/foo/child'); }));
when trying test route lazy loaded children, e.g.
{ path: 'foo', loadchildren: 'app/foo/foo.module#foomodule'}
an error "cannot find module..." thrown.
error: uncaught (in promise): error: cannot find module app/foo/foo.module#foomodule error: cannot find module app/foo/foo.module#foomodule @ spyngmodulefactoryloader.array.concat.spyngmodulefactoryloader.load (webpack:///~/@angular/router/@angular/router/testing.es5.js:78:0 <- src/test.ts:92347:35) [proxyzone] @ routerconfigloader.array.concat.routerconfigloader.loadmodulefactory (webpack:///~/@angular/router/@angular/router.es5.js:3417:0 <- src/test.ts:24005:129) [proxyzone] @ routerconfigloader.array.concat.routerconfigloader.load (webpack:///~/@angular/router/@angular/router.es5.js:3401:25 <- src/test.ts:23989:52) [proxyzone] @ mergemapsubscriber.project (webpack:///~/@angular/router/@angular/router.es5.js:1569:0 <- src/test.ts:22157:108) [proxyzone] @ mergemapsubscriber.array.concat.mergemapsubscriber._trynext (webpack:///~/rxjs/operator/mergemap.js:120:0 <- src/test.ts:57460:27) [proxyzone] @ mergemapsubscriber.array.concat.mergemapsubscriber._next (webpack:///~/rxjs/operator/mergemap.js:110:0 <- src/test.ts:57450:18) [proxyzone] @ mergemapsubscriber.array.concat.subscriber.next (webpack:///~/rxjs/subscriber.js:89:0 <- src/test.ts:19369:18) [proxyzone] @ scalarobservable.array.concat.scalarobservable._subscribe (webpack:///~/rxjs/observable/scalarobservable.js:49:0 <- src/test.ts:56878:24) [proxyzone] @ scalarobservable.array.concat.observable._trysubscribe (webpack:///~/rxjs/observable.js:57:0 <- src/test.ts:232:25) [proxyzone] @ scalarobservable.array.concat.observable.subscribe (webpack:///~/rxjs/observable.js:45:0 <- src/test.ts:220:27) [proxyzone] @ mergemapoperator.array.concat.mergemapoperator.call (webpack:///~/rxjs/operator/mergemap.js:85:0 <- src/test.ts:57425:23) [proxyzone] @ observable.array.concat.observable.subscribe (webpack:///~/rxjs/observable.js:42:0 <- src/test.ts:217:22) [proxyzone] @ mergemapoperator.array.concat.mergemapoperator.call (webpack:///~/rxjs/operator/mergemap.js:85:0 <- src/test.ts:57425:23) [proxyzone] @ observable.array.concat.observable.subscribe (webpack:///~/rxjs/observable.js:42:0 <- src/test.ts:217:22) [proxyzone] @ resolvepromise (webpack:///~/zone.js/dist/zone.js:683:0 <- src/polyfills.ts:3331:17) [proxyzone] @ webpack:///~/zone.js/dist/zone.js:760:0 <- src/polyfills.ts:3408:17 [proxyzone] @ proxyzonespec.array.concat.proxyzonespec.oninvoketask (webpack:///~/zone.js/dist/proxy.js:103:0 <- src/test.ts:90529:39) [proxyzone] @ fakeasynctestzonespec.array.concat.fakeasynctestzonespec.flushmicrotasks (webpack:///~/zone.js/dist/fake-async-test.js:204:0 <- src/test.ts:90058:17) [proxyzone] @ fakeasynctestzonespec.array.concat.fakeasynctestzonespec.tick (webpack:///~/zone.js/dist/fake-async-test.js:187:0 <- src/test.ts:90041:18) [proxyzone] @ object.tick (webpack:///~/@angular/core/@angular/core/testing.es5.js:389:0 <- src/test.ts:53426:29) [proxyzone] @ object.advance (webpack:///src/app/test/test.module.ts:33:2 <- src/test.ts:85821:15) [proxyzone] @ object.<anonymous> (webpack:///src/app/app.component.spec.ts:79:6 <- src/test.ts:85712:27) [proxyzone] @ object.<anonymous> (webpack:///~/@angular/core/@angular/core/testing.es5.js:348:0 <- src/test.ts:53385:26) [proxyzone]
how can test such routes?
ok, found out. should have paid more attention error log. answer myself in case helps someone.
it can done setting stubbed modules spyngmodulefactoryloader
this works:
it('should navigate foo child path', fakeasync(() => { let router = testbed.get(router); let location = testbed.get(location); let fixture = testbed.createcomponent(appcomponent); router.initialnavigation(); const loader = testbed.get(ngmodulefactoryloader); loader.stubbedmodules = {lazymodule: foomodule}; router.resetconfig([ {path: 'foo', loadchildren: 'lazymodule'}, ]); router.navigatebyurl('/foo/child'); tick(); fixture.detectchanges(); expect(location.path()).tobe('/foo/child'); }));
Comments
Post a Comment