Skip to content
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

fix(no-standalone-expect): only report on expect.hasAssertions & expect.assertions member calls #1191

Merged
merged 1 commit into from Aug 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/rules/__tests__/no-standalone-expect.test.ts
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
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