-
Notifications
You must be signed in to change notification settings - Fork 237
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Enforce valid `describe()` callback (valid-describe) | ||
|
||
Using an improper `describe()` callback function can lead to unexpected test errors. | ||
|
||
## Rule Details | ||
|
||
This rule validates that the second parameter of a `describe()` function is a callback function. This callback function: | ||
|
||
* should not be [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) | ||
* should not contain any parameters | ||
* should not contain any `return` statements | ||
|
||
The following `describe` function aliases are also validated: | ||
|
||
* `describe` | ||
* `describe.only` | ||
* `describe.skip` | ||
* `fdescribe` | ||
* `xdescribe` | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
// Async callback functions are not allowed | ||
describe('myFunction()', async () => { | ||
// ... | ||
}); | ||
|
||
// Callback function parameters are not allowed | ||
describe('myFunction()', done => { | ||
// ... | ||
}); | ||
|
||
// | ||
describe('myFunction', () => { | ||
// No return statements are allowed in block of a callback function | ||
return Promise.resolve().then(() => { | ||
it('breaks', () => { | ||
throw new Error('Fail'); | ||
}); | ||
}); | ||
}); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
describe('myFunction()', () => { | ||
it('returns a truthy value', () => { | ||
expect(myFunction()).toBeTruthy(); | ||
}); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../..').rules; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
}, | ||
}); | ||
|
||
ruleTester.run('valid-describe', rules['valid-describe'], { | ||
valid: [ | ||
'describe("foo")', | ||
'describe("foo", function() {})', | ||
'describe("foo", () => {})', | ||
'xdescribe("foo", () => {})', | ||
'fdescribe("foo", () => {})', | ||
'describe.only("foo", () => {})', | ||
'describe.skip("foo", () => {})', | ||
` | ||
describe('foo', () => { | ||
it('bar', () => { | ||
return Promise.resolve(42).then(value => { | ||
expect(value).toBe(42) | ||
}) | ||
}) | ||
}) | ||
`, | ||
` | ||
describe('foo', () => { | ||
it('bar', async () => { | ||
expect(await Promise.resolve(42)).toBe(42) | ||
}) | ||
}) | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'describe("foo", async () => {})', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as a follow up, mind adding a rule that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure, I'll open an issue to track that suggestion. 👍 |
||
errors: [{ message: 'No async describe callback', line: 1, column: 17 }], | ||
}, | ||
{ | ||
code: 'describe("foo", async function () {})', | ||
errors: [{ message: 'No async describe callback', line: 1, column: 17 }], | ||
}, | ||
{ | ||
code: 'xdescribe("foo", async function () {})', | ||
errors: [{ message: 'No async describe callback', line: 1, column: 18 }], | ||
}, | ||
{ | ||
code: 'fdescribe("foo", async function () {})', | ||
errors: [{ message: 'No async describe callback', line: 1, column: 18 }], | ||
}, | ||
{ | ||
code: 'describe.only("foo", async function () {})', | ||
errors: [{ message: 'No async describe callback', line: 1, column: 22 }], | ||
}, | ||
{ | ||
code: 'describe.skip("foo", async function () {})', | ||
errors: [{ message: 'No async describe callback', line: 1, column: 22 }], | ||
}, | ||
{ | ||
code: ` | ||
describe('sample case', () => { | ||
it('works', () => { | ||
expect(true).toEqual(true); | ||
}); | ||
describe('async', async () => { | ||
await new Promise(setImmediate); | ||
it('breaks', () => { | ||
throw new Error('Fail'); | ||
}); | ||
}); | ||
});`, | ||
errors: [{ message: 'No async describe callback', line: 6, column: 27 }], | ||
}, | ||
{ | ||
code: ` | ||
describe('foo', function () { | ||
return Promise.resolve().then(() => { | ||
it('breaks', () => { | ||
throw new Error('Fail') | ||
}) | ||
}) | ||
}) | ||
`, | ||
errors: [ | ||
{ | ||
message: 'Unexpected return statement in describe callback', | ||
line: 3, | ||
column: 9, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
describe('foo', () => { | ||
return Promise.resolve().then(() => { | ||
it('breaks', () => { | ||
throw new Error('Fail') | ||
}) | ||
}) | ||
describe('nested', () => { | ||
return Promise.resolve().then(() => { | ||
it('breaks', () => { | ||
throw new Error('Fail') | ||
}) | ||
}) | ||
}) | ||
}) | ||
`, | ||
errors: [ | ||
{ | ||
message: 'Unexpected return statement in describe callback', | ||
line: 3, | ||
column: 9, | ||
}, | ||
{ | ||
message: 'Unexpected return statement in describe callback', | ||
line: 9, | ||
column: 11, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
describe('foo', async () => { | ||
await something() | ||
it('does something') | ||
describe('nested', () => { | ||
return Promise.resolve().then(() => { | ||
it('breaks', () => { | ||
throw new Error('Fail') | ||
}) | ||
}) | ||
}) | ||
}) | ||
`, | ||
errors: [ | ||
{ | ||
message: 'No async describe callback', | ||
line: 2, | ||
column: 23, | ||
}, | ||
{ | ||
message: 'Unexpected return statement in describe callback', | ||
line: 6, | ||
column: 11, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'describe("foo", done => {})', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument(s) in describe callback', | ||
line: 1, | ||
column: 17, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'describe("foo", function (done) {})', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument(s) in describe callback', | ||
line: 1, | ||
column: 27, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'describe("foo", function (one, two, three) {})', | ||
errors: [ | ||
{ | ||
message: 'Unexpected argument(s) in describe callback', | ||
line: 1, | ||
column: 27, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'describe("foo", async function (done) {})', | ||
errors: [ | ||
{ message: 'No async describe callback', line: 1, column: 17 }, | ||
{ | ||
message: 'Unexpected argument(s) in describe callback', | ||
line: 1, | ||
column: 33, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
const isDescribe = require('./util').isDescribe; | ||
const isFunction = require('./util').isFunction; | ||
|
||
const isAsync = node => node.async; | ||
|
||
const hasParams = node => node.params.length > 0; | ||
|
||
const paramsLocation = params => { | ||
const first = params[0]; | ||
const last = params[params.length - 1]; | ||
return { | ||
start: { | ||
line: first.loc.start.line, | ||
column: first.loc.start.column, | ||
}, | ||
end: { | ||
line: last.loc.end.line, | ||
column: last.loc.end.column, | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: | ||
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/valid-describe.md', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isDescribe(node)) { | ||
const callbackFunction = node.arguments[1]; | ||
if (callbackFunction && isFunction(callbackFunction)) { | ||
if (isAsync(callbackFunction)) { | ||
context.report({ | ||
message: 'No async describe callback', | ||
node: callbackFunction, | ||
}); | ||
} | ||
if (hasParams(callbackFunction)) { | ||
context.report({ | ||
message: 'Unexpected argument(s) in describe callback', | ||
loc: paramsLocation(callbackFunction.params), | ||
}); | ||
} | ||
callbackFunction.body.body.forEach(node => { | ||
if (node.type === 'ReturnStatement') { | ||
context.report({ | ||
message: 'Unexpected return statement in describe callback', | ||
node: node, | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thevalid-expect
rule. Open to other naming suggestions too!