Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(valid-expect): error on missing async matchers #53

Merged
merged 2 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/valid-expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ expect().toEqual('something');
expect('something', 'else');
expect('something');
expect(true).toBeDefined;
expect(Promise.resolve('hello')).resolves;
```

The following patterns are not warnings:
Expand All @@ -40,4 +41,5 @@ The following patterns are not warnings:
expect('something').toEqual('something');
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect(true).toBeDefined();
expect(Promise.resolve('hello')).resolves.toEqual('hello');
```
30 changes: 30 additions & 0 deletions rules/__tests__/valid_expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,35 @@ ruleTester.run('valid-expect', rules['valid-expect'], {
},
],
},
{
code: 'expect(true).resolves;',
errors: [
{
endColumn: 22,
column: 14,
message: '"resolves" needs to call a matcher.',
},
],
},
{
code: 'expect(true).rejects;',
errors: [
{
endColumn: 21,
column: 14,
message: '"rejects" needs to call a matcher.',
},
],
},
{
code: 'expect(true).not;',
errors: [
{
endColumn: 17,
column: 14,
message: '"not" needs to call a matcher.',
},
],
},
],
});
9 changes: 8 additions & 1 deletion rules/valid_expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,18 @@ module.exports = context => {

// matcher was not called
if (grandParent.type === 'ExpressionStatement') {
let message;
if (expectProperties.indexOf(propertyName) > -1) {
message = `"${propertyName}" needs to call a matcher.`;
} else {
message = `"${propertyName}" was not called.`;
}

context.report({
// For some reason `endColumn` isn't set in tests if `loc` is not
// added
loc: parentProperty.loc,
message: `"${propertyName}" was not called.`,
message,
node: parentProperty,
});
}
Expand Down