-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(no-expect-in-setup-teardown): add no-expect-in-setup-teardown rule
- Loading branch information
Showing
5 changed files
with
230 additions
and
15 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,48 @@ | ||
# Discourage making expectations in setup and teardown functions (no-expect-in-setup-teardown) | ||
|
||
Making expectations inside test setup or teardown functions is sometimes a sign of a bad test design. | ||
|
||
It might negatively impact readability as a reader might think you have the actual test logic splattered between setup, teardown and the test functions themselves. | ||
|
||
Sometimes there is actually a need to test the result of a setup/teardown function call. In cases like these, it is recommended to create a separate test to test the setup/teardown process. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if there is an `expect()` call inside `beforeEach()`, `afterEach()`, `beforeAll()` and `afterAll()`. | ||
|
||
An array of expect function names may be passed to the configuration of this rule. By default only `expect()` is used. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
beforeEach(function() { expect(true).toBe(true); }); | ||
afterEach(function() { expect(true).toBe(true); }); | ||
beforeAll(function() { expect(true).toBe(true); }); | ||
afterAll(function() { expect(true).toBe(true); }); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
beforeEach(function() { someOtherFunction(); }); | ||
afterEach(function() {}); | ||
beforeAll(function() { someOtherFunction(); }); | ||
afterAll(function() {}); | ||
``` | ||
|
||
### AngularJS `$httpBackend` rule configuration example | ||
|
||
```yaml | ||
rules: | ||
no-expect-in-setup-teardown: | ||
- 2 | ||
- expect() | ||
- $httpBackend.expect() | ||
- $httpBackend.expectDELETE() | ||
- $httpBackend.expectGET() | ||
- $httpBackend.expectJSONP() | ||
- $httpBackend.expectHEAD() | ||
- $httpBackend.expectPATCH() | ||
- $httpBackend.expectPOST() | ||
- $httpBackend.expectPUT() | ||
``` |
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,50 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Discourage having expect in setup and teardown functions | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
module.exports = function (context) { | ||
var allowed = context.options.length ? context.options : ['expect()'] | ||
var setupRegexp = /^(before|after)(Each|All)$/ | ||
|
||
var insideSetup = false | ||
var setupFunctionName | ||
|
||
function buildName (node) { | ||
if (node.type === 'CallExpression') { | ||
return buildName(node.callee) + '()' | ||
} | ||
if (node.type === 'MemberExpression') { | ||
return buildName(node.object) + '.' + node.property.name | ||
} | ||
if (node.type === 'Identifier') { | ||
return node.name | ||
} | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (setupRegexp.test(node.callee.name)) { | ||
insideSetup = true | ||
setupFunctionName = node.callee.name | ||
return | ||
} | ||
|
||
if (insideSetup) { | ||
var functionName = buildName(node) | ||
if (allowed.indexOf(functionName) > -1) { | ||
context.report(node, 'Unexpected "' + functionName + '" call in "' + setupFunctionName + '()"') | ||
} | ||
} | ||
}, | ||
|
||
'CallExpression:exit': function (node) { | ||
if (setupRegexp.test(node.callee.name)) { | ||
insideSetup = false | ||
setupFunctionName = undefined | ||
} | ||
} | ||
} | ||
} |
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,113 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/no-expect-in-setup-teardown') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('no-expect-in-setup-teardown', rule, { | ||
valid: [ | ||
'beforeEach(function() {});', | ||
'afterEach(function() {});', | ||
'beforeAll(function() {});', | ||
'afterAll(function() {});', | ||
'it("", function() { expect(true).toBe(true); })', | ||
'beforeEach(function() { someOtherFunction(); });', | ||
'afterEach(function() { someOtherFunction(); });', | ||
'beforeAll(function() { someOtherFunction(); });', | ||
'afterAll(function() { someOtherFunction(); });', | ||
'expect(true).toBe(true);', | ||
{ | ||
code: 'it("", function() {$httpBackend.expectGET();})', | ||
options: [ | ||
'$httpBackend.expectGET()' | ||
] | ||
}, | ||
{ | ||
code: 'it("", function() {return $httpBackend.expectGET();})', | ||
options: [ | ||
'$httpBackend.expectGET()' | ||
] | ||
}, | ||
{ | ||
code: 'it("", function() {a.deeply.nested().expect.expression();})', | ||
options: [ | ||
'a.deeply.nested().expect.expression()' | ||
] | ||
}, | ||
{ | ||
code: 'it("", function() { expect(true).toBe(true); })', | ||
options: [ | ||
'a.deeply.nested().expect.expression()' | ||
] | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'beforeEach(function() { expect(true).toBe(true); });', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "expect()" call in "beforeEach()"' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'afterEach(function() { expect(true).toBe(true); });', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "expect()" call in "afterEach()"' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'beforeAll(function() { expect(true).toBe(true); });', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "expect()" call in "beforeAll()"' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'afterAll(function() { expect(true).toBe(true); });', | ||
errors: [ | ||
{ | ||
message: 'Unexpected "expect()" call in "afterAll()"' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'beforeEach(function() {$httpBackend.expectGET();})', | ||
options: [ | ||
'$httpBackend.expectGET()' | ||
], | ||
errors: [ | ||
{ | ||
message: 'Unexpected "$httpBackend.expectGET()" call in "beforeEach()"' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'beforeAll(function() {return $httpBackend.expectGET();})', | ||
options: [ | ||
'$httpBackend.expectGET()' | ||
], | ||
errors: [ | ||
{ | ||
message: 'Unexpected "$httpBackend.expectGET()" call in "beforeAll()"' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'afterEach(function() {a.deeply.nested().expect.expression()})', | ||
options: [ | ||
'a.deeply.nested().expect.expression()' | ||
], | ||
errors: [ | ||
{ | ||
message: 'Unexpected "a.deeply.nested().expect.expression()" call in "afterEach()"' | ||
} | ||
] | ||
} | ||
] | ||
}) |