From 5524bf744dd9e537fbb999b4a86b27e2efb45273 Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Sat, 24 Feb 2024 11:06:47 -0600 Subject: [PATCH] fix(no-standalone-expect): Allow `expect()` calls in test hooks Fixes #242 --- docs/rules/valid-title.md | 1 + src/rules/no-standalone-expect.ts | 13 ++++++++++++- test/spec/no-standalone-expect.spec.ts | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/rules/valid-title.md b/docs/rules/valid-title.md index 5014750..5baf5ec 100644 --- a/docs/rules/valid-title.md +++ b/docs/rules/valid-title.md @@ -143,6 +143,7 @@ test.describe('foo', () => { ```ts interface Options { ignoreSpaces?: boolean; + ignoreTypeOfTestName?: boolean; ignoreTypeOfDescribeName?: boolean; disallowedWords?: string[]; mustNotMatch?: Partial> | string; diff --git a/src/rules/no-standalone-expect.ts b/src/rules/no-standalone-expect.ts index 9724659..a622b58 100644 --- a/src/rules/no-standalone-expect.ts +++ b/src/rules/no-standalone-expect.ts @@ -7,6 +7,7 @@ import { isDescribeCall, isFunction, isTestCall, + isTestHook, } from '../utils/ast'; const getBlockType = ( @@ -45,7 +46,13 @@ const getBlockType = ( return null; }; -type BlockType = 'arrow' | 'describe' | 'function' | 'template' | 'test'; +type BlockType = + | 'arrow' + | 'describe' + | 'function' + | 'hook' + | 'template' + | 'test'; export default { create(context: Rule.RuleContext) { @@ -95,6 +102,10 @@ export default { callStack.push('test'); } + if (isTestHook(context, node)) { + callStack.push('hook'); + } + if (node.callee.type === 'TaggedTemplateExpression') { callStack.push('template'); } diff --git a/test/spec/no-standalone-expect.spec.ts b/test/spec/no-standalone-expect.spec.ts index 17e4239..d255744 100644 --- a/test/spec/no-standalone-expect.spec.ts +++ b/test/spec/no-standalone-expect.spec.ts @@ -75,6 +75,17 @@ runRuleTester('no-standalone-expect', rule, { 'test.only("an only", value => { expect(value).toBe(true); });', 'class Helper { foo() { expect(1).toBe(1); } }', 'class Helper { foo = () => { expect(1).toBe(1); } }', + { + code: dedent` + test.describe('Test describe', () => { + test.beforeAll(async ({ page }) => { + await page.goto('https://google.com'); + await expect(page.getByRole('button')).toBeVisible(); + }); + }); + `, + name: 'Allows expect in hooks', + }, // Global aliases { code: dedent`