Skip to content

Commit

Permalink
fix(no-standalone-expect): only report on expect.hasAssertions & `e…
Browse files Browse the repository at this point in the history
…xpect.assertions` member calls (#1191)
  • Loading branch information
G-Rath authored Aug 8, 2022
1 parent 98a9a48 commit 4bf9eea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/rules/__tests__/no-standalone-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('no-standalone-expect', rule, {
valid: [
'expect.any(String)',
'expect.extend({})',
'describe("a test", () => { it("an it", () => {expect(1).toBe(1); }); });',
'describe("a test", () => { it("an it", () => { const func = () => { expect(1).toBe(1); }; }); });',
'describe("a test", () => { const func = () => { expect(1).toBe(1); }; });',
Expand Down Expand Up @@ -65,6 +67,10 @@ ruleTester.run('no-standalone-expect', rule, {
code: 'expect.hasAssertions()',
errors: [{ endColumn: 23, column: 1, messageId: 'unexpectedExpect' }],
},
{
code: 'expect().hasAssertions()',
errors: [{ endColumn: 25, column: 1, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
describe('scenario', () => {
Expand Down Expand Up @@ -163,5 +169,14 @@ ruleTester.run('no-standalone-expect', rule, {
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 51, column: 28, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
import { expect as pleaseExpect } from '@jest/globals';
beforeEach(() => pleaseExpect.hasAssertions());
`,
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 46, column: 18, messageId: 'unexpectedExpect' }],
},
],
});
18 changes: 15 additions & 3 deletions src/rules/no-standalone-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
import {
DescribeAlias,
createRule,
getAccessorValue,
getNodeName,
isFunction,
isTypeOfJestFnCall,
Expand Down Expand Up @@ -86,9 +87,20 @@ export default createRule<

return {
CallExpression(node) {
const { type: jestFnCallType } = parseJestFnCall(node, context) ?? {};
const jestFnCall = parseJestFnCall(node, context);

if (jestFnCall?.type === 'expect') {
if (
jestFnCall.head.node.parent?.type ===
AST_NODE_TYPES.MemberExpression &&
jestFnCall.members.length === 1 &&
!['assertions', 'hasAssertions'].includes(
getAccessorValue(jestFnCall.members[0]),
)
) {
return;
}

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

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

if (jestFnCallType === 'test' || isCustomTestBlockFunction(node)) {
if (jestFnCall?.type === 'test' || isCustomTestBlockFunction(node)) {
callStack.push('test');
}

Expand Down

0 comments on commit 4bf9eea

Please sign in to comment.