Skip to content

Review API for inject now that we have app modules #9975

Closed

Description

@juliemr

In #9846, we switched to using AppModules for tests. This is great because it allows for more flexibility, but one consequence is that each test case may require asynchronous setup, in order to precompile components with external templates (because they need to make an XHR).

The solution in #9846 was to make the inject helper method inspect whether it is being called inside an async block or not. This will work for most current user cases, but destroys encapsulation and ties users to our ways of dealing with async. It's also a lot of 'just trust us magic'.

We could, instead, require the user to let us know if they need to do something asynchronous. This could look like:

beforeEach(async(() => {
  configureModule({
    precompile: MyComponentWithExternalTemplate;
  });

  doAsyncPrecompilation(); // Proposed new method
}));

it('does something with that module', inject([TestComponentBuilder], (builder) => {
  // This will always be called synchronously now.
  var fixture = builder.createSync(MyComponentWithExternalTemplate);
});

Currently, (in master at the moment) this would look like:

beforeEach(() => {
  configureModule({
    precompile: MyComponentWithExternalTemplate;
  });
});

it('does something with that module', async(inject([TestComponentBuilder], (builder) => {
  // This will throw an error if it's not inside an async block.
  var fixture = builder.createSync(MyComponentWithExternalTemplate);
}));

Activity

  1. added
    area: testingIssues related to Angular testing features, such as TestBed
    on Jul 11, 2016
  2. added this to the 2.0.0-rc.5 milestone on Jul 11, 2016
  3. self-assigned this
    on Jul 11, 2016
  4. juliemr commented on Jul 11, 2016

    @juliemr
    ContributorAuthor

    (Also, I'm very happy to hear other ideas to keep things nicely encapsulated but low-boilerplate. I don't love my solution here)

  5. juliemr commented on Jul 11, 2016

    @juliemr
    ContributorAuthor

    We have one vote for sticking with the status quo: "Of the three evils, this is most magical but least intrusive. I guess I like it best ... Because being explicit gives me more to do and more to explain and I probably still don't know why I'm doing it really unless I'm thinking hard and re-re-re-reading the docs."

  6. juliemr commented on Jul 12, 2016

    @juliemr
    ContributorAuthor

    Discussed this off thread, and the conclusion is - I'm going to prepare a PR which introduces a function for the user to call which will set up the AppModuleFactory. This will add some burden for the user to understand when that needs to be called, but it removes a lot of magic, makes our methods more encapsulated, and gives us more room for nicely modifying w/o breaking changes before final.

  7. lacolaco commented on Jul 12, 2016

    @lacolaco
    Contributor

    how about configureModuleSync and configureModuleAsync?

  8. juliemr commented on Jul 12, 2016

    @juliemr
    ContributorAuthor

    @laco0416 we would like configureModule to always be synchronous, since it is just storing the info to be set up later - this lets it be nested inside multiple describe blocks and use only the latest. If it created an AppModule every time we called configureModule, we could get weird side effects from creating a module more than once.

  9. lacolaco commented on Jul 12, 2016

    @lacolaco
    Contributor

    @juliemr Thanks, I understand.

  10. added a commit that references this issue on Jul 20, 2016
    b43f954
  11. angular-automatic-lock-bot commented on Sep 9, 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.

  12. locked and limited conversation to collaborators on Sep 9, 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 TestBed

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions