fix(core): improve TestBed declarations standalone error message#45999
fix(core): improve TestBed declarations standalone error message#45999dario-piotrowicz wants to merge 1 commit intoangular:mainfrom
Conversation
17d1c6d to
150ca50
Compare
There was a problem hiding this comment.
@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).
|
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 🙂 |
@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). |
150ca50 to
bb25a4a
Compare
AndrewKushnir
left a comment
There was a problem hiding this comment.
@dario-piotrowicz thanks for the update! 👍 Just left a few comments, please take a look when you get a chance. Thank you.
|
@AndrewKushnir all comments addressed, sorry for the sea of silly mistakes! 😓 |
AndrewKushnir
left a comment
There was a problem hiding this comment.
@dario-piotrowicz looks great, thanks! Just left one minor comment.
|
@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. |
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
a6c4a4b to
c0abf67
Compare
|
This PR was merged into the repository by commit a1e5aad. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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?
Issue
Issue Number: #45923
Does this PR introduce a breaking change?
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 🙂