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

Support adding mock modules to Angular 2 applications #3092

Open
alecxe opened this issue Apr 6, 2016 · 32 comments
Open

Support adding mock modules to Angular 2 applications #3092

alecxe opened this issue Apr 6, 2016 · 32 comments

Comments

@alecxe
Copy link
Contributor

alecxe commented Apr 6, 2016

Currently it is not possible to add mock modules for Angular 2 applications.

This issue reflects the TODO posted here.

And, to cross-reference the relevant SO post: http://stackoverflow.com/questions/36354233/protractor-mocking-backend-with-angular2-on-api-request.

Thank you.

@jlin412
Copy link

jlin412 commented Dec 14, 2016

@juliemr are we planning to support mock for angular 2? If yes, when? If we uses hybrid mode, will mock continue to work at the meantime?

@juliemr
Copy link
Member

juliemr commented Dec 22, 2016

@jlin412 yes, eventually, but it will require investigation and careful thinking about how it works in both aot and non-aot. If you use hybrid it will continue to work for the ng1 bits.

Everyone else - please use github's emoji response to +1 issues. Adding comments makes it difficult to parse real progress. I'll be removing these comments so that this is readable. Thanks!

@jlin412
Copy link

jlin412 commented Jan 5, 2017

@juliemr Do we have an ETA on mock implementation for angular 2?
Also, is there alternative that we can use in the meantime?

@kaishuu0123
Copy link

kaishuu0123 commented Feb 6, 2017

FROM: http://stackoverflow.com/questions/36354233/protractor-mocking-backend-with-angular2-on-api-request.

Same is true for the http-backend-proxy and httpbackend packages.

Therefore, I created an E2E test environment using docker-compose, but i feel it's exaggerated implementation.

Do you currently have best practices?

@mdasberg
Copy link
Contributor

mdasberg commented Feb 6, 2017

I ran into the same issue for mocking my backend calls. As we use connect middleware to run our app, I created a plugin that allows you to mock your angular 1 and 2 http request.

https://mdasberg.github.io/ng-apimock/

maybe you can use it.

@mrmeku
Copy link

mrmeku commented Mar 24, 2017

I'm not sure the machinism by which to do this but I think the naive approach to install mock modules would be the following:

Protractor would somehow override both of these methods:

  • platformBrowserDynamic().bootstrapModule
  • platformBrowser().bootstrapModuleFactory

In their place would be a simple wrapper which constructs a new ngModule and provides the modules we want to mock out (below is pseudo code of what it would look like):

function bootstrapModuleOverride (moduleToBootstrap) {
  @NgModule({
    imports: [moduleToBootstrap],
    provide: ArrayOfMockModules,
    bootstrap: moduleToBootstrap.bootstrap
  })
  class moduleToBoostrapWrapper {}

  platformBrowserDynamic().bootstrapModule(moduleToBoostrapWrapper);
}

@YoannBureau
Copy link

Any idea if there is a planned release for this feature?

@tauil
Copy link

tauil commented Sep 21, 2017

+1 for this feature. We need mocks in the e2e tests in Angular 2+ applications!

@PCasafont
Copy link

Angular 2+ is growing, so this feature is becoming pretty urgent :)

@alexboisselle-rd
Copy link

Yeah is absolutely crucial to our workflow at the moment.

@lfv89
Copy link

lfv89 commented Nov 19, 2017

We already are at angular5 and still unable to mock http in e2e specs 😕

(@tauil small world, huh?)

@leblancmeneses
Copy link

leblancmeneses commented Nov 27, 2017

Here are common uses I need addMockModule for .

  1. injecting custom interceptors.
    https://angular.io/guide/http#writing-an-interceptor
    example capture http activity and writing to disk using protractor if test fails.

  2. override functionality in services normally to mock thirdparty data services .

      .decorator(
          'xyzService',
          [
            '$q', '$delegate',
            ($q, $delegate) => {
              $delegate.isEnabled = () => true;
              return $delegate;
            }
            $delegate.getProfileDetail = () =>
                  $q.when({name: 'Tester 001', title: 'E2E Tester'});
          ])

For custom services: we could follow the httpclient template approach. Implementing (1) would potentially solve this also.

  1. for access to individual page objects internals (although not kosher)
    ng.probe($0)._debugInfo
    protractor executeScript

@mkopylec
Copy link

When will it be implemented?

@thedanhodson
Copy link

thedanhodson commented Mar 23, 2018

Could really do with this!

@jamescookie
Copy link

This a must-have feature, how can it be two years old?!

As a work-around, you can use jswiremock to stub out the back end calls. I have written up some instructions here: https://gist.github.com/jamescookie/f7b854226ca1e133d1b7b2798095b7f4

@mkopylec
Copy link

Another alternative is nock.
It is an universal library for mocking HTTP requests, not dependent on any framework.

@mdasberg
Copy link
Contributor

mdasberg commented Apr 19, 2018

You can use ng-apimock which is already present on the protractor site. There is a great blog post about it here

You can use it for AngularJs and Angular apps

@00benallen
Copy link

This feature is crucial enough that I think it should be part of core protractor, even if ng-apimock was just included into protractor's core libraries.

@Bmahecha123
Copy link

Trying to thumbs up as much as I can, what is the progress on this feature? It's a heavy blocker at the moment.

@TetonRaven
Copy link

Seriously, has protractor just been abandoned for Angular?

@westonpace
Copy link

So this is partly doable without any changes to protractor or angular today. You can create a "mock" configuration to ng serve which will serve up a different set of modules. This should allow you to do backend testing without needing dependent servers up and running and avoid the need of having to implement the mocking at the HTTP layer. I have created an example repository demonstrating this here: https://github.com/westonpace/angular-example-mock-services

I also outline the steps required in my answer to an SO question here: https://stackoverflow.com/questions/39942118/how-to-inject-different-service-based-on-certain-build-environment-in-angular2/51772549#51772549

It isn't as easy/customizable as it was in Angular 1 (e.g. you can't add different mock services for different tests) but once the scaffolding is up and running adding and removing mock services should be pretty straightforward.

@gabrieldoty
Copy link

Summary of a quick - clean - library-less way to handle this here:

https://stackoverflow.com/questions/36354233/protractor-mocking-backend-with-angular2-on-api-request/51834607#51834607

@katiesandford
Copy link

Is there any update on this? @jlin412 you said that it will require investigation and careful thinking - have you started on this?

@gabrieldoty
Copy link

Is there any update on this? @jlin412 you said that it will require investigation and careful thinking - have you started on this?

@katiesandford, I believe the approach detailed at here will accommodate most testing situations in this setup. If it does not, I would love to know what those outliers are so I can think about those for future situations

@gaeljw
Copy link

gaeljw commented Sep 17, 2018

@gabrieldoty With this approach, you can mock at service level but not at http level if I understand well.
Moreover, in a behavior driven development approach, you want your mocks to be dynamic. Different test steps would setup differents mocks. This seems hard to achieve with this setup.

@gabrieldoty
Copy link

@gaeljw great points! I can see some rather clunky ways to handle the dynamic mocks, but no question a dynamic solution would be preferable!

My application only uses a single dataservice class to handle all HTTP requests, so it is fairly easy to swap out that one service. But that is not nearly as universal as a true http mock..

Thanks for those thoughts!

@FrancescoBorzi
Copy link

any news about this? It looks like Protractor development is no longer that active...

@dubzzz
Copy link

dubzzz commented Feb 1, 2019

any updates?

@flensrocker
Copy link

Mocking modules would help to get around #4300

@DenysVuika
Copy link

Wow, this ticket is almost 4 years old now! Are there any updates?

@sviat9440
Copy link

Any news?

@sujithasekar23
Copy link

Using ng-apimock currently, but is there an update to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests