Repository navigation
Description
Is there a way to support long strings for test frameworks?
Currently the following code:
describe('my test suite', () => {
it('does something really long and complicated so I have to write a very long name for the test', () => {
console.log('hello!');
});
it('has name that runs right up to the edge so the arrow function is wierd', () => {
console.log('world!');
});
});Transforms to:
describe('my test suite', () => {
it(
'does something really long and complicated so I have to write a very long name for the test',
() => {
console.log('hello!');
}
);
it('has name that runs right up to the edge so the arrow function is wierd', (
) =>
{
console.log('world!');
});
});If you add trailing comma support it looks like:
describe('my test suite', () => {
it(
'does something really long and complicated so I have to write a very long name for the test',
() => {
console.log('hello!');
},
);
it('has name that runs right up to the edge so the arrow function is wierd', (
,
) =>
{
console.log('world!');
});
});…and I’m not sure ( , ) => { ... } is valid JavaScript, but maybe I’m wrong? I would expect it to keep the original formatting in this case.
It would be nice to have custom support for this case. I think the way to detect this is if there is a function call with two parameters. A string literal in the first position and a function in the second. Other variations of this pattern may include:
suite('suite name', () => { ... });
test('test name', () => { ... });
it.only('test name', () => { ... });
it('test name', done => { ... });
it('test name', function () { ... });Off the top of my head I can’t think of many other patterns in JavaScript that have function calls with a string literal as the first parameter and a function as the second. And if such patterns did exist I don’t think this change would hurt them.
Activity
jlongster commentedon Jan 13, 2017
There's a bug about this already (sorry, not sure which one, I'm mainly following PRs right now)
I'm open to this if other people run into this as well. I'd like to get at least a few people 👍 these patterns so we don't implement a ton of them for just single people.
I'd like to be pretty restrictive about this one though, maybe even specifically hardcoding a list of testing function names that will pass (like
it). I think just detecting a string and function is very generic and will have a lot of false positives. In many cases the wrapping behavior won't trigger because the string is very long, but because the function call is already indented far enough that even a short string triggers it. We want things to break consistently as much as possible.I don't think I would run into it because I don't usually write such long strings to name my tests :) Do you really have enough tests that have such long strings to require this? If it's just a few tests here or there, I don't think it warrants this pattern. But I'd like to here what others think.
hawkrives commentedon Jan 13, 2017
That's the bug I meant to talk about in #118. I think I've seen it in one other issue, too.
I don't have an opinion on the long test names, although that's exactly how I found the bug as well.
calebmer commentedon Jan 13, 2017
Not many of my tests wrap because of this, but there are generally 3–4 tests per file that go over and would need support for this. If you want to use identifier names to determine if a function qualifies for the pattern, here is a potential list:
describeitit.onlyit.skipxdescribefdescribexitfittesttestthe recommended name, but it is imported so it can actually be whatever the user wants.test.cbtest.serialtest.onlytest.skiptest.todotest.failingtest.serial.only,test.only.serial, etc (modifiers can be chained).describedescribe.onlydescribe.skipitit.onlyit.skipsuitesuite.onlysuite.skiptesttest.onlytest.skiptestrecommended name, but it is required so it may be named whatever the user wants.test.skiptest.onlyGiven the complexity of this list I personally like the string-literal-and-one-or-none-argument-function of checking for this pattern because it feels like even in non-test scenarios this would be non-surprising and perhaps even expected?
On another note, keep up the good work you’re doing great! There’s probably going to be a lot of PRs and issues in the first couple of weeks, but if anything that’s validation that you made something that people actually need 😊
Thanks so much for your work! If you need breaks take them. People who really care will send a PR 😉
vjeux commentedon Jan 13, 2017
CallExpression(StringLiteral, FunctionExpression/ArrowFunction)
could be a good heuristic. I'm also seeing this trigger a lot in the nuclide codebase
jlongster commentedon Jan 13, 2017
Oh really? Ok, if multiple people see this in large codebases we can definitely make it a formal pattern.
jlongster commentedon Jan 13, 2017
Thanks for the list of name, btw, that's super comprehensive!
sophistifunk commentedon Jan 17, 2017
A list of function names embedded within prettier itself seems both fragile and also likely to lead to anybody experiencing a false positive to have a bad-time. Perhaps some sort of in-file comment along the lines of
eslint-disable?jlongster commentedon Jan 18, 2017
I think I was convinced that the metric posted in @vjeux's comment is a better approach for the reasons you mentioned. There is an issue for an escape hatch: #120
vjeux commentedon Jan 19, 2017
I have the beginning of a working solution for this but this is getting kind of crazy. I'm starting to get unsure that this is worth the complexity. Not sure.
Do not break long it calls
vjeux commentedon Mar 1, 2017
I found an easier way: #842
Do not break long it calls
Do not break long it calls (#842)
karl commentedon Mar 8, 2017
It looks like Prettier is currently whitelisting
testandit.We still have a number of cases in our codebase where a Jasmine
describeblock becomes long enough to cause it to wrap.Would there be interest in adding
describeto the whitelist?vjeux commentedon Mar 8, 2017
@karl describe sounds fine to me, could you send a pull request?
karl commentedon Mar 8, 2017
RemyRylan commentedon Jun 14, 2017
Is anyone open to an option for disabling the whitelisting? Running prettier actually forces tests with long descriptions to go over 80 chars, which triggers linting errors in other tools like .editorconfig (using the non-standard
max_line_lengthoption), or themax-lenrule in ESLint.I'd personally prefer to strictly enforce 80 chars whenever possible and format my tests differently. Such as:
I'm willing to submit a pull request if the team is open to adding the option. If so, what do we call it?
printWidthWhitelist: false?calebmer commentedon Jun 14, 2017
@jrylan I’d recommend disabling max line lengths since Prettier will handle line length in most cases, an eslint rule on top feels redundant. Otherwise just split the string in half with a
+and Prettier will use its default formatting:https://prettier.github.io/prettier/#%7B%22content%22%3A%22test('test%20with%20a%20really%20long%20description%20that%20definetly%20'%20%2B%20'goes%20past%20the%20character%20limit%2C%20does%20it%20break%3F'%2C%20()%20%3D%3E%20%7B%5Cn%20%20%2F%2F%20yes%5Cn%7D)%3B%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3A%22none%22%2C%22bracketSpacing%22%3Atrue%2C%22jsxBracketSameLine%22%3Afalse%2C%22parser%22%3A%22babylon%22%2C%22semi%22%3Atrue%2C%22useTabs%22%3Afalse%2C%22doc%22%3Afalse%7D%7D
RemyRylan commentedon Jun 14, 2017
@calebmer thanks for the suggestion. I decided to just go for it, implemented
eslint-plugin-prettierandeslint-config-prettier, plus removed themax-lenoption from my ESLint config and so far seems pretty good. Thank you!ncknuna commentedon Oct 28, 2017
Any chance we could also handle wrapped functions as the second argument (at the most restrictive, check if the second argument is a call which takes a function as its only argument, or more loosely just allowing calls in the second argument)?
We're using sinon and wrapping our tests with
sinon-test(https://github.com/sinonjs/sinon-test), which the current logic doesn't support, like: