Skip to content

fix(prefer-explicit-assert): check property existence #476

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 19, 2021
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
14 changes: 11 additions & 3 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,17 @@ export default createTestingLibraryRule<Options, MessageIds>({
const expectCallNode = findClosestCallNode(node, 'expect');
if (!expectCallNode) return;

const expectStatement =
expectCallNode.parent as TSESTree.MemberExpression;
const property = expectStatement.property as TSESTree.Identifier;
const expectStatement = expectCallNode.parent;
if (!isMemberExpression(expectStatement)) {
return;
}

const property = expectStatement.property;

if (!ASTUtils.isIdentifier(property)) {
return;
}

let matcher = property.name;
let isNegatedMatcher = false;

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@typescript-eslint/experimental-utils": "^4.30.0"
},
"devDependencies": {
"@babel/eslint-plugin": "^7.14.5",
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@types/jest": "^27.0.1",
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/prefer-explicit-assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),
{
// https://github.com/testing-library/eslint-plugin-testing-library/issues/475
code: `
// incomplete expect statement should be ignored
expect('something');
expect(getByText('foo'));
`,
options: [
{
assertion: 'toBeInTheDocument',
},
],
},
],
invalid: [
...COMBINED_QUERIES_METHODS.map(
Expand Down