-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feature(jest-each): Enable comments inside .each template literal table #8717
Conversation
0198f3e
to
174afbc
Compare
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.
Good idea and implementation LGTM as well :)
|
||
each` | ||
a | b | expected | ||
// ${1} | ${1} | ${0} |
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 about commenting out a title line? Not sure if there's a good use case but should definitely have a test case to specify how it behaves.
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.
Throw on missing headngs (should be in a different PR?): 065155a.
aff5aaa#diff-a6c0398707b866880e5098b277508a6dR474
Error message could probably be improved.
each` | ||
a | b | expected | ||
${0} | ${0} | ${0} // ignores trailing comment | ||
${1} | ${1} | ${2} /* ignores second comment */ |
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.
Maybe also a test for /* /* */
just to make sure the regex "lexer" works correctly
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 think I've added this in: fffad4d.
Hey @chrisblossom thanks for the PR! I'm not sure I'm convinced by adding support for comments in a test template literal, would you mind expanding on your motivation more? If one of the benefits is to disable a test row with a comment, I think it might make more sense to support an official API (same could be said for Currently trailing comments are supported by using the test.each`
a | b | expected
${0} | ${1} | ${1 /* 0 + 1 == 1 */}
${1 /* One */} | ${1} | ${2}
`('add $a + $b == $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
}); As for comments in the title, I'd hope that the title is already expressive enough to not require a comment. Would love to hear your thoughts on this 😄 |
@mattphillips thanks for the reply! When I was initially replying I put my actual use cases in as tests and found some pretty big errors causing me to refactor how it works.
The limited times I've used test.each`
a | b | expected
// test a subset of issues
${0} | ${1} | ${1}
// test another subset of issues
${1} | ${1} | ${2}
${2} | ${2} | ${4} // fix for: https://github.com/facebook/jest/pull/8717
${3} | ${3} | ${6} // some random note about this test
// enable later
// ${1} | ${1} | ${2}
// ${1} | ${1} | ${2}
`('add $a + $b == $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
}); Currently the above code ends up looking like this when I'm developing: // ${1} | ${1} | ${2}
// ${2} | ${2} | ${4}
// ${3} | ${3} | ${6}
// ${1} | ${1} | ${2}
// ${1} | ${1} | ${2}
test.each`
a | b | expected
${0} | ${1} | ${1}
`('add $a + $b == $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
}); Often times I decide not to use
I think an official API would be nice, but I don't think it has to be one or the other. Comments here look natural and make code work like the rest of javascript, also have other use cases mentioned above. Even with Also, at least with Webstorm and VSCode, the editor's shortcut to comment out lines work inside
I personally do not think that is very good developer UX / doesn't look very nice.
An assumption of people's code, but yes it should be. |
Hey @chrisblossom thanks for the reply. I'm still not convinced by the motivations, fundamentally when you use While adding I think by adding an official API for focussing/skipping a row it will make developing these sorts of tests a much nicer experience. As for the subheading comment, this probably shouldn't be achieved at this level of test but you should rather utilise a different describe block. Alternatively you could use the Array table syntax to allow you to have comments anywhere you like. |
When I first put this PR in I didn't fully understand how tagged template literals I have since refactored this to no longer guess and correctly utilize the I am now confident this PR is ready to be merged if accepted. I don't think I'll push anymore changes unless requested.
I'm not sure I understand this comment. Comments are valid JS, I'm not proposing adding an operator that is not JS. Do you think things like
Why does that matter that comments aren't built into tagged template literals? This is about giving better developer UX (which it seems is the very point of This is not a new concept. styled-components and emotion are the biggest libraries that use tagged template literals as their main API that I know of and both support JS comments (and have a plugins that enables proper syntax highlighting as well). I think I'm the first person to open an issue here about this because I don't think
This only helps with one of several use cases here, the others are important as well.
Isn't the point of Maybe you personally easily understand code without comments/empty lines, but I and many others have a much harder time without them. This feature is optional, nothing is forcing someone to add comments to thier
Yes, but there is a reason it is preferred to use the tagged template literal format. It is much nicer to read/understand especially in-combination with prettier. Better developer UX. Also, normally I could create a package outside of jest to do this as well, but because prettier is currently programmed to remove excess characters from In conclusion, I think this feature is complementary to |
I'm in favor of shipping this. Of course it's a bit of extra code to maintain, but it's one of those things that a user might try during development and than be happy that it just works. And if they know a bit of how JS works, they might even realize that jest-each has implemented this for them ;) |
Hey guys, I'm still yet to be convinced. The features being enabled here are:
If we take a closer look at these features, I'm in disagree that these features need enabling via comments.
I acknowledge that currently The difference with @chrisblossom I'm finding all of your arguments to be subjective to your opinion, I'd like you to please consider this and focus on tangible outcomes from this change. The fact is, that this change brings with it a lot of maintenance overhead. To illustrate this, currently To go back to my point of valid JS, I don't think we should be extending the As I say for now I am not convinced by this at all and do not see this being merged - I'd welcome any suggestions around a potential API for skipping/focussing test rows from within a template table. |
Of your three points, I viewed this as a solution for the first. |
Exactly this.
What kind of API would be acceptable to you? I first considered trying to make
Yes, but it is currently hacky and not very apparent what it is for. Is the comment talking about the value inside or the whole line? Even JSX has the ability to have comments outside of values.
Again, unless I am mistaken (and so is google when you search "jest each")
Your opinion...I would find them very relevant in my code and I am sure others would as well. I've decided multiple times against using
The same thing could have been said about
Outside of your last comment about lines added, I'd ask you to do the same. I think you are looking at this feature as it would apply to you personally and how you code. My "arguments" are focusing around developer UX and expectations, not implementation details. This is very much a tangible outcome. I find it concerning that instead of responding to my questions / comments, you disregard/undervalue all of them by saying everything is an opinion. Seems to me that you just fundamentally disagree with this feature because it isn't built into the language. I am just trying to get you to understand that this PR can help me and others write more-clear code/tests, quickly focus a single test using existing IDE shortcuts, and that there is a developer expectation of them working with other popular libraries allowing them (emotion and styled components).
This is completely taken out of context. The majority of the code added is from tests, which could be made into one/two tests if wanted. A lot of the tests were written because there was a lot of room for edge cases since I was guessing where comments were/where the data was. This is no longer the case with the current implementation. Funny enough that if these tests were able to be written in a Regarding your comment here, the same thing could have been said for adding You make me regret trying to create a complete PR that everyone could look at the tests and feel confident that it wouldn't break anyone's existing code. Have you actually taken a look at the code / tried to understand what it is doing? Feels like I am being gaslighted here...
You obviously do not see a benefit from this, but I do and I think others would too.
It doesn't break prettier. Whoever wrote the formatting code for prettier decided to remove characters that aren't The two features you mentioned test.each`
a | b | expected
// test a subset of issues
${0} | ${1} | ${1}
// test another subset of issues
${1} | ${1} | ${2}
${2} | ${2} | ${4} // fix for: https://github.com/facebook/jest/pull/8717
${3} | ${3} | ${6} // some random note about this test
`('add $a + $b == $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
}); The comment markers are not needed because as you pointed out earlier, it just a string. But they are very helpful because we are trained that I can't imagine the prettier folks will change this without the support of the jest team (although they might because as far as I know, prettier shouldn't remove code). If the biggest concern with this PR is the 130 (with spaces and comments) lines of code added, I'm fairly certain I could significantly reduce that by only supporting single line comments and removing support for I'm also planning on releasing this as a npm package because the implementation is not specific to Jest. (UPDATE: released as tagged-template-literal-comments) I assumed the Jest team would prefer the code to live inside the repo, but I can add the package instead, which would significantly reduce the lines changed to almost nothing... |
dad7318
to
068598c
Compare
Oh, having the comments removed by Prettier is pretty much a blocker to me. Since a lot of JS community use it, what's the point of adding a feature that's gonna cause confusion (some may run Prettier on pre-commit hook so they wouldn't even realize)?. We should first discuss this with them and find a common ground. cc @ikatyang On a personal note – I'm not a fan of making test.each`
a | b
0 | 1 | ${ /* comment */'' }
`
Also, please have in mind the performance implications. |
@chrisblossom I apologise if you feel I have shot this down too much - my bad! As the author of I think for future reference it would be worth raising an issue for a new feature as an RFC, this way no time is wasted on coding something that might not be accepted as a feature. In regards to the DSL my preference would be to keep it thin and not put semantics around comments. This is an API which we own, so if we do want to enable labelling (sub headings) then we should take some time and think about what that DSL looks like - for me it isn't a comment. As for the primary focus of adding comments to disable tests - I completely disagree with this as an approach. It is not a better DX, it actually goes against Jest's DX. By adding an official API to skip/focus tests the developer experience is improved as we will include those tests in the reporting outputted at the end of a test run. Going forward with this I think we should do several things:
Currently any additional text will cause
If we do these three steps then users will be able to receive reporting on skipped/focussed tests in This will result in minimal amount of code being added to enable the desired behaviours. @jeysal @thymikee @chrisblossom what do you guys think about this as a way forward? |
I don't understand point 2, maybe an example would help. |
@thymikee It shouldn't be hard to implement on the Prettier side if it only adds support for single-line comments. The reason why Prettier strips them is simply because we only expected the first TemplateElement (the non-expression part in the template string) to be meaningful. |
Sorry for the late reply...
No apology necessary. I know I didn't communicate very well either. I really do appreciate the time you've taken to respond, and your previous work creating
Although I did raise an issue before submitting this PR, it wasn't very thorough. Also, I personally don't always have time to work on open source and try to take advantage of the times I do. I try and contribute back as much as I can instead of always relying on maintainers.
For me, this is often a worse developer experience. When using In my experience, knowing what tests are skipped is only beneficial in CI / when completing a feature and should be handled via linting. Ignoring the feature to comment data out, I personally still think the // no comment marker necessary (https://github.com/facebook/jest/pull/8766)
test.each`
a | b | expected
test a subset of issues
${0} | ${1} | ${1}
test another subset of issues
${1} | ${1} | ${2}
${2} | ${2} | ${4} fix for: https://github.com/facebook/jest/pull/8717
${3} | ${3} | ${6} some random note about this test
`('add $a + $b == $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
}); // comment marker required (https://github.com/facebook/jest/pull/8717)
test.each`
a | b | expected
// test a subset of issues
${0} | ${1} | ${1}
// test another subset of issues
${1} | ${1} | ${2}
${2} | ${2} | ${4} // fix for: https://github.com/facebook/jest/pull/8717
${3} | ${3} | ${6} // some random note about this test
`('add $a + $b == $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
If wanted, I could remove FYI, editors can (and do) dim comments inside template literals (See: emotion and styled-components).
Definitely something to consider, although in practice I don't see this being an issue. I've sped this up quite a bit with a package I'll be releasing soon that uses regexes instead splitting all characters and looping through them.
I think this is a great idea regardless if this PR is merged or not. What are some ideas on how this would look?
Although I think this PR solves this in a better, cleaner way, this would at least allow users to add their own comments / additional new lines. Also, commenting out values could then be solved outside of |
Oh wow, I only just realized that Matt's PR that ignores everything |
What's the status here? Is it still needed after #8766 landed in v27? |
This PR is stale because it has been open 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This PR was closed because it has been stalled for 30 days with no activity. Please open a new PR if the issue is still relevant, linking to this one. |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
This PR allows comments inside of
jest.each
template literals.If this PR is accepted, prettier will need to be updated not to remove comments.
Closes #8638.
Test plan
Added several tests to
jest-each
.