Skip to content

Commit

Permalink
fix(no-standalone-expect): Allow expect() calls in test hooks
Browse files Browse the repository at this point in the history
Fixes #242
  • Loading branch information
mskelton committed Feb 24, 2024
1 parent 1eb3721 commit 5524bf7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/rules/valid-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ test.describe('foo', () => {
```ts
interface Options {
ignoreSpaces?: boolean;
ignoreTypeOfTestName?: boolean;
ignoreTypeOfDescribeName?: boolean;
disallowedWords?: string[];
mustNotMatch?: Partial<Record<'describe' | 'test', string>> | string;
Expand Down
13 changes: 12 additions & 1 deletion src/rules/no-standalone-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isDescribeCall,
isFunction,
isTestCall,
isTestHook,
} from '../utils/ast';

const getBlockType = (
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -95,6 +102,10 @@ export default {
callStack.push('test');
}

if (isTestHook(context, node)) {
callStack.push('hook');
}

if (node.callee.type === 'TaggedTemplateExpression') {
callStack.push('template');
}
Expand Down
11 changes: 11 additions & 0 deletions test/spec/no-standalone-expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 5524bf7

Please sign in to comment.