Skip to content

Unit testing ngUpgrade applications #5462

Closed

Description

@teropa

How does one write unit tests for hybrid applications that have a mixture of Angular 1 and Angular 2 components?

I've been able to run a test suite that has some tests for ng1 and some for ng2, but I've achieved this by always mocking out cross-framework dependencies and using beforeEachProviders/injectAsync for Angular 2 components, and angular.mock.module/angular.mock.inject for Angular 1 components. But I don't see a way to test a hybrid with either of these two approaches.

For a minimal example app demonstrating the issue, see https://github.com/teropa/ng1-2-hybrid-unit-tests

Activity

  1. teropa commented on Nov 24, 2015

    @teropa
    ContributorAuthor
  2. added
    area: testingIssues related to Angular testing features, such as TestBed
    on Nov 24, 2015
  3. self-assigned this
    on Nov 24, 2015
  4. yjaaidi commented on Apr 28, 2016

    @yjaaidi
    Contributor

    +1
    Same issue here!
    I'm testing a ng2 component which is using an ng1 component.

    We should find a way to inject scopes for ng1 sub components.

    I get the following error:

    Failed: No provider for $scope! (class0 -> $scope) Error: DI Exception at NoProviderError.BaseException [as constructor] (webpack:///./~/angular2/src/facade/exceptions.js?:17:23) at NoProviderError.AbstractProviderError [as constructor] (webpack:///./~/angular2/src/core/di/exceptions.js?:39:16) at new NoProviderError (webpack:///./~/angular2/src/core/di/exceptions.js?:75:16) at Injector._throwOrNull (webpack:///./~/angular2/src/core/di/injector.js?:877:19) at Injector._getByKeyDefault (webpack:///./~/angular2/src/core/di/injector.js?:928:21) at Injector._getByKey (webpack:///./~/angular2/src/core/di/injector.js?:868:25) at Injector._getByDependency (webpack:///./~/angular2/src/core/di/injector.js?:854:25) at Injector._instantiate (webpack:///./~/angular2/src/core/di/injector.js?:744:36) at Injector._instantiateProvider (webpack:///./~/angular2/src/core/di/injector.js?:716:25) at Injector._new (webpack:///./~/angular2/src/core/di/injector.js?:705:21) at InjectorInlineStrategy.instantiateProvider (webpack:///./~/angular2/src/core/di/injector.js?:205:30) at ElementDirectiveInlineStrategy.init (webpack:///./~/angular2/src/core/linker/element.js?:559:24) at new AppElement (webpack:///./~/angular2/src/core/linker/element.js?:236:28) at viewFactory_LzJobList0 (viewFactory_LzJobList:1392:23) at HostViewFactory.viewFactory_HostLzJobList0 [as viewFactory] (viewFactory_HostLzJobList:75:1) at AppViewManager_.createRootHostView (webpack:///./~/angular2/src/core/linker/view_manager.js?:94:36) at eval (webpack:///./~/angular2/src/core/linker/dynamic_component_loader.js?:103:50) at ZoneDelegate.invoke (webpack:///./~/angular2/bundles/angular2-polyfills.js?:390:29) at Zone.run (webpack:///./~/angular2/bundles/angular2-polyfills.js?:283:44) at eval (webpack:///./~/angular2/bundles/angular2-polyfills.js?:635:58) at ZoneDelegate.invokeTask (webpack:///./~/angular2/bundles/angular2-polyfills.js?:423:38) at Zone.runTask (webpack:///./~/angular2/bundles/angular2-polyfills.js?:320:48) at drainMicroTaskQueue (webpack:///./~/angular2/bundles/angular2-polyfills.js?:541:36) at Zone.run (webpack:///./~/zone.js/dist/zone-microtask.js?:1216:24) at zoneBoundFn (webpack:///./~/zone.js/dist/zone-microtask.js?:1193:26) at lib$es6$promise$$internal$$tryCatch (webpack:///./~/zone.js/dist/zone-microtask.js?:441:17) at lib$es6$promise$$internal$$invokeCallback (webpack:///./~/zone.js/dist/zone-microtask.js?:453:18) at eval (webpack:///./~/zone.js/dist/zone-microtask.js?:1001:14) at eval (webpack:///./~/zone.js/dist/zone-microtask.js?:96:10) at Zone.run (webpack:///./~/zone.js/dist/zone-microtask.js?:1216:24) at zoneBoundFn (webpack:///./~/zone.js/dist/zone-microtask.js?:1193:26) at lib$es6$promise$asap$$flush (webpack:///./~/zone.js/dist/zone-microtask.js?:235:10)

  5. SonofNun15 commented on May 20, 2016

    @SonofNun15

    Any changes on this issue? We're trying to get our Angular 1 unit tests to work with the upgrade adapter in place, downgrading now Angular 2 services to Angular 1. We are getting Error: [$injector:unpr] Unknown provider: ng2.InjectorProvider <- ng2.Injector <- rlObjectService

  6. yjaaidi commented on May 21, 2016

    @yjaaidi
    Contributor

    Hi!

    Interesting issue. Actually, you should not use angular mocks' module and inject but the upgradeProvider.

    It's not documented yet but there are some clues in angular 2's source code.

    1 - Your UpgradeAdapter instance should be a singleton.
    2 - You should add your providers to the UpgradeAdapter singleton using UpgradeAdapter.addProvider.
    3 - You should start the app using UpgradeAdapter.bootstrap then grab the UpgradeAdapterRef instance given to ready's callback function.
    4 - Get the ng1Injector attribute from the UpgradeAdapterRef instance.
    5 - Instantiate your service using the injector's synchronuous get method.

    Here's a plunker with the solution:
    https://embed.plnkr.co/EauYut/

    We also published a blog post about angular 2 migration on Wishtack. We will soon be filling the blog with more posts about angular 2 testing.

    http://www.blog.wishtack.com/#!AngularJS-vs-Angular-2-Should-I-Stay-or-Should-I-Go/cuhk/573b59710cf233ef713771b2

  7. yjaaidi commented on May 21, 2016

    @yjaaidi
    Contributor

    If it works for you then we might close this issue.

  8. teropa commented on May 25, 2016

    @teropa
    ContributorAuthor

    That's pretty neat @yjaaidi. Ideally there'd be some sugar for this in ngUpgrade though, integrating it with angular-mock better.

    I'm working on some updates to the upgrade guide and I'll see if I can add this in as a possible pattern to use.

  9. yjaaidi commented on May 25, 2016

    @yjaaidi
    Contributor

    Cool! Thanks @teropa

  10. 19 remaining items

  11. attilacsanyi commented on Nov 25, 2016

    @attilacsanyi

    Thank you @petebacondarwin 👍

  12. ryanlangton commented on Dec 1, 2016

    @ryanlangton

    I'm not clear on how to get my NG1 unit tests to start working again. My NG1 unit tests are using angular.mock, everything works fine until a dependency is NG2 (for example a downgraded NG2 service using another standard NG2 service). Is there any way to get this working? Do I need to wait for a later release? The documentation on angular.io doesn't address my use case. For now, leaving all my NG1 tests disabled.

  13. added a commit that references this issue on Dec 15, 2016
    2fc0560
  14. angular-automatic-lock-bot commented on Sep 10, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  15. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: testingIssues related to Angular testing features, such as TestBed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions