Skip to content

Commit 1949a93

Browse files
authored
fix(prefer-explicit-assert): check property existence (#476)
1 parent e0a122b commit 1949a93

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/rules/prefer-explicit-assert.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,17 @@ export default createTestingLibraryRule<Options, MessageIds>({
156156
const expectCallNode = findClosestCallNode(node, 'expect');
157157
if (!expectCallNode) return;
158158

159-
const expectStatement =
160-
expectCallNode.parent as TSESTree.MemberExpression;
161-
const property = expectStatement.property as TSESTree.Identifier;
159+
const expectStatement = expectCallNode.parent;
160+
if (!isMemberExpression(expectStatement)) {
161+
return;
162+
}
163+
164+
const property = expectStatement.property;
165+
166+
if (!ASTUtils.isIdentifier(property)) {
167+
return;
168+
}
169+
162170
let matcher = property.name;
163171
let isNegatedMatcher = false;
164172

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@typescript-eslint/experimental-utils": "^4.30.0"
4646
},
4747
"devDependencies": {
48+
"@babel/eslint-plugin": "^7.14.5",
4849
"@commitlint/cli": "^12.1.4",
4950
"@commitlint/config-conventional": "^12.1.4",
5051
"@types/jest": "^27.0.1",

tests/lib/rules/prefer-explicit-assert.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ ruleTester.run(RULE_NAME, rule, {
160160
},
161161
],
162162
})),
163+
{
164+
// https://github.com/testing-library/eslint-plugin-testing-library/issues/475
165+
code: `
166+
// incomplete expect statement should be ignored
167+
expect('something');
168+
expect(getByText('foo'));
169+
`,
170+
options: [
171+
{
172+
assertion: 'toBeInTheDocument',
173+
},
174+
],
175+
},
163176
],
164177
invalid: [
165178
...COMBINED_QUERIES_METHODS.map(

0 commit comments

Comments
 (0)