-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add
no-test-return-statement
- Loading branch information
1 parent
5bc0eea
commit cd99d8e
Showing
4 changed files
with
147 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,47 @@ | ||
# Disallow returning from tests (no-test-return-statement) | ||
|
||
Tests in Jest are void and cannot return values. | ||
|
||
If you are returning Promises then you should update the test to use | ||
`async/await`. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if you use a return statement inside of a test | ||
body. | ||
|
||
```js | ||
/*eslint jest/no-test-return: "error"*/ | ||
|
||
// valid: | ||
|
||
it('noop', function() {}); | ||
|
||
test('noop', () => {}); | ||
|
||
test('one arrow', () => expect(1).toBe(1)); | ||
|
||
test('empty'); | ||
|
||
test('one', () => { | ||
expect(1).toBe(1); | ||
}); | ||
|
||
it('one', function() { | ||
expect(1).toBe(1); | ||
}); | ||
|
||
it('returning a promise', async () => { | ||
await new Promise(res => setTimeout(res, 100)); | ||
expect(1).toBe(1); | ||
}); | ||
|
||
// invalid: | ||
test('return an expect', () => { | ||
return expect(1).toBe(1); | ||
}); | ||
|
||
it('returning a promise', function() { | ||
return new Promise(res => setTimeout(res, 100)).then(() => expect(1).toBe(1)); | ||
}); | ||
``` |
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,55 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../no-test-return-statement'); | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } }); | ||
|
||
ruleTester.run('no-test-prefixes', rule, { | ||
valid: [ | ||
'it("noop", function () {});', | ||
'test("noop", () => {});', | ||
'test("one", () => expect(1).toBe(1));', | ||
'test("empty")', | ||
` | ||
test("one", () => { | ||
expect(1).toBe(1); | ||
}); | ||
`, | ||
` | ||
it("one", function () { | ||
expect(1).toBe(1); | ||
}); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
test("one", () => { | ||
return expect(1).toBe(1); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
message: 'Jest tests should not return a value.', | ||
column: 9, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
it("one", function () { | ||
return expect(1).toBe(1); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
message: 'Jest tests should not return a value.', | ||
column: 9, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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 @@ | ||
'use strict'; | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
const isFunction = require('./util').isFunction; | ||
const isTestCase = require('./util').isTestCase; | ||
|
||
const MESSAGE = 'Jest tests should not return a value.'; | ||
const RETURN_STATEMENT = 'ReturnStatement'; | ||
const BLOCK_STATEMENT = 'BlockStatement'; | ||
|
||
const getBody = args => { | ||
if ( | ||
args.length > 1 && | ||
isFunction(args[1]) && | ||
args[1].body.type === BLOCK_STATEMENT | ||
) { | ||
return args[1].body.body; | ||
} | ||
return []; | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (!isTestCase(node)) return; | ||
const body = getBody(node.arguments); | ||
const returnStmt = body.find(t => t.type === RETURN_STATEMENT); | ||
if (!returnStmt) return; | ||
|
||
context.report({ | ||
message: MESSAGE, | ||
node: returnStmt, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |