forked from jest-community/eslint-plugin-jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves jest-community#72
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 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,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) |
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,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], | ||
}, | ||
], | ||
}); |
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,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', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |