Skip to content

fix(core): improve TestBed declarations standalone error message#45999

Closed
dario-piotrowicz wants to merge 1 commit intoangular:mainfrom
dario-piotrowicz:testbed-declatarions-standalone-error-message
Closed

fix(core): improve TestBed declarations standalone error message#45999
dario-piotrowicz wants to merge 1 commit intoangular:mainfrom
dario-piotrowicz:testbed-declatarions-standalone-error-message

Conversation

@dario-piotrowicz
Copy link
Contributor

improve the error message developers get when adding a standalone
component in the TestBed.configureTestingModule's declarations array,
by making more clear the fact that this error originated from the
TestBed call

resolves #45923

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

Issue

Issue Number: #45923

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@AndrewKushnir I've tried following the implementation note you added to the issue, I do hope this is close to what you had in mind 🙂

@pullapprove pullapprove bot requested a review from alxhub May 14, 2022 15:16
@dario-piotrowicz dario-piotrowicz force-pushed the testbed-declatarions-standalone-error-message branch from 17d1c6d to 150ca50 Compare May 14, 2022 20:43
@pkozlowski-opensource pkozlowski-opensource added cross-cutting: standalone Issues related to the NgModule-less world area: core Issues related to the framework runtime labels May 16, 2022
@ngbot ngbot bot added this to the Backlog milestone May 16, 2022
@AndrewKushnir AndrewKushnir self-requested a review May 16, 2022 17:10
@AndrewKushnir AndrewKushnir self-assigned this May 16, 2022
@AndrewKushnir AndrewKushnir removed the request for review from alxhub May 16, 2022 17:10
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@dario-piotrowicz thanks a lot for exploring this approach 👍

As you pointed out, the tricky part is that the code that throws the error is actually very "far" from the test code which is actually incorrect. Passing this information through the stack would be very fragile and add some complexity.

Another approach that we can try in this case is to have a check that would reside in the TestBed code itself and use Component metadata (i.e. what we have in a decorator) to assess if a component is standalone or not.

For example, we can update the TestBed code here:

  configureTestingModule(moduleDef: TestModuleMetadata): void {
    // Enqueue any compilation tasks for the directly declared component.
    if (moduleDef.declarations !== undefined) {
      // Verify that there are no standalone components
      assertNoStandaloneComponents(moduleDef.declarations, this.resolvers.component, 'TestBed.configureTestingModule');
      this.queueTypeArray(moduleDef.declarations, TestingModuleOverride.DECLARATION);
      this.declarations.push(...moduleDef.declarations);
    }
    ...

and the function would look like:

function assertNoStandaloneComponents(types: Type[], resolver: Resolver, location: string) {
  types.forEach(type => {
    const component = resolver.resolve(type);
    if (component && component.standalone) {
      throw new Error('...'); // or actually reuse the code in the `verifyNotStandalone` function
                              // (by extracting it in a separate function) to throw an error
    }
  });
}

The benefit of having a separate assertNoStandaloneComponents function is to be able to reuse it in other places (I have at least one more place where this logic would be helpful).

@dario-piotrowicz
Copy link
Contributor Author

Hi @AndrewKushnir , thanks a lot for your comment, I guess I misunderstood your implementation note in the issue 😓 , I'll try to re-implement the thing in the way you suggested 🙂

@AndrewKushnir
Copy link
Contributor

I guess I misunderstood your implementation note in the issue

@dario-piotrowicz I wasn't sure what the best solution is, so it's great that we've explored this approach to validate that it requires a bit more changes than expected (thus we should change the approach).

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@dario-piotrowicz thanks for the update! 👍 Just left a few comments, please take a look when you get a chance. Thank you.

@dario-piotrowicz
Copy link
Contributor Author

@AndrewKushnir all comments addressed, sorry for the sea of silly mistakes! 😓

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@dario-piotrowicz looks great, thanks! Just left one minor comment.

@AndrewKushnir
Copy link
Contributor

@dario-piotrowicz FYI I've also restarted the CI and it looks like some additional tests require updates (due to the error message change). Thank you.

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit target: rc This PR is targeted for the next release-candidate action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit labels May 21, 2022
improve the error message developers get when adding a standalone
component in the TestBed.configureTestingModule's declarations array,
by making more clear the fact that this error originated from the
TestBed call

resolves angular#45923
@alxhub alxhub force-pushed the testbed-declatarions-standalone-error-message branch from a6c4a4b to c0abf67 Compare May 23, 2022 20:57
@angular-robot angular-robot bot requested a review from AndrewKushnir May 23, 2022 20:57
@alxhub
Copy link
Member

alxhub commented May 25, 2022

This PR was merged into the repository by commit a1e5aad.

alxhub pushed a commit that referenced this pull request May 25, 2022
)

improve the error message developers get when adding a standalone
component in the TestBed.configureTestingModule's declarations array,
by making more clear the fact that this error originated from the
TestBed call

resolves #45923

PR Close #45999
@alxhub alxhub closed this in a1e5aad May 25, 2022
@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 Jun 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cross-cutting: standalone Issues related to the NgModule-less world hotlist: error messages target: rc This PR is targeted for the next release-candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve TestBed error when standalone types are present in "declarations"

4 participants