Skip to content

Commit 16889bb

Browse files
authored
fix(valid-expect): error on missing async matchers (#53)
1 parent 6960e93 commit 16889bb

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

docs/rules/valid-expect.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ expect().toEqual('something');
3232
expect('something', 'else');
3333
expect('something');
3434
expect(true).toBeDefined;
35+
expect(Promise.resolve('hello')).resolves;
3536
```
3637

3738
The following patterns are not warnings:
@@ -40,4 +41,5 @@ The following patterns are not warnings:
4041
expect('something').toEqual('something');
4142
expect([1, 2, 3]).toEqual([1, 2, 3]);
4243
expect(true).toBeDefined();
44+
expect(Promise.resolve('hello')).resolves.toEqual('hello');
4345
```

rules/__tests__/valid_expect.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,35 @@ ruleTester.run('valid-expect', rules['valid-expect'], {
101101
},
102102
],
103103
},
104+
{
105+
code: 'expect(true).resolves;',
106+
errors: [
107+
{
108+
endColumn: 22,
109+
column: 14,
110+
message: '"resolves" needs to call a matcher.',
111+
},
112+
],
113+
},
114+
{
115+
code: 'expect(true).rejects;',
116+
errors: [
117+
{
118+
endColumn: 21,
119+
column: 14,
120+
message: '"rejects" needs to call a matcher.',
121+
},
122+
],
123+
},
124+
{
125+
code: 'expect(true).not;',
126+
errors: [
127+
{
128+
endColumn: 17,
129+
column: 14,
130+
message: '"not" needs to call a matcher.',
131+
},
132+
],
133+
},
104134
],
105135
});

rules/valid_expect.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,18 @@ module.exports = context => {
8383

8484
// matcher was not called
8585
if (grandParent.type === 'ExpressionStatement') {
86+
let message;
87+
if (expectProperties.indexOf(propertyName) > -1) {
88+
message = `"${propertyName}" needs to call a matcher.`;
89+
} else {
90+
message = `"${propertyName}" was not called.`;
91+
}
92+
8693
context.report({
8794
// For some reason `endColumn` isn't set in tests if `loc` is not
8895
// added
8996
loc: parentProperty.loc,
90-
message: `"${propertyName}" was not called.`,
97+
message,
9198
node: parentProperty,
9299
});
93100
}

0 commit comments

Comments
 (0)