Skip to content

Commit

Permalink
feat(wip): add no-async-describe rule
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed Feb 3, 2018
1 parent eb31a54 commit 9895ab6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ for more information about extending configuration files.

| Rule | Description | Recommended | Fixable |
| ------------------------------------------------------------------ | --------------------------------------------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [no-async-describe](docs/rules/no-async-describe.md) | Prevent async describe callback | | |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
Expand Down
3 changes: 3 additions & 0 deletions docs/rules/no-async-describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Prevent async describe callback (no-async-describe)

TODO
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

const noAsyncDescribe = require('./rules/no_async_describe');
const noDisabledTests = require('./rules/no_disabled_tests');
const noFocusedTests = require('./rules/no_focused_tests');
const noIdenticalTitle = require('./rules/no_identical_title');
Expand Down Expand Up @@ -56,6 +55,7 @@ module.exports = {
'.snap': snapshotProcessor,
},
rules: {
'no-async-describe': noAsyncDescribe,
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-identical-title': noIdenticalTitle,
Expand Down
43 changes: 43 additions & 0 deletions rules/__tests__/no_async_describe.test.js
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 }],
},
],
});
34 changes: 34 additions & 0 deletions rules/no_async_describe.js
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,
});
}
}
},
};
};

0 comments on commit 9895ab6

Please sign in to comment.