Skip to content

Custom testing frameworks style #159

Closed

Description

@calebmer

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.

https://jlongster.github.io/prettier/#%7B%22content%22%3A%22describe('my%20test%20suite'%2C%20()%20%3D%3E%20%7B%5Cn%5Ctit('does%20something%20really%20long%20and%20complicated%20so%20I%20have%20to%20write%20a%20very%20long%20name%20for%20the%20test'%2C%20()%20%3D%3E%20%7B%5Cn%20%20%20%20%5Ctconsole.log('hello!')%3B%5Cn%20%20%20%20%7D)%3B%5Cn%20%20%5Cn%20%20%5Ctit('has%20name%20that%20runs%20right%20up%20to%20the%20edge%20so%20the%20arrow%20function%20is%20wierd'%2C%20()%20%3D%3E%20%7B%5Cn%20%20%20%20%5Ctconsole.log('world!')%3B%5Cn%20%20%20%20%7D)%3B%5Cn%7D)%3B%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Atrue%2C%22trailingComma%22%3Atrue%2C%22bracketSpacing%22%3Afalse%7D%7D

Activity

  1. jlongster commented on Jan 13, 2017

    @jlongster
    Member

    …and I’m not sure ( , ) => { ... } is valid JavaScript, but maybe I’m wrong?

    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.

  2. hawkrives commented on Jan 13, 2017

    @hawkrives
    Contributor

    …and I’m not sure ( , ) => { ... } is valid JavaScript, but maybe I’m wrong?

    There's a bug about this already (sorry, not sure which one, I'm mainly following PRs right now)

    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.

  3. added
    status:needs discussion
    on Jan 13, 2017
  4. calebmer commented on Jan 13, 2017

    @calebmer
    Author

    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:

    • Jest and Jasmine
      • describe
      • it
      • it.only
      • it.skip
      • xdescribe
      • fdescribe
      • xit
      • fit
      • test
    • Ava
      • test the recommended name, but it is imported so it can actually be whatever the user wants.
      • test.cb
      • test.serial
      • test.only
      • test.skip
      • test.todo
      • test.failing
      • test.serial.only, test.only.serial, etc (modifiers can be chained).
    • Mocha
      • describe
      • describe.only
      • describe.skip
      • it
      • it.only
      • it.skip
      • suite
      • suite.only
      • suite.skip
      • test
      • test.only
      • test.skip
      • Test names may also be required and named to whatever one wants.
    • Tape
      • test recommended name, but it is required so it may be named whatever the user wants.
      • test.skip
      • test.only

    Given 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 😉

  5. vjeux commented on Jan 13, 2017

    @vjeux
    Contributor

    CallExpression(StringLiteral, FunctionExpression/ArrowFunction)

    could be a good heuristic. I'm also seeing this trigger a lot in the nuclide codebase

  6. jlongster commented on Jan 13, 2017

    @jlongster
    Member

    Oh really? Ok, if multiple people see this in large codebases we can definitely make it a formal pattern.

  7. jlongster commented on Jan 13, 2017

    @jlongster
    Member

    Thanks for the list of name, btw, that's super comprehensive!

  8. sophistifunk commented on Jan 17, 2017

    @sophistifunk

    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?

  9. jlongster commented on Jan 18, 2017

    @jlongster
    Member

    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

  10. vjeux commented on Jan 19, 2017

    @vjeux
    Contributor
          if (
            parent &&
            parent.type === "ExpressionStatement" &&
            n.callee.type === "Identifier" &&
            n.arguments.length === 2 &&
            (n.arguments[0].type === "StringLiteral" ||
              n.arguments[0].type === "Literal") &&
            n.arguments[1].type === "ArrowFunctionExpression" &&
            !n.arguments[1].async &&
            !n.arguments[1].rest &&
            !n.arguments[1].predicate &&
            !n.arguments[1].returnType &&
            n.arguments[1].params.length === 0) {
    
            let identifier;
            let body;
            let i = 0;
            path.each(function(arg) {
              if (i === 0) {
                identifier = print(arg);
              }
              if (i === 1) {
                body = arg.call(print, "body");
              }
              i++;
            }, "arguments");
    
            return concat([
              path.call(print, "callee"),
              "(",
              identifier,
              ", ",
              "() => ",
              body,
              ")"
            ])
          }

    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.

  11. added a commit that references this issue on Mar 1, 2017
    a4bab0c
  12. vjeux commented on Mar 1, 2017

    @vjeux
    Contributor

    I found an easier way: #842

  13. added a commit that references this issue on Mar 3, 2017
    bf287a2
  14. added a commit that references this issue on Mar 3, 2017
    184382d
  15. karl commented on Mar 8, 2017

    @karl
    Collaborator

    It looks like Prettier is currently whitelisting test and it.

    We still have a number of cases in our codebase where a Jasmine describe block becomes long enough to cause it to wrap.

    Would there be interest in adding describe to the whitelist?

  16. vjeux commented on Mar 8, 2017

    @vjeux
    Contributor

    @karl describe sounds fine to me, could you send a pull request?

  17. karl commented on Mar 8, 2017

    @karl
    Collaborator
  18. RemyRylan commented on Jun 14, 2017

    @RemyRylan

    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_length option), or the max-len rule in ESLint.

    I'd personally prefer to strictly enforce 80 chars whenever possible and format my tests differently. Such as:

    it(
      'some-long-test-description',
      () => {
       // Test code here
      }
    )

    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 ?

  19. calebmer commented on Jun 14, 2017

    @calebmer
    Author
  20. RemyRylan commented on Jun 14, 2017

    @RemyRylan

    @calebmer thanks for the suggestion. I decided to just go for it, implemented eslint-plugin-prettier and eslint-config-prettier, plus removed the max-len option from my ESLint config and so far seems pretty good. Thank you!

  21. ncknuna commented on Oct 28, 2017

    @ncknuna

    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:

        it('handles some case and uses sinon', sinonTest(function () {
            // stuff
        }));
    
  22. added
    locked-due-to-inactivity
    on Jul 6, 2018
  23. locked as resolved and limited conversation to collaborators on Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivitystatus:needs discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions