Skip to content

Commit 9fca36b

Browse files
mattphillipsSimenB
authored andcommittedAug 9, 2018
feat(rules): add no-test-return-statement (#136)
Adds a rule for not returning inside of a test. Related issue in Jest: jestjs/jest#6516
1 parent 61c2adf commit 9fca36b

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
 

Diff for: ‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ for more information about extending configuration files.
9090
| [no-jest-import][] | Disallow importing `jest` | ![recommended][] | |
9191
| [no-large-snapshots][] | Disallow large snapshots | | |
9292
| [no-test-prefixes][] | Disallow using `f` & `x` prefixes to define focused/skipped tests | | ![fixable-green][] |
93+
| [no-test-return-statement][] | Disallow explicitly returning from tests | | |
9394
| [prefer-expect-assertions][] | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
9495
| [prefer-to-be-null][] | Suggest using `toBeNull()` | | ![fixable-green][] |
9596
| [prefer-to-be-undefined][] | Suggest using `toBeUndefined()` | | ![fixable-green][] |
@@ -114,6 +115,7 @@ for more information about extending configuration files.
114115
[no-jest-import]: docs/rules/no-jest-import.md
115116
[no-large-snapshots]: docs/rules/no-large-snapshots.md
116117
[no-test-prefixes]: docs/rules/no-test-prefixes.md
118+
[no-test-return-statement]: docs/rules/no-test-return-statement.md
117119
[prefer-expect-assertions]: docs/rules/prefer-expect-assertions.md
118120
[prefer-to-be-null]: docs/rules/prefer-to-be-null.md
119121
[prefer-to-be-undefined]: docs/rules/prefer-to-be-undefined.md

Diff for: ‎docs/rules/no-test-return-statement.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Disallow explicitly returning from tests (no-test-return-statement)
2+
3+
Tests in Jest should be void and not return values.
4+
5+
If you are returning Promises then you should update the test to use
6+
`async/await`.
7+
8+
## Rule details
9+
10+
This rule triggers a warning if you use a return statement inside of a test
11+
body.
12+
13+
```js
14+
/*eslint jest/no-test-return-statement: "error"*/
15+
16+
// valid:
17+
18+
it('noop', function() {});
19+
20+
test('noop', () => {});
21+
22+
test('one arrow', () => expect(1).toBe(1));
23+
24+
test('empty');
25+
26+
test('one', () => {
27+
expect(1).toBe(1);
28+
});
29+
30+
it('one', function() {
31+
expect(1).toBe(1);
32+
});
33+
34+
it('returning a promise', async () => {
35+
await new Promise(res => setTimeout(res, 100));
36+
expect(1).toBe(1);
37+
});
38+
39+
// invalid:
40+
test('return an expect', () => {
41+
return expect(1).toBe(1);
42+
});
43+
44+
it('returning a promise', function() {
45+
return new Promise(res => setTimeout(res, 100)).then(() => expect(1).toBe(1));
46+
});
47+
```

Diff for: ‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const noJasmineGlobals = require('./rules/no-jasmine-globals');
1010
const noJestImport = require('./rules/no-jest-import');
1111
const noLargeSnapshots = require('./rules/no-large-snapshots');
1212
const noTestPrefixes = require('./rules/no-test-prefixes');
13+
const noTestReturnStatement = require('./rules/no-test-return-statement');
1314
const preferToBeNull = require('./rules/prefer-to-be-null');
1415
const preferToBeUndefined = require('./rules/prefer-to-be-undefined');
1516
const preferToHaveLength = require('./rules/prefer-to-have-length');
@@ -75,6 +76,7 @@ module.exports = {
7576
'no-jest-import': noJestImport,
7677
'no-large-snapshots': noLargeSnapshots,
7778
'no-test-prefixes': noTestPrefixes,
79+
'no-test-return-statement': noTestReturnStatement,
7880
'prefer-to-be-null': preferToBeNull,
7981
'prefer-to-be-undefined': preferToBeUndefined,
8082
'prefer-to-have-length': preferToHaveLength,

Diff for: ‎rules/__tests__/no-test-return-statement.test.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../no-test-return-statement');
5+
6+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
7+
8+
ruleTester.run('no-test-prefixes', rule, {
9+
valid: [
10+
'it("noop", function () {});',
11+
'test("noop", () => {});',
12+
'test("one", () => expect(1).toBe(1));',
13+
'test("empty")',
14+
`
15+
test("one", () => {
16+
expect(1).toBe(1);
17+
});
18+
`,
19+
`
20+
it("one", function () {
21+
expect(1).toBe(1);
22+
});
23+
`,
24+
],
25+
invalid: [
26+
{
27+
code: `
28+
test("one", () => {
29+
return expect(1).toBe(1);
30+
});
31+
`,
32+
errors: [
33+
{
34+
message: 'Jest tests should not return a value.',
35+
column: 9,
36+
line: 3,
37+
},
38+
],
39+
},
40+
{
41+
code: `
42+
it("one", function () {
43+
return expect(1).toBe(1);
44+
});
45+
`,
46+
errors: [
47+
{
48+
message: 'Jest tests should not return a value.',
49+
column: 9,
50+
line: 3,
51+
},
52+
],
53+
},
54+
],
55+
});

Diff for: ‎rules/no-test-return-statement.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const getDocsUrl = require('./util').getDocsUrl;
4+
const isFunction = require('./util').isFunction;
5+
const isTestCase = require('./util').isTestCase;
6+
7+
const MESSAGE = 'Jest tests should not return a value.';
8+
const RETURN_STATEMENT = 'ReturnStatement';
9+
const BLOCK_STATEMENT = 'BlockStatement';
10+
11+
const getBody = args => {
12+
if (
13+
args.length > 1 &&
14+
isFunction(args[1]) &&
15+
args[1].body.type === BLOCK_STATEMENT
16+
) {
17+
return args[1].body.body;
18+
}
19+
return [];
20+
};
21+
22+
module.exports = {
23+
meta: {
24+
docs: {
25+
url: getDocsUrl(__filename),
26+
},
27+
},
28+
create(context) {
29+
return {
30+
CallExpression(node) {
31+
if (!isTestCase(node)) return;
32+
const body = getBody(node.arguments);
33+
const returnStmt = body.find(t => t.type === RETURN_STATEMENT);
34+
if (!returnStmt) return;
35+
36+
context.report({
37+
message: MESSAGE,
38+
node: returnStmt,
39+
});
40+
},
41+
};
42+
},
43+
};

0 commit comments

Comments
 (0)
Please sign in to comment.