Skip to content

Commit

Permalink
feat(wip): add no-hooks rule
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed Feb 12, 2018
1 parent 38f8ca0 commit 989ab73
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ for more information about extending configuration files.
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce consistent test or it keyword | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [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-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
Expand Down
14 changes: 14 additions & 0 deletions docs/rules/no-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Disallow setup and teardown hooks (no-hooks)

## Rule Details

## Options

### `allowed`

## When Not To Use It

## Further Reading

* [Jest docs - Setup and Teardown](https://facebook.github.io/jest/docs/en/setup-teardown.html)
* [@jamiebuilds Twitter thread](https://twitter.com/jamiebuilds/status/954906997169664000)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const consistentTestIt = require('./rules/consistent-test-it');
const noDisabledTests = require('./rules/no-disabled-tests');
const noFocusedTests = require('./rules/no-focused-tests');
const noHooks = require('./rules/no-hooks');
const noIdenticalTitle = require('./rules/no-identical-title');
const noLargeSnapshots = require('./rules/no-large-snapshots');
const preferToBeNull = require('./rules/prefer-to-be-null');
Expand Down Expand Up @@ -61,6 +62,7 @@ module.exports = {
'consistent-test-it': consistentTestIt,
'no-disabled-tests': noDisabledTests,
'no-focused-tests': noFocusedTests,
'no-hooks': noHooks,
'no-identical-title': noIdenticalTitle,
'no-large-snapshots': noLargeSnapshots,
'prefer-to-be-null': preferToBeNull,
Expand Down
49 changes: 49 additions & 0 deletions rules/__tests__/no-hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const rules = require('../..').rules;
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
});

const error = {
ruleId: 'no-hooks',
message: 'Unexpected setup/teardown hook',
};

ruleTester.run('no-hooks', rules['no-hooks'], {
valid: [
'test("foo")',
'describe("foo", () => { it("bar") })',
'test("foo", () => { expect(subject.beforeEach()).toBe(true) })',
{
code: 'afterEach(() => {}); afterAll(() => {});',
options: [{ allowed: ['afterEach', 'afterAll'] }],
},
],
invalid: [
{
code: 'beforeAll(() => {})',
errors: [error],
},
{
code: 'beforeEach(() => {})',
errors: [error],
},
{
code: 'afterAll(() => {})',
errors: [error],
},
{
code: 'afterEach(() => {})',
errors: [error],
},
{
code: 'beforeEach(() => {}); afterEach(() => { jest.resetModules() });',
options: [{ allowed: ['afterEach'] }],
errors: [error],
},
],
});
52 changes: 52 additions & 0 deletions rules/no-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

module.exports = {
meta: {
docs: {
url:
'https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-hooks.md',
},
},
schema: [
{
type: 'object',
properties: {
allowed: {
type: 'array',
contains: ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'],
},
},
additionalProperties: false,
},
],
create(context) {
const testHookNames = Object.assign(Object.create(null), {
beforeAll: true,
beforeEach: true,
afterAll: true,
afterEach: true,
});

const allowed = (context.options[0] || { allowed: [] }).allowed.reduce(
(hashMap, value) => {
hashMap[value] = true;
return hashMap;
},
Object.create(null)
);

const isHook = node => testHookNames[node.callee.name];
const isWhitelisted = node => allowed[node.callee.name];

return {
CallExpression(node) {
if (isHook(node) && !isWhitelisted(node)) {
context.report({
node,
message: 'Unexpected setup/teardown hook',
});
}
},
};
},
};

0 comments on commit 989ab73

Please # to comment.