Skip to content
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

Adds rule "no setup in describe" #147

Merged
merged 10 commits into from
Mar 2, 2018
Merged

Adds rule "no setup in describe" #147

merged 10 commits into from
Mar 2, 2018

Conversation

donabrams
Copy link

Setup for test cases in mocha should be done in before, beforeEach, or it blocks. Unfortunately there is nothing stopping you from doing setup directly inside a describe block and it's not immediately obvious setup should be inside a before or beforeEach.

This rule catches many cases where setup is accidentally done directly in a describe block and promotes the use of the hooks before and beforeEach.

This rule's code is a little more complex than I'd like because it does what it can to minimize processing time outside a describe block (Array.length check only).

This rule should never be activated by default because it blocks the use of dynamically generated tests.

@donabrams donabrams changed the title No setup in describe blocks Adds rule "no setup in describe" Feb 2, 2018
Copy link
Owner

@lo1tuma lo1tuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @donabrams and thank you for the contribution. Overall it looks like a good starting point, although it is quite hard to correctly detect setup code. I would say let’s start with this approach and see how it works in practice.

Fixes #38

.eslintrc Outdated
@@ -103,7 +103,7 @@
"comma-dangle": [ 2, "never" ],
"comma-spacing": [ 2, { "before": false, "after": true } ],
"comma-style": [ 2, "last" ],
"complexity": [ 2, 4 ],
"complexity": [ 2, 8 ],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don’t change this rule globally. It is ok to make exception from this rule, but then please only for those functions where it is really necessary via eslint-disable-comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was erroring in plugins other than this one, so I set it to the highest value other plugins required. I can bring this back down and add eslint-disable-line to the other plugins if wanted

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve fixed that problem in #149. So you just need to rebase from master.

@@ -11,6 +11,7 @@
* [no-nested-tests](no-nested-tests.md) - disallow tests to be nested within other tests
* [no-pending-tests](no-pending-tests.md) - disallow pending/unimplemented mocha tests
* [no-return-and-callback](no-return-and-callback.md) - disallow returning in a test or hook function that uses a callback
* [no-setup-in-describe](no-setup-in-describe.md) - disallow calling functions and dot operaters directly in describe blocks
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dot operators? Do you mean member expressions? If so, please also consider that member expression could be written as foo[bar].

Copy link
Author

@donabrams donabrams Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, yes, on it!

  • handle bracket notation (foo[bar])

});
```

Any setup directly in a `describe` is run before all tests execute. This is undesirable primarily for two reasons:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about top-level code outside of a describe?

Copy link
Author

@donabrams donabrams Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to handle this, but it could result in worse performance on non-test code (currently a .length check is all the overhead that is incurred outside a describe). Thoughts on that tradeoff?

Copy link
Author

@donabrams donabrams Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually I just don't know how to flag a file as having a describe with eslint. What's the perf on a hasDescribe(file) and can this even be done?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure why it should be necessary to check if there is a describe in a specific file, you could just look for setup code directly in the Program node.
Anyway I’m not sure if this is wanted, because most people probably prefer to have their require calls at the top of the file. So maybe we leave it for now as it is and if some one requests that feature we can implement this behind an option.


This rule reports all function calls and use of the dot operator (due to getters and setters) directly in describe blocks.

If you're using [dynamically generated tests](https://mochajs.org/#dynamically-generating-tests), you should disable this rule.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was just going to ask about this 😀 .
I think it is ok that this rule will only work for non dynamically generated tests.

DESCRIBE = 2,
PURE = 3;

function isPureN(node) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does N mean?

Copy link
Author

@donabrams donabrams Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node. I'll expand it.

  • expand N to Node

@@ -0,0 +1,89 @@
'use strict';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this has a different filename than the actual rule?

Copy link
Author

@donabrams donabrams Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, changed the name

  • rename this file

} ]
},
{
code: 'describe("", function () { a.b; });',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test for a['b']?

Copy link
Author

@donabrams donabrams Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • add test for foo[bar]

@donabrams
Copy link
Author

I've addressed everything I don't have questions about.

@lo1tuma lo1tuma added the feature label Mar 2, 2018
@lo1tuma
Copy link
Owner

lo1tuma commented Mar 2, 2018

Hi @donabrams, sorry for the late feedback. This looks almost good to me, only a rebase is missing and the removal of the .eslintrc changes 😉.

@donabrams
Copy link
Author

Alright, rebased and complexity "decreased"

Copy link
Owner

@lo1tuma lo1tuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thank you 🍻.

@lo1tuma lo1tuma merged commit 2a55b86 into lo1tuma:master Mar 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants