Skip to content

fix(@angular-devkit/build-angular): add a base href to karma context #12889

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

Merged
merged 1 commit into from
Nov 8, 2018

Conversation

cexbrayat
Copy link
Member

This commits adds a base href value in the karma context iframe used to run unit tests.

It solves a very old issue in Angular angular/angular#12295 where a unit test throws:

No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.

even if the application is fine. This is because the index.html from Angular CLI contains a base href value, but not the Karma context iframe. So when adding a unit test with a testing module that imports a NgModule, for example AppModule, which itself imports RouterModule, the unit test used to throw an error (regression appeared in router 3.1).

That could be solved by either adding RouterTestingModule to the testing module, or by adding a provider { provide: APP_BASE_HREF, useValue: '/' }, but required to understand the issue (see how many thumbs up there are on the original issue).

This solves the issue in a transparent way: developers won't even encounter the problem anymore.

This commits adds a base href value in the karma context iframe used to run unit tests.

It solves a very old issue in Angular angular/angular#12295 where a unit test throws:

    No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.

even if the application is fine. This is because the `index.html` from Angular CLI contains a base href value, but not the Karma context iframe. So when adding a unit test with a testing module that imports a NgModule, for example `AppModule`, which itself imports `RouterModule`, the unit test used to throw an error (regression appeared in router 3.1).

That could be solved by either adding `RouterTestingModule` to the testing module, or by adding a provider `{ provide: APP_BASE_HREF, useValue: '/' }`, but required to understand the issue (see how many thumbs up there are on the original issue).

This solves the issue in a transparent way: developers won't even encounter the problem anymore.
Copy link
Contributor

@filipesilva filipesilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is a confusion situation, but am not 100% sure if this is the right approach...

We do add <base href="/"> to the default index so that part should be a default in the karma context too, I agree.

But that baseHref is configurable on builds, while it isn't on test. So there are side effects that could conceivably come from this change.

I can't think of any concrete ones right now so I think it's better if we add it in for consistency with the default index.html.

@alan-agius4 alan-agius4 added the target: patch This PR is targeted for the next patch release label Nov 8, 2018
@vikerman vikerman merged commit 3c8b33a into angular:master Nov 8, 2018
vikerman pushed a commit that referenced this pull request Nov 8, 2018
…12889)

This commits adds a base href value in the karma context iframe used to run unit tests.

It solves a very old issue in Angular angular/angular#12295 where a unit test throws:

    No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.

even if the application is fine. This is because the `index.html` from Angular CLI contains a base href value, but not the Karma context iframe. So when adding a unit test with a testing module that imports a NgModule, for example `AppModule`, which itself imports `RouterModule`, the unit test used to throw an error (regression appeared in router 3.1).

That could be solved by either adding `RouterTestingModule` to the testing module, or by adding a provider `{ provide: APP_BASE_HREF, useValue: '/' }`, but required to understand the issue (see how many thumbs up there are on the original issue).

This solves the issue in a transparent way: developers won't even encounter the problem anymore.
@Uli-Tiger
Copy link

Uli-Tiger commented Jul 19, 2019

@cexbrayat @vikerman
using "@angular/cli": "7.3.6".
The tag is still missing in karma-debug.html in same directory. It seems that it was only added to karma-context.html. Hence the described issues re-occurs, if you switch to the karma debug runner.

@filipesilva
Copy link
Contributor

@UliOnline would you be interested in submitting a similar PR?

Uli-Tiger added a commit to Uli-Tiger/angular-cli that referenced this pull request Jul 19, 2019
same fix as in angular#12889 , but for the karma debug context.
Issue described in the above PR happens again, if you switch to the karma debug runner. Karma will now use the karma-debug.html instead of the karma-context.html, rendering the initial fix useless.
@Uli-Tiger
Copy link

@filipesilva i tried so, but realized the following:
This change breaks the karma debug runner. clicking on a component test now triggers all tests again instead of only this test, probably because the karma html page relies on base href to point to "/debug.html". So a more sophisticated solution is required here.
Unfortunately i have no clue on who such a solution could/should look like.

In case someone else is having the same issue, i just decided to provide a base ref in the corresponding component test directly

  TestBed.configureTestingModule({
            providers: [{ provide: APP_BASE_HREF, useValue: "/" }]
            [...]

so the tests work in any case.

@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
target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants