Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing ngUpgrade applications #5462

Closed
teropa opened this issue Nov 24, 2015 · 12 comments
Closed

Unit testing ngUpgrade applications #5462

teropa opened this issue Nov 24, 2015 · 12 comments
Assignees
Labels
area: testing Issues related to Angular testing features, such as TestBed

Comments

@teropa
Copy link
Contributor

teropa commented Nov 24, 2015

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

@teropa
Copy link
Contributor Author

teropa commented Nov 24, 2015

Ping @juliemr

@juliemr juliemr added the area: testing Issues related to Angular testing features, such as TestBed label Nov 24, 2015
@juliemr juliemr self-assigned this Nov 24, 2015
@yjaaidi
Copy link
Contributor

yjaaidi commented Apr 28, 2016

+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)

@SonofNun15
Copy link

SonofNun15 commented May 20, 2016

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

@yjaaidi
Copy link
Contributor

yjaaidi commented May 21, 2016

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

@yjaaidi
Copy link
Contributor

yjaaidi commented May 21, 2016

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

@teropa
Copy link
Contributor Author

teropa commented May 25, 2016

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.

@yjaaidi
Copy link
Contributor

yjaaidi commented May 25, 2016

Cool! Thanks @teropa

youdz added a commit to youdz/angular that referenced this issue Jun 23, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462
youdz added a commit to youdz/angular that referenced this issue Jun 23, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462
youdz added a commit to youdz/angular that referenced this issue Jun 23, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462
youdz added a commit to youdz/angular that referenced this issue Jun 23, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462
youdz added a commit to youdz/angular that referenced this issue Jun 24, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462
youdz added a commit to youdz/angular that referenced this issue Jun 29, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Jun 29, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Jun 29, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Jul 14, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Oct 14, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Oct 14, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Oct 14, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Nov 2, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
youdz added a commit to youdz/angular that referenced this issue Nov 2, 2016
- Full use of core Angular 2 projection for downgraded Angular 2
  components. In particular, this enables multi-slot projection and
  other features on <ng-content>.
- Hierarchical injectors for downgraded Angular 2 components: downgraded
  components inherit the injector of the first other downgraded Angular
  2 component they find up the DOM tree.
- declareNg1Module() and initForNg1Tests() methods on the UpgradeAdapter
  to allow testing hybrid applications through Angular 1 without having
  to redeclare the adapter module and recompile every downgraded component
  for every test.

Closes angular#6629, angular#7727, angular#8729, angular#5462, angular#9643, angular#9649
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 2, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 3, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Nov 3, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Nov 3, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 4, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 17, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 17, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 18, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 18, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
@attilacsanyi
Copy link

Any update on this @petebacondarwin ? In which Angular 2 version can we use this? Thanks

@petebacondarwin
Copy link
Member

This is the PR to follow: #12675
I hope to get it into 2.3.0

@attilacsanyi
Copy link

Thank you @petebacondarwin 👍

petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 25, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Nov 30, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Dec 1, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Dec 1, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462
@ryanlangton
Copy link

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.

petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Dec 2, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462, angular#12675
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Dec 8, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462, angular#12675
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Dec 8, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462, angular#12675
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Dec 8, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462, angular#12675
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this issue Dec 14, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes angular#5462, angular#12675
@vicb vicb closed this as completed in c18d2fe Dec 14, 2016
vicb pushed a commit that referenced this issue Dec 15, 2016
- New method `UpgradeAdapter.registerForNg1Tests(modules)` declares the
  Angular 1 upgrade module and provides it to the `angular.mock.module()`
  helper.
  This prevents the need to bootstrap the entire hybrid for every test.

Closes #5462, #12675
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: testing Issues related to Angular testing features, such as TestBed
Projects
None yet
Development

No branches or pull requests

7 participants