<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pankaj Kumar Singh on Medium]]></title>
        <description><![CDATA[Stories by Pankaj Kumar Singh on Medium]]></description>
        <link>https://medium.com/@pankaj28843?source=rss-bc86c7ee0d0e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*rKuzux5hI4dOAxKNDFSLCA.jpeg</url>
            <title>Stories by Pankaj Kumar Singh on Medium</title>
            <link>https://medium.com/@pankaj28843?source=rss-bc86c7ee0d0e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 20:40:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pankaj28843/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AngularJS to Angular 7 — Upgrade for performance without breaking unit tests]]></title>
            <link>https://medium.com/@pankaj28843/angularjs-to-angular-7-upgrade-for-performance-without-breaking-unit-tests-a03edf40dfaa?source=rss-bc86c7ee0d0e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a03edf40dfaa</guid>
            <category><![CDATA[ngupgrade]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[downgrade]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Pankaj Kumar Singh]]></dc:creator>
            <pubDate>Tue, 04 Jun 2019 12:14:18 GMT</pubDate>
            <atom:updated>2019-06-05T09:12:20.098Z</atom:updated>
            <content:encoded><![CDATA[<h3>AngularJS to Angular 7 — Upgrade for performance without breaking unit tests</h3><p>Recently we started the process of migrating our app from AngularJS 1.7 to Angular 7. We are using downgradeModule because <a href="https://angular.io/guide/upgrade-performance">it has better performance when compared to the </a><a href="https://angular.io/guide/upgrade-performance">UpgradeModule</a>.</p><h3>Our setup for running the hybrid app:</h3><ol><li>All of our AngularJS code is written using Typescript and we have custom webpack config and some .d.ts files for API responses etc.</li><li>We started writing Angular code in a new git repository so that we can use the latest tools for development without affecting the original repository where we are still developing new features using AngularJS.</li><li>We publish a package called @my-org/ng1-app-typings to our private NPM registry, this is used in our Angular 7 code for type checking (whenever we refer to an AngularJS service or use certain types for @Input() binding).</li><li>In the Angular 7 project, <a href="https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5">we have a library </a>called hybrid-app. This library has an Angular module called HybridAppModule. We import various feature modules into this one.</li><li>From the Angular 7 project, we publish a package called @my-org/hybrid-app to our private NPM registry. This package contains a downgraded module and other components/injectables which we need to use in our AngularJS app.</li><li>We have a component called BootstrapComponent, which is available to AngularJS as &lt;ng2-bootstrap&gt;. This is inserted at the beginning of &lt;body&gt; tag in our index.html.</li><li>We use ui-router for managing routes in our AngularJS app. We <a href="https://github.com/ui-router/angular-hybrid#defer-intercept">defer </a><a href="https://github.com/ui-router/angular-hybrid#defer-intercept">$urlService of </a><a href="https://github.com/ui-router/angular-hybrid#defer-intercept">ui-router as described in the official docs</a>, and resume it in the constructor of BootstrapComponent. This means ui-router does not render any route until Angular 7 has been bootstrapped. Because we place &lt;ng2-bootstrap&gt; before &lt;ui-view&gt; in our index.html bootstrapping of HybridAppModule is always guaranteed.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dIvVstexOpcYseFgqBieWQ.png" /><figcaption>Parts of Angular source code which helped me in fixing our unit tests 😁</figcaption></figure><h3>Code examples:</h3><p><strong>BootstrapComponent</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e1c7ce9fce080641b9c65421f2d1b46/href">https://medium.com/media/4e1c7ce9fce080641b9c65421f2d1b46/href</a></iframe><p>In the above code snippet, we resume <a href="https://ui-router.github.io/ng1/docs/latest/classes/url.urlservice.html">$urlService of UI-Router</a> in the constructor of BootstrapComponent. Because it is a very lightweight component, bootstrapping of Angular should be really fast.</p><p><strong>HybridAppModule</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7490715eb4276c51488f0df471771c1f/href">https://medium.com/media/7490715eb4276c51488f0df471771c1f/href</a></iframe><p>In our project, we have added all of our feature modules in imports of HybridAppModule. This will be replaced by a regular AppModule as we near towards completion of the migration process.</p><p><strong>downgradedNg2Module</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20cd806121007e6b1a73f9bd419a0843/href">https://medium.com/media/20cd806121007e6b1a73f9bd419a0843/href</a></iframe><p>Here we are downgrading just one module. <a href="https://angular.io/api/upgrade/static/downgradeModule#downgrading-multiple-modules">Angular also supports downgrading multiple modules</a>, maybe we will use it as the size of our Angular code increases.</p><p><strong>Importing downgraded module in AngularJS app</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78e55289f77439dd2035dfcf3e82fe13/href">https://medium.com/media/78e55289f77439dd2035dfcf3e82fe13/href</a></iframe><p>We have configured our CI pipeline to publish a new version of the NPM package in every build. This makes it easy for us to deploy each PR to a temporary URL, and the app can be tested on various browsers and operating systems.</p><p>For local development, we use yarn link.</p><p><strong>An example of an Angular service which will be used in an AngularJS component</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e2a326da183fbadaaf8cf634845e1ca/href">https://medium.com/media/2e2a326da183fbadaaf8cf634845e1ca/href</a></iframe><p>In the above code snippet, we have a specified type for myNg1Service, this helps the Intellisense of our IDE.</p><p>@my-org/ng1-app-typings gets published automatically by our CI as we make changes to our AngularJS code, and we only need to update the version number if required types have changed.</p><p>There is a risk of using an older version @my-org/ng1-app-typings in our Angular code, i.e., the CI builds succeed, but the app breaks during the runtime. It has not happened yet.</p><p><strong>Using Angular service in AngularJS component</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/611e5bf6c1b6baec2d8d0430d6457ff5/href">https://medium.com/media/611e5bf6c1b6baec2d8d0430d6457ff5/href</a></iframe><p>Here we have injected a downgraded Injectable from Angular 7. Because @angular/cli publishes type definitions as part of <a href="https://docs.google.com/document/d/1CZC2rcpxffTDfRDs6p1cfbmKNLA6x5O-NtkJglDaBVs/edit#!">APF (Angular Package Format)</a>, Intellisense of IDE and TSLint help us in catching type errors.</p><h3>How does it affect existing unit tests for AngularJS code?</h3><p>We have good number of unit tests (&gt;48% coverage) for AngularJS components, services, directives etc., and we want them to run in the hybrid app mode as well.</p><p>After injecting the Angular service, tests for our AppRootComponent started failing: <a href="https://github.com/angular/angular/blob/7.2.x/packages/upgrade/static/src/downgrade_module.ts#L152-L156">Error: Trying to get the Angular injector before bootstrapping the corresponding Angular module.</a> 🤔</p><p>We bootstrap AngularJS automatically in our index.html <a href="https://docs.angularjs.org/guide/bootstrap#automatic-initialization">using </a>the <a href="https://docs.angularjs.org/guide/bootstrap#automatic-initialization">ng-app directive</a>. Angular gets bootstrapped automatically when the app runs in the browser because we have &lt;ng2-bootstrap&gt;in index.html.</p><p>After a lot of Google searching and diving into the Angular source code, I learned that:</p><ol><li>In <a href="https://github.com/angular/angular/blob/7.2.x/packages/upgrade/static/test/integration/downgrade_module_spec.ts">unit tests of </a><a href="https://github.com/angular/angular/blob/7.2.x/packages/upgrade/static/test/integration/downgrade_module_spec.ts">downgradeModule</a>, they use a helper function called html, to create new HTMLElement from HTML string which also gets added to the DOM. This element is later used for bootstrapping downgraded module using angular.bootstrap(element, [downgradedModuleName]).</li><li>Since, Angular 7.1, <a href="https://github.com/angular/angular/pull/27175/files#diff-283a3559b01e22a37555592e3d0a760bR148">ng-version attribute is set on the HTMLElement of the root component</a>. It means ng-version attribute is also available on all downgraded components in the hybrid app, after they get rendered.</li></ol><p>So, I created a helper function that would bootstrap our downgraded module in the same fashion as above and then it will wait for availability of the ng-version attribute to check if rendering of the &lt;ng2-bootstrap&gt; component has finished.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/997540fb5d5b3baed045c364c67dc510/href">https://medium.com/media/997540fb5d5b3baed045c364c67dc510/href</a></iframe><p>Now we can use the above bootstrapDowngradedNg2Module function in beforeEach to make sure Angular gets bootstrapped before any unit test runs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fa3c36802c13ac387f6931c1ed16d3a/href">https://medium.com/media/7fa3c36802c13ac387f6931c1ed16d3a/href</a></iframe><h3>Fix for “Error: Trying to get the Angular injector before bootstrapping the corresponding Angular module” in other cases</h3><p>We faced the same error when we tried to use a downgraded injectable in one of our directives. Because, we already solved this error for unit tests tests, we know a way to programmatically detect completion of Angular bootstrapping at runtime.</p><p>So, we created an AngularJS service to do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c83839ea0be7a3501c070ecf842afa6/href">https://medium.com/media/3c83839ea0be7a3501c070ecf842afa6/href</a></iframe><p>Ng2BootstrapDetectionService can be used like following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/354bf25906c89735b4af6d2b690c6e62/href">https://medium.com/media/354bf25906c89735b4af6d2b690c6e62/href</a></iframe><blockquote>Warning: One should think twice about alternative solutions instead of blindly using something like <em>Ng2BootstrapDetectionService</em> . Because, even if this works, the code looks bit complicated. If you have an option to fix this error by migrating more things to Angular 7, then please do that.</blockquote><h3>Conclusion</h3><ol><li>downgradeModule has better performance than UpgradeModule. This can be a good argument to convince your teammates/managers if you have not yet started the migration process because of unknown effects of two frameworks on the same page.</li><li>There are fewer online resources for downgradeModule approach in compariosn to UpgradeModule.</li><li>If you use two repository approach like us, you can easily try the latest tools like @nrwl/nx, @angular/cli. This can be really helpful if you want to try the hybrid app in production without changing much in current setup of AngularJS.</li><li>If you don’t find answers for the error messages you are getting, please take a look at the source code of @angular/upgrade. There is a chance you will find something which will help you.</li></ol><p>I hope this post was helpful to you. If you have any feedback or questions, please post them in the comments section, I will try to answer as soon as possible. 😊</p><p>I plan to write more posts as we continue our journey towards latest Angular. 🤞🤞🤞</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a03edf40dfaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>