diff --git a/lib/rules/prefer-explicit-assert.ts b/lib/rules/prefer-explicit-assert.ts index a261b17f..86e1f4ed 100644 --- a/lib/rules/prefer-explicit-assert.ts +++ b/lib/rules/prefer-explicit-assert.ts @@ -156,9 +156,17 @@ export default createTestingLibraryRule({ 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; diff --git a/package.json b/package.json index 0a59a6b7..979b9b4e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/lib/rules/prefer-explicit-assert.test.ts b/tests/lib/rules/prefer-explicit-assert.test.ts index e894e232..08dd5fb8 100644 --- a/tests/lib/rules/prefer-explicit-assert.test.ts +++ b/tests/lib/rules/prefer-explicit-assert.test.ts @@ -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(