Skip to content

Should be able to load templateUrls in unit tests #1610

Closed

Description

@juliemr

Need something analogous to karma-ng-html2js-preprocessor for Angular1, which allows components using templateUrl to load their template for unit testing.

Activity

  1. self-assigned this
    on Apr 30, 2015
  2. jbdeboer commented on May 1, 2015

    @jbdeboer

    We need this feature as well, since all of our components use templateUrl and we want to unit test them.

  3. added this to the milestone on May 5, 2015
  4. matanlurey commented on May 15, 2015

    @matanlurey
    Contributor

    I implemented this internally by creating a Map<String, String> (templateUrl: HTML) via a build process, and using that with the test bed and a custom TemplateLoader. Angular may consider providing this out of the box.

  5. matthewjh commented on May 15, 2015

    @matthewjh
    Contributor

    It would be nice if there was a solution to this that didn't involve a build step, e.g.

    mockXhr.when('templates/(.*?).html').passThroughTo('base/templates/$1.html');

    Then I could add templates/**/*.html to my karma.conf so that these files are served.

  6. removed this from the Backlog milestone on May 29, 2015
  7. added
    area: testingIssues related to Angular testing features, such as TestBed
    featureLabel used to distinguish feature request from other issues
    on Jun 1, 2015
  8. 34 remaining items

  9. modified the milestones: alpha-35, alpha-34 on Aug 10, 2015
  10. sjelin commented on Aug 11, 2015

    @sjelin
    Contributor

    Hey, sorry. My original approach didn't work at all & I haven't been able to make the meeting. Anyway, I have something working here:

    https://github.com/sjelin/karma-redirect-preprocessor

    The API is essentially the same as karma-ng-html2js-preprocessor's (example here, documentation pending), but behind the scenes it works totally differently. Caching the templates client side isn't feasible without an API like $templateCache. Instead, the idea here is to make karma serve the templates at the same URLs that would be used in production. This way they can be retrieved by Angular through AJAX like normal. (Not sure how clear I'm being, it's 3 AM...)

    If this makes sense to everyone I'll clean it up and throw it on NPM as soon as I can (I'm busy until Wednesday at 2 PM though). Otherwise I think the only solution is to implement something like $templateCache for Angular2.

  11. sjelin commented on Aug 12, 2015

    @sjelin
    Contributor

    I didn't get any feedback so I just went ahead an published to NPM: https://www.npmjs.com/package/karma-redirect-preprocessor

    Let me know if this solves everyone's problems or I need to go do something else.

  12. jbdeboer commented on Aug 17, 2015

    @jbdeboer

    Per @matanlurey 's July 1 comment, this is no longer an issue for GT. I removed it from the GT hotlist.

  13. tbosch commented on Aug 18, 2015

    @tbosch
    Contributor

    Note that we are planning on precompiling templates, see #3605. We probably want to store the templates in this form...

  14. sjelin commented on Aug 18, 2015

    @sjelin
    Contributor

    I think it should still work in that case. If the templates are inline (as in the design doc example), this plugin is unnecessary. If they're retrieved by AJAX, this plugin will allow the AJAX to work regardless of how the template is formatted.

    I'm going to close this issue since I think it solves the problem and there don't seem to be any internal users to ask about it. @zzo, can you update karma-ng-html2js-preprocessor telling them to use karma-redirect-preprocessor in angular2?

  15. zzo commented on Aug 19, 2015

    @zzo
    Contributor

    done.

  16. jelbourn commented on Aug 20, 2015

    @jelbourn
    Contributor

    @sjelin After trying this out today, I found that it's not the most ideal solution for the angular2 codebase, at least. Similar to angular 1, angular2 uses a mock XHR implementation when running unit tests, so there aren't any real HTTP requests made for karma to handle.

    It's seeming more and more that we should have a tool that inlines template contents into the component's JS as some build step (i.e., templateUrl > template in the @View block). Dart already has something like this with the dart transformers.

  17. ludohenin commented on Aug 25, 2015

    @ludohenin

    I have one working if you're interested in gulp tool to do that.
    https://github.com/ludohenin/gulp-inline-ng2-template

  18. tbosch commented on Aug 25, 2015

    @tbosch
    Contributor

    I think we should precompile templates for unit tests as well. This way,
    all templates and also styles will be loaded offline on the server and then
    converted into a format that Angular already understands. See
    #3165

    On Tue, Aug 25, 2015 at 9:42 AM Ludovic HENIN notifications@github.com
    wrote:

    I have this one working if you're interested in gulp tool to do that
    https://github.com/ludohenin/gulp-inline-ng2-template

    —
    Reply to this email directly or view it on GitHub
    #1610 (comment).

  19. angular-automatic-lock-bot commented on Sep 7, 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.

  20. locked and limited conversation to collaborators on Sep 7, 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 TestBedeffort2: daysfeatureLabel used to distinguish feature request from other issues

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions