Skip to content

Commit 61c2adf

Browse files
deecewanSimenB
authored andcommittedAug 9, 2018
feat(rules): add expect-expect (#133)
1 parent 5bc0eea commit 61c2adf

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
 

Diff for: ‎docs/rules/expect-expect.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Enforce assertion to be made in a test body (expect-expect)
2+
3+
Ensure that there is at least one `expect` call made in a test.
4+
5+
## Rule details
6+
7+
This rule triggers when there is no call made to `expect` in a test, to prevent
8+
users from forgetting to add assertions.
9+
10+
### Default configuration
11+
12+
The following patterns are considered warnings:
13+
14+
```js
15+
it('should be a test', () => {
16+
console.log('no assertion');
17+
});
18+
test('should assert something', () => {});
19+
```
20+
21+
The following patterns are not warnings:
22+
23+
```js
24+
it('should be a test', () => {
25+
expect(true).toBeDefined();
26+
});
27+
it('should work with callbacks/async', () => {
28+
somePromise().then(res => expect(res).toBe('passed'));
29+
});
30+
```

Diff for: ‎rules/__tests__/expect-expect.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const RuleTester = require('eslint').RuleTester;
4+
const rule = require('../expect-expect');
5+
6+
const ruleTester = new RuleTester({
7+
parserOptions: {
8+
ecmaVersion: 6,
9+
},
10+
});
11+
12+
ruleTester.run('expect-expect', rule, {
13+
valid: [
14+
'it("should pass", () => expect(true).toBeDefined())',
15+
'test("should pass", () => expect(true).toBeDefined())',
16+
'it("should pass", () => somePromise().then(() => expect(true).toBeDefined()))',
17+
],
18+
19+
invalid: [
20+
{
21+
code: 'it("should fail", () => {});',
22+
errors: [
23+
{
24+
message: 'Test has no assertions',
25+
type: 'CallExpression',
26+
},
27+
],
28+
},
29+
{
30+
code: 'test("should fail", () => {});',
31+
errors: [
32+
{
33+
message: 'Test has no assertions',
34+
type: 'CallExpression',
35+
},
36+
],
37+
},
38+
{
39+
code: 'it("should fail", () => { somePromise.then(() => {}); });',
40+
errors: [
41+
{
42+
message: 'Test has no assertions',
43+
type: 'CallExpression',
44+
},
45+
],
46+
},
47+
],
48+
});

Diff for: ‎rules/expect-expect.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
/*
4+
* This implementation is adapted from eslint-plugin-jasmine.
5+
* MIT license, Remco Haszing.
6+
*/
7+
8+
const getDocsUrl = require('./util').getDocsUrl;
9+
10+
module.exports = {
11+
meta: {
12+
docs: {
13+
url: getDocsUrl(__filename),
14+
},
15+
},
16+
create(context) {
17+
// variables should be defined here
18+
const unchecked = [];
19+
20+
//----------------------------------------------------------------------
21+
// Helpers
22+
//----------------------------------------------------------------------
23+
const isExpectCall = node =>
24+
// if we're not calling a function, ignore
25+
node.type === 'CallExpression' &&
26+
// if we're not calling expect, ignore
27+
node.callee.name === 'expect';
28+
//----------------------------------------------------------------------
29+
// Public
30+
//----------------------------------------------------------------------
31+
return {
32+
// give me methods
33+
CallExpression(node) {
34+
// keep track of `it` calls
35+
if (['it', 'test'].indexOf(node.callee.name) > -1) {
36+
unchecked.push(node);
37+
return;
38+
}
39+
if (!isExpectCall(node)) {
40+
return;
41+
}
42+
// here, we do have a call to expect
43+
// use `some` to return early (in case of nested `it`s
44+
context.getAncestors().some(ancestor => {
45+
const index = unchecked.indexOf(ancestor);
46+
if (index !== -1) {
47+
unchecked.splice(index, 1);
48+
return true;
49+
}
50+
return false;
51+
});
52+
},
53+
'Program:exit'() {
54+
unchecked.forEach(node =>
55+
context.report({
56+
message: 'Test has no assertions',
57+
node,
58+
})
59+
);
60+
},
61+
};
62+
},
63+
};

0 commit comments

Comments
 (0)
Please sign in to comment.