-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wip): add no-async-describe rule
- Loading branch information
Showing
5 changed files
with
83 additions
and
2 deletions.
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,3 @@ | ||
# Prevent async describe callback (no-async-describe) | ||
|
||
TODO |
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,43 @@ | ||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../..').rules; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
}, | ||
}); | ||
|
||
const expectedErrorMessage = 'No async describe'; | ||
|
||
ruleTester.run('no-async-describe', rules['no-async-describe'], { | ||
valid: [ | ||
'describe("foo")', | ||
'describe("foo", function() {})', | ||
'describe("foo", () => {})', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'describe("foo", async () => {})', | ||
errors: [{ message: expectedErrorMessage, line: 1, column: 17 }], | ||
}, | ||
{ | ||
code: 'describe("foo", async function () {})', | ||
errors: [{ message: expectedErrorMessage, line: 1, column: 17 }], | ||
}, | ||
{ | ||
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: expectedErrorMessage, line: 6, column: 27 }], | ||
}, | ||
], | ||
}); |
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,34 @@ | ||
function isDescribe(node) { | ||
return node.type === 'CallExpression' && node.callee.name === 'describe'; | ||
} | ||
|
||
function isFunction(node) { | ||
return ( | ||
node.type === 'FunctionExpression' || | ||
node.type === 'ArrowFunctionExpression' | ||
); | ||
} | ||
|
||
function isAsync(node) { | ||
return node.async; | ||
} | ||
|
||
module.exports = context => { | ||
return { | ||
CallExpression(node) { | ||
if (node && isDescribe(node)) { | ||
const callbackFunction = node.arguments[1]; | ||
if ( | ||
callbackFunction && | ||
isFunction(callbackFunction) && | ||
isAsync(callbackFunction) | ||
) { | ||
context.report({ | ||
message: 'No async describe', | ||
node: callbackFunction, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |