Skip to content

Commit 4bf9eea

Browse files
authored
fix(no-standalone-expect): only report on expect.hasAssertions & expect.assertions member calls (#1191)
1 parent 98a9a48 commit 4bf9eea

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/rules/__tests__/no-standalone-expect.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const ruleTester = new TSESLint.RuleTester({
1212

1313
ruleTester.run('no-standalone-expect', rule, {
1414
valid: [
15+
'expect.any(String)',
16+
'expect.extend({})',
1517
'describe("a test", () => { it("an it", () => {expect(1).toBe(1); }); });',
1618
'describe("a test", () => { it("an it", () => { const func = () => { expect(1).toBe(1); }; }); });',
1719
'describe("a test", () => { const func = () => { expect(1).toBe(1); }; });',
@@ -65,6 +67,10 @@ ruleTester.run('no-standalone-expect', rule, {
6567
code: 'expect.hasAssertions()',
6668
errors: [{ endColumn: 23, column: 1, messageId: 'unexpectedExpect' }],
6769
},
70+
{
71+
code: 'expect().hasAssertions()',
72+
errors: [{ endColumn: 25, column: 1, messageId: 'unexpectedExpect' }],
73+
},
6874
{
6975
code: dedent`
7076
describe('scenario', () => {
@@ -163,5 +169,14 @@ ruleTester.run('no-standalone-expect', rule, {
163169
parserOptions: { sourceType: 'module' },
164170
errors: [{ endColumn: 51, column: 28, messageId: 'unexpectedExpect' }],
165171
},
172+
{
173+
code: dedent`
174+
import { expect as pleaseExpect } from '@jest/globals';
175+
176+
beforeEach(() => pleaseExpect.hasAssertions());
177+
`,
178+
parserOptions: { sourceType: 'module' },
179+
errors: [{ endColumn: 46, column: 18, messageId: 'unexpectedExpect' }],
180+
},
166181
],
167182
});

src/rules/no-standalone-expect.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
22
import {
33
DescribeAlias,
44
createRule,
5+
getAccessorValue,
56
getNodeName,
67
isFunction,
78
isTypeOfJestFnCall,
@@ -86,9 +87,20 @@ export default createRule<
8687

8788
return {
8889
CallExpression(node) {
89-
const { type: jestFnCallType } = parseJestFnCall(node, context) ?? {};
90+
const jestFnCall = parseJestFnCall(node, context);
91+
92+
if (jestFnCall?.type === 'expect') {
93+
if (
94+
jestFnCall.head.node.parent?.type ===
95+
AST_NODE_TYPES.MemberExpression &&
96+
jestFnCall.members.length === 1 &&
97+
!['assertions', 'hasAssertions'].includes(
98+
getAccessorValue(jestFnCall.members[0]),
99+
)
100+
) {
101+
return;
102+
}
90103

91-
if (jestFnCallType === 'expect') {
92104
const parent = callStack[callStack.length - 1];
93105

94106
if (!parent || parent === DescribeAlias.describe) {
@@ -98,7 +110,7 @@ export default createRule<
98110
return;
99111
}
100112

101-
if (jestFnCallType === 'test' || isCustomTestBlockFunction(node)) {
113+
if (jestFnCall?.type === 'test' || isCustomTestBlockFunction(node)) {
102114
callStack.push('test');
103115
}
104116

0 commit comments

Comments
 (0)