jest-preset-angular: InlineHtmlStripStylesTransformer is not strict enough
Current Behavior
Any object literals have their property assignments transformed thus causing source code such as:
console.log({
styles: ['styles.css']
})
to print out
{
styles: []
}
Expected Behavior
Only object literals passed to @angular/core’s @Component decorator should have it’s property assignments transformed.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 15 (2 by maintainers)
Ok, so one more thought I just had,
templateUrlis actually more important to be transformed thanstylesto be removed. WithouttemplateUrlbeing transformed totemplate, it will not even load.styleson the other hand is just removed for performance issues (correct me if I’m wrong), as we assume styles are usually not tested in jest, that is better done in e2e frameworks.Proposal
We could also think about handling them differently, although this might complicate the code unnecessarily.
templateUrl:Componentgets imported from@angular/coreorTestBedgets imported from@angular/core/testingstyles:This would seem more practical to me as
stylesis also used in more contexts thantemplateUrl.Yeah, I am almost done with the PR, will submit it this weekend.
It’s not really an error, it is an intrusive caveat that kept the transformer simple, but can create side-effects.
I agree, the second example is not very important.
The third and fourth are the problematic ones.
@electrichead The AST transformer is written in TypeScript, but it works with AST objects. AST is a representation of the code text, not of the evaluated objects.
So this situation
is definitely a problem, as in this example each line is an independent object. References are made when JavaScript is interpreted, not when the AST transformer is executed. The
stylesassignment is not inside the decorator and therefore we would have to search for thecomponentConfigvariable in the whole file, maybe even in other files if it is imported.Yeah, this caveat is known and discussed
Problem is, that theoretically someone might rename the annotation, or pull an object into the annotation, that is not declared inside the annotation itself.
The AST transformer does not know if an object is actually the Angular Component Decorator or if an object with
stylesandtemplateUrlis actually a component configuration, it only knows in what relation code pieces are and how they are named.Consider these examples:
We should discuss approaches and their caveats before implementing it.
Edit: fixed linked issues