-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding serializer to remove internal angular attributes from the snapshots #97
Conversation
This is absolutely not final changes. It is just PR for showing the general idea of what I suggest. Let's say I created this for some discussion. Technical stuff is really easy so we will fix it in a few moments. |
<div | ||
class="kek" | ||
> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this formatting. Do you mind adding some format tool like prettier or stuff?
Do you know any formatting tool for jest snapshots? How is Jest doing this? They might have serializer for that but I can't find it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They seem to use pretty-format with a custom React plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @sharikovvladislav , @barretodavid,
There is a solution for this. Can you guys please check #86 discussion ?
...src/app/a-bit-more-complex-component/__snapshots__/a-bit-more-complex.component.spec.ts.snap
Outdated
Show resolved
Hide resolved
@thymikee any thoughts? |
I'd rather like this serializer to work on actual values instead of regexing things around. Is there a way you could add this functionality to the current one? |
What do you mean by "actual values"? Do you want me to modify existing serializer? Now it is separated. Your serializer doing one thing. Another serializer extends yours by doing something extra. Do you think this approach is wrong? |
I think we should strive to have one that does the job well. By actual values I mean testing for objects with certain values as keys (e.g. |
okay, as you wish! what about general idea? what do you think about it? do you think it is good to have this feature in your tool? if everything is ok I will spend a lot of time for this. |
If you create a dedicated describe-block for your snapshot tests, you can run |
Is this PR going to be merged some day? I don't really know how and why but after updating to Angular 6 I found that a lot of |
Would love to in some form, but now it looks like the feedback wasn't addressed. Anybody willing to pick this up? |
Hey! Well, question is still opened.
I am not sure you need this @thymikee , thats why I don't work on this PR. |
Not me, but the project would benefit from stripping some Angular internals. |
So do you mind merging upgraded version of this PR? This is only one suggestion? #97 (comment) Anything else? I can fix this. |
My concern was #97 (comment) and actually invoking |
Ok, If you don't have any problems with general idea I will fix this 2 points and lets discuss it. |
Hi! I'm following this thread since, it is getting too much hard for me to find a way to get rid of the |
@isidro1983 well the ng-reflect stuff should be part of "devMode", so enabling prod mode removes anything related to it. We are sticking to the fully fledged snapshots now, as they can provide relevant information about what is given to inputs etc |
Any news? |
I will try to implement it this weekend.
пт, 19 апр. 2019 г. в 19:14, listepo-alterpost <notifications@github.com>:
… Any news?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#97 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABK4V7UJGJTH3Y5WEOL7WKLPRHVVBANCNFSM4EFXPQNA>
.
|
b342224
to
ba496a4
Compare
@@ -4,7 +4,6 @@ exports[`SimpleComponent snapshots 1`] = ` | |||
<app-simple> | |||
<p> | |||
simple works! | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay that I removed that empty lanes from existing snapshots? it is because of added filter function in serializer.
Done. @thymikee what do you think? |
In general:
@thymikee Probably, you want to add this behind some configuration flag? What do you think? Also, probably somebody want to see debug attributes in snapshots. For example from message above:
So may be it is better to apply this feature behind some config flag. |
Angular v8 is almost there, I guess we can merge and hold for release once Angular v8 releases then we can release after that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is still missing in my opinion is a test where a kek
-attribute-content would collide with the angular naming pattern, like kek="_ngcontent"
, but it will still be rendered, as it is not the attribute name conflicting with the patterns.
Also pure text content like <span>ng-reflect-whatever</span>
could be included to test edge cases.
We should also give the users a fallback-possibility to use the old serializer in case they still want to keep the old serializing behavior. So this PR could be implemented in a second serializer. If it is worth it, the two serializers can share common code.
As long as we do not change the current serializer behavior, there is no need for a breaking change. The new serializers could be made default at a later point in time.
Another thing that does not seem to be discussed here is class-names in components. They get indexed and each time the test runs in a different order, the indeces are different, e. g. class="ng-tns-c1-1"
.
Angular also adds attributes to inner non-component html elments like through directives and animations. I think these are not handled by this serializer, but I might be wrong.
it('snapshot test', () => { | ||
expect(fixture).toMatchSnapshot(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component, test file and the test suite should not just be named "a bit more complex component", we have also other tests than the serializers, so it should definitely explain, that it is a serializer-test.
You can name it something like "complex templating component" or name what is complex about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this code was wrote very long time ago, but still... It was "copy-pasted" from other specs in example project.
example/src/app/calc/calc.component.spec.ts
:
it('should snap', () => {
expect(fixture).toMatchSnapshot();
});
example/src/app/simple/simple.component.spec.ts
:
it('snapshots', () => {
expect(fixture).toMatchSnapshot();
})
And stuff...
I understand your point. Is it critical? If yes, other code have to be refactored too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Still I think "SimpleComponent" alone is clear what to expect in the component. "ABitMoreComplexComponent" will always only stand in comparison to "SimpleComponent", which is not as good.
I would recommend e. g. "MediumComplexComponent"
AngularSnapshotSerializer.js
Outdated
@@ -29,6 +29,17 @@ const print = (val, print, indent, opts, colors) => { | |||
.map(node => Array.from(node.renderElement.childNodes).map(print).join('')) | |||
.join(opts.edgeSpacing); | |||
|
|||
|
|||
const nodesWithoutDebugInformation = nodes | |||
.split('\n') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if opts.edgeSpacing
(see line 30) is not set to \n
? I think this might not work anymore.
Better make your transformations before the html gets joined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, I can just split and join lines by opts.edgeSpacing
? What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 30 the join is applied. If you use the nodes without joining them there you do not have to split them.
Your join afterwards should use opts.edgeSpacing
then.
@wtho very good points. And actually my code does not handle such cases it all. Such text will be removed.
Can you give some examples? Especialy animations. TODO:
|
Can you give some example of such behavior? I tried to get this. Here is the example: click me. I can not see any indexes in class list. |
I build a small example with a directive and an animation, but luckily both do not add any attributes to the html, they both just add more classes. https://stackblitz.com/edit/angular-hqvsrr?file=src%2Fapp%2Fapp.component.html <p _ngcontent-xqo-c12 class="ng-tns-c12-0 test" appsomedirective>
directive
</p>
<p _ngcontent-xqo-c12 class="ng-tns-c12-0 ng-trigger ng-trigger-triggerName" style>
animation
</p>
<p _ngcontent-xqo-c12>
text
</p> Still all children get the same The current serializer does not render non-angular component html, it lets This is just a suggestion and open for discussion. EDIT:
Yeah I remember they are added through the animations module. As soon as you add |
So do you suggest to remove such technical (for animations, like
Sorry, I didn't get. How this paragraph relates to my PR? What exactly you want me to do in this PR?
Also, lets discuss this option. This have to be done in this PR. We don't want breaking changes after implementing this feature, do we? So this feature has to be hidden behind some configuration flag. By default, this feature will be disabled. If people like this feature, default value of that configuration may be changed in some major version in the future. UPD:
I thought we could just optionally import prettier (according to this option https://jestjs.io/docs/en/configuration#prettierpath-string) and if it exists, just pass generated html (by lib serializers) throught it. Am I missing something? |
Done. |
i think all good to go |
03e6786
to
33b1827
Compare
@thymikee could you please have a look? By putting it as an optional serializer for now we can immediately publish a new minor version and do not have to hold it back. Also this PR fixes some issue with CI that we did not stumble upon before: we cache So changes in the root folder were not respected by the example app, as it used a previously cached version of This is now fixed by not using cache in the For me everything looks good in this PR 👍 |
Gonna have a look at this tomorrow. I'm so impressed by the amount of work you all put into this! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks way better now. Left some comments to address :)
README.md
Outdated
@@ -1,4 +1,4 @@ | |||
# jest-preset-angular | |||
AngularSnapshotSerializer# jest-preset-angular |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Done.
README.md
Outdated
@@ -69,6 +69,7 @@ module.exports = { | |||
}, | |||
transformIgnorePatterns: ['node_modules/(?!@ngrx)'], | |||
snapshotSerializers: [ | |||
'jest-preset-angular/AngularNoNgAttributesSnapshotSerializer.js', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not included by default yet, please revert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
example/package.json
Outdated
@@ -8,7 +8,7 @@ | |||
"build": "ng build", | |||
"test": "jest", | |||
"test:watch": "jest --watch", | |||
"test:ci": "jest --runInBand", | |||
"test:ci": "jest --runInBand --no-cache", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why no-cache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wtho looks like it was your change... do we still need it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be removed!
@@ -0,0 +1,34 @@ | |||
/* tslint:disable:no-unused-variable */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
future notice: we should move to eslint with TS support
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah!
But I would suggest to wait for Angular to make the official move to eslint, as this is still an Angular app example (might come in v8?).
Also see the official tslint -> eslint issue
example/src/app/a-bit-more-complex-component/a-bit-more-complex.component.ts
Outdated
Show resolved
Hide resolved
Also, please make sure CI is green (looks like something is leaking) |
33b1827
to
1617874
Compare
I'll fix the CI later 👍 |
1617874
to
bf05f6f
Compare
bf05f6f
to
a582354
Compare
How can I find out about leaks in the test runs? I ran the tests and they exceeded 10 minutes without output on CircleCI. Then I added the The only change between the test runs was: - yarn test:ci
+ yarn test:ci --detectOpenHandles
- yarn test:coverage
+ yarn test:coverage --detectOpenHandles Unfortunately there is no information about possible leaks in the output. |
README.md
Outdated
@@ -84,7 +84,8 @@ module.exports = { | |||
- `"moduleFileExtensions"` – our modules are TypeScript and JavaScript files | |||
- `"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex | |||
- `"setupFilesAfterEnv"` – this is the heart of our config, in this file we'll setup and patch environment within tests are running | |||
- `"transformIgnorePatterns"` – unfortunately some modules (like @ngrx ) are released as TypeScript files, not pure JavaScript; in such cases we cannot ignore them (all node_modules are ignored by default), so they can be transformed through TS compiler like any other module in our project. | |||
- `"transformIgnorePatterns"` – unfortunately some modules (like @ngrx) are released as TypeScript files, not pure JavaScript; in such cases we cannot ignore them (all node_modules are ignored by default), so they can be transformed through TS compiler like any other module in our project. | |||
- `"snapshotSerializers"` - array of serializers which will be applied to snapshot the code. Note: by default angular adds some angular-specific attributes to the code (like `ng-reflect-*`, `ng-version="*"`, `_ngcontent-c*` etc). This package provides serializer to remove such attributes. This makes snapshots cleaner and more human-readable. To remove such specific attributes use `AngularNoNgAttributesSnapshotSerializer` serializer. You need to add `AngularNoNgAttributesSnapshotSerializer` serializer manually (like in `example` application in `package.json`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a link to the exact lines in package.json?
Feel free to merge it @wtho :) |
Looking forward to test this in real projects 👍 |
@listepo-alterpost @barretodavid @isoden @iam4x @Lykathia @jfcere Any feedback? |
I won't be able to check for a couple weeks now, unfortunately. Looking forward to it tho! |
Hi!
I added serializer to remove the internal angular attributes from the result snapshots.
This is good because snapshots become more human readable. This is also good because sometimes angular renderer changes the indexes of that internal attributes.
The questions: