-
Notifications
You must be signed in to change notification settings - Fork 236
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
feat(rule): add valid-describe rule #57
Conversation
9895ab6
to
fa010fe
Compare
index.js
Outdated
@@ -1,5 +1,4 @@ | |||
'use strict'; |
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'm not sure why lint-staged is removing this as part of the eslint --fix
step in my local environment.
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.
you probably have an eslintrc
file in $HOME
or something. Try adding root: true
in the eslintrc.js
in this repo
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.
Thanks for the tip! I had an unexpected .eslintrc
file one level up from this repo on my machine. Removed that and was able to see my local linting match up with what Travis CI is reporting. 👍
Fitting, my first eslint rule ever was handling a callback in I'm not sure how we could figure out nr 2, though. You can take a look at #42, there is some promise detection in it. |
fa010fe
to
d9a8208
Compare
I'll work towards including the two other use cases in this PR - thanks for the suggestions on other implementations to look at. We can figure out a proper name for this rule before merging, but I won't worry much about that right now. |
I'm still working on use case number 2, but is there ever a reason to return within a // invalid
describe('foo', () => {
return Promise.resolve().then(() => {
it('breaks', () => {
throw new Error('Fail')
})
})
}) Looking at this code, I can update to check that the returned value is a Promise (by looking at #42, as suggested), but I am wondering if there's a code example where returning anything within the body of a |
886ce50
to
286c8ba
Compare
I'm still unsure about the previous comment. Also, is there ever a reason to call // we will warn on the `async` describe callback function
describe('foo', async () => {
// should we also warn that we are calling `await` here
// or is the `async` warning above enough?
await something()
it('does something', () => {
// ...
})
}) |
You have to call |
7ea9894
to
5cea64a
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.
This is now ready for review. 👍
@@ -0,0 +1,53 @@ | |||
# Enforce valid `describe()` callback (valid-describe) |
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 settled on calling this valid-describe
since this addresses a few different cases. This follows the convention of the valid-expect
rule. Open to other naming suggestions too!
rules/valid-describe.js
Outdated
@@ -0,0 +1,66 @@ | |||
'use strict'; | |||
|
|||
const describeAliases = Object.assign(Object.create(null), { |
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.
This was taken from no-identical-title
:
eslint-plugin-jest/rules/no_identical_title.js
Lines 3 to 9 in 5e5ceba
const describeAliases = Object.assign(Object.create(null), { | |
describe: true, | |
'describe.only': true, | |
'describe.skip': true, | |
fdescribe: true, | |
xdescribe: true, | |
}); |
I can extract an isDescribe(node)
function, move it to util.js
and import it into no-identical-title
and this rule, if that refactor would be helpful.
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, that sounds good
rules/valid-describe.js
Outdated
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (node && isDescribe(node)) { |
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 node
ever be falsy here? I'm not sure I need to include this node &&
.
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.
not sure. Seems weird if it can be?
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.
Does seem weird, will remove that check
rules/valid-describe.js
Outdated
if (hasParams(callbackFunction)) { | ||
context.report({ | ||
message: 'Unexpected argument in describe callback', | ||
node: callbackFunction.params[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.
This only warns for the first param - perhaps this should warn for all passed in params?
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, that makes sense.
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.
Will update with a test case for multiple params in a describe()
callback
needs a rebase |
0e913bc
to
5e32962
Compare
Rebased and force pushed up. |
], | ||
invalid: [ | ||
{ | ||
code: 'describe("foo", async () => {})', |
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.
as a follow up, mind adding a rule that describe(() => {})
is invalid as well?
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.
For sure, I'll open an issue to track that suggestion. 👍
Resolves #18
This rule validates that the second parameter of a
describe()
function is a callback function. This callback function:return
statementsFile naming is kebab-case, per the discussion in #60.