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

Angular 4 fails to provide MockBackend #15521

Closed
ealeykin opened this issue Mar 27, 2017 · 20 comments
Closed

Angular 4 fails to provide MockBackend #15521

ealeykin opened this issue Mar 27, 2017 · 20 comments
Assignees
Labels
regression Indicates than the issue relates to something that worked in a previous version type: bug/fix

Comments

@ealeykin
Copy link

ealeykin commented Mar 27, 2017

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Exception if adding MockBackend to a list of AppModule providers:

"Uncaught Error: Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files."

This behavior since Angular 4.0 RC4/5. Mocking this way in 4.0 RC3 and earlier working just fine.

Exception in CompileMetadataResolver.prototype._getProvidersMetadata function
...
else if (provider === void 0) {
_this._reportError(syntaxError("Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files."));
}
...

Expected behavior

FakeProvider is instantiated and MockBackend instance successfully injected.

Minimal reproduction of the problem with instructions

Add MockBackend to a list of AppModule providers.
...
import {MockBackend} from "@angular/http/testing";
...
providers: [
...
MockBackend, // <--
//FakeProvider,
//BaseRequestOptions
],
...

What is the motivation / use case for changing the behavior?

Mock an Http service for testing purpose and backend-less development

Please tell us about your environment:

  • Angular version: 4.0.0
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: Typescript 2.2

  • Node (for AoT issues): node --version = 6.6.0

  • webpack: "^2.3.2", webpack-dev-server: "^2.4.2", webpack-merge: "^4.1.0"
    
@DzmitryShylovich
Copy link
Contributor

Please add a small reproduction

@ealeykin
Copy link
Author

Done, added.

@DzmitryShylovich
Copy link
Contributor

@ealeykin
Copy link
Author

ealeykin commented Mar 28, 2017

Although that happened after updating to Angular 4 RC4/5

provider = webpack_require.i(WEBPACK_IMPORTED_MODULE_0__angular_core["_46" /* resolveForwardRef */])(provider);
...
else if (provider === void 0) {
_this._reportError(syntaxError("Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files."));
}

Could it be a webpack related issue? So basically MockBackend is undefined running the app with a webpack-dev-server.

Please note I'm using that not for tests, but for backendless development.

@IgorMinar IgorMinar added comp: http regression Indicates than the issue relates to something that worked in a previous version type: bug/fix labels Mar 29, 2017
@ealeykin
Copy link
Author

Set all angular components to latest version 4.0, but reverted http module to 4.0 RC3 - and MockBackend provider now works as expected.

Are there any changes I need to consider when moving to angular 4.0?

@DzmitryShylovich
Copy link
Contributor

There has been no changes.
I've just tested it using this starter and haven't got any errors. Here's my code

import {Component, NgModule} from '@angular/core'
import {MockBackend} from '@angular/http/testing'
import {BrowserModule} from '@angular/platform-browser'
import {ConnectionBackend} from '@angular/http';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor(backend: ConnectionBackend) {
    console.log(backend);
    this.name = 'Angular!'
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ],
  providers: [{provide: ConnectionBackend, useClass: MockBackend}]
})
export class AppModule {}

@rerickson1
Copy link

I believe I am seeing a similar issue.
If I build with AOT (ng build --prod):
ERROR in ./src/$$_gendir/app/app.module.ngfactory.ts
Module not found: Error: Can't resolve '@angular/http/testing/src/mock_backend' in 'C:\git\web_lcm_gatwick\src$$_gendir\app'
@ ./src/$$_gendir/app/app.module.ngfactory.ts 60:0-67
@ ./src/main.ts
@ multi ./src/main.ts

If I comment out MockBackend in my providers the issue goes away. If I disable AOT, the issue goes away.

Simple reproduction with angular-cli 1.0.0:

  1. ng new ngcli
  2. edit app.module.ts
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MockBackend } from "@angular/http/testing";

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [
      MockBackend
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
  1. ng build --prod produces error:

ERROR in ./src/$$_gendir/app/app.module.ngfactory.ts
Module not found: Error: Can't resolve '@angular/http/testing/src/mock_backend' in 'C:\git\ngcli\src$$_gendir\app'
@ ./src/$$_gendir/app/app.module.ngfactory.ts 23:0-66
@ ./src/main.ts
@ multi ./src/main.ts

  1. if you run ng build everything works fine

@vascofernandes
Copy link

same problem, MockBackend not available in aot.

@beelaal
Copy link

beelaal commented Apr 12, 2017

How i can run Angular 4 with MVC Asp.net ?? anybody please help

@rerickson1
Copy link

Just tried this again with Angular version 4.0.3 and the issue still exists.
Using my reproduction steps above >#15521 (comment)

ng build --prod or ng serve --prod produces the error.

@chi-lee
Copy link

chi-lee commented Apr 25, 2017

Same problem using Angular CLI 1.0.0 and Angular 4.0.0

alxhub added a commit to alxhub/angular that referenced this issue Apr 27, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes angular#15521.
@alxhub alxhub moved this from Legacy Bugs to PR Pending in Http Apr 27, 2017
alxhub added a commit to alxhub/angular that referenced this issue Apr 28, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes angular#15521.
@mertyildiran
Copy link

mertyildiran commented Apr 30, 2017

I'm getting this error:

ERROR in ./src/$$_gendir/app/app.module.ngfactory.ts
Module not found: Error: Can't resolve '@angular/http/testing/src/mock_backend' in '/home/mertyildiran/Documents/Tower/src/$$_gendir/app'
 @ ./src/$$_gendir/app/app.module.ngfactory.ts 26:0-66
 @ ./src/main.ts
 @ multi ./src/main.ts

You can reproduce the error with using this project of mine.

Removing MockBackend project-wide is fixing the error. But what will people do if this module is mandatory for their project?

Is there any alternative to MockBackend?

@FreezeSoul
Copy link

FreezeSoul commented May 3, 2017

I‘ve also got the same error on angular4 or ionic2/3 with " --prod"!

Uncaught Error: Cannot find module "@angular/http/testing/src/mock_backend"

alxhub added a commit to alxhub/angular that referenced this issue May 3, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes angular#15521.
matsko pushed a commit that referenced this issue May 4, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes #15521.

PR Close #16390
matsko pushed a commit that referenced this issue May 4, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes #15521.

PR Close #16390
@matsko matsko closed this as completed in 9da6340 May 5, 2017
@pain0486
Copy link

pain0486 commented May 6, 2017

So what version will this be released in?

jasonaden pushed a commit to jasonaden/angular that referenced this issue May 10, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes angular#15521.
@nicholasserra
Copy link

Confirmed fix in 4.1.2. Thanks everyone!

@alxhub alxhub removed this from PR Pending in Http May 15, 2017
@pearldiver
Copy link

The issue is closed but i still get the error, i just upgraded to angular 4 (4.1.3) 2 days before, all the production code works fine, but all the tests are down, on reason is the MockBackend imported form '@angular/http/testing' is undefined.
My project don't use cli, the test is started by Karma and webpack.
the curious thing is all other angular module import works except for testing include '@angular/http/testing' and '@angular/core/testing'
Any ideas? thanks!

@sittingbool
Copy link

Same here, also webpack

@iturn
Copy link

iturn commented Jun 21, 2017

issue confirmed in ng 4.0.0, building --prod
issue confimerd in ng 4.2.3, building --prod

ERROR in ./src/$$_gendir/app/app.module.ngfactory.ts
Module not found: Error: Can't resolve '@angular/http/testing/src/mock_backend' in '/Users/user/www/ng4_angle/src/$$_gendir/app'
@ ./src/$$_gendir/app/app.module.ngfactory.ts 46:0-67
@ ./src/main.ts
@ multi ./src/main.ts

asnowwolf pushed a commit to asnowwolf/angular that referenced this issue Aug 11, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes angular#15521.
juleskremer pushed a commit to juleskremer/angular that referenced this issue Aug 28, 2017
@angular/http/testing used to publish a metadata structure which paralleled
the .d.ts structure. This causes ngc to write incorrect imports for this
bundle when compiling providers using MockBackend and other http testing
classes.

This change restructures the @angular/http/testing build a bit, modeling it
after @angular/platform-browser-animations, and produces a FESM structure
that has flat metadata.

Fixes angular#15521.
@madhavsharmaagra
Copy link

I have started to use Angular 5 but it still seems that the deep imports are not working yet. My IDE automatically predicts and add import to the related but to the deep level. By removing the deep link, the app started to work fine. For example,

import { ElementRef } from '@angular/core'; // works fine but on the other hand,

import { ElementRef } from '@angular/core/src/linker/element_ref'; // throws error.

I made it to work just by removing the rest of the deep link and leaving only '@angular/core';

@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 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
regression Indicates than the issue relates to something that worked in a previous version type: bug/fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.