Skip to content

Commit

Permalink
fix: padding-around-expect-groups doesn't detect expectTypeOf as an e…
Browse files Browse the repository at this point in the history
…xpect clause (#588)

* fix: support expectTypeOf in padding-around-expect-groups rule

* test: add test
  • Loading branch information
y-hsgw authored Dec 10, 2024
1 parent 66fbbce commit 1138003
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/rules/padding-around-expect-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ export const config: Config[] = [
prevStatementType: StatementType.ExpectToken,
nextStatementType: StatementType.ExpectToken,
},
{
paddingType: PaddingType.Always,
prevStatementType: StatementType.Any,
nextStatementType: StatementType.ExpectTypeOfToken,
},
{
paddingType: PaddingType.Always,
prevStatementType: StatementType.ExpectTypeOfToken,
nextStatementType: StatementType.Any,
},
{
paddingType: PaddingType.Any,
prevStatementType: StatementType.ExpectTypeOfToken,
nextStatementType: StatementType.ExpectTypeOfToken,
},
];

export default createPaddingRule(
Expand Down
2 changes: 2 additions & 0 deletions src/utils/padding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const enum StatementType {
BeforeEachToken,
DescribeToken,
ExpectToken,
ExpectTypeOfToken,
FdescribeToken,
FitToken,
ItToken,
Expand Down Expand Up @@ -175,6 +176,7 @@ const statementTesters: { [T in StatementType]: StatementTester } = {
[StatementType.BeforeEachToken]: createTokenTester('beforeEach'),
[StatementType.DescribeToken]: createTokenTester('describe'),
[StatementType.ExpectToken]: createTokenTester('expect'),
[StatementType.ExpectTypeOfToken]: createTokenTester('expectTypeOf'),
[StatementType.FdescribeToken]: createTokenTester('fdescribe'),
[StatementType.FitToken]: createTokenTester('fit'),
[StatementType.ItToken]: createTokenTester('it'),
Expand Down
42 changes: 40 additions & 2 deletions tests/padding-around-expect-groups.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import type { TSESLint } from '@typescript-eslint/utils';
import rule, { RULE_NAME } from '../src/rules/padding-around-expect-groups';
import { ruleTester } from './ruleTester';
import { InvalidTestCase } from "@typescript-eslint/rule-tester"

const testCase = {
code: `
Expand Down Expand Up @@ -61,6 +61,16 @@ test('awaited expect', async () => {
await somethingElseAsync();
await expect(hasAPromise()).resolves.toEqual('foo');
});
test('expectTypeOf test', () => {
const hoge = 123;
expectTypeOf(hoge).toBeNumber();
expectTypeOf(hoge).toBeNumber();
const foo = "abc";
// Comment
expectTypeOf(foo).toBeString();
expectTypeOf(foo).toBeString();
});
`,
output: `
foo();
Expand Down Expand Up @@ -129,6 +139,19 @@ test('awaited expect', async () => {
await expect(hasAPromise()).resolves.toEqual('foo');
});
test('expectTypeOf test', () => {
const hoge = 123;
expectTypeOf(hoge).toBeNumber();
expectTypeOf(hoge).toBeNumber();
const foo = "abc";
// Comment
expectTypeOf(foo).toBeString();
expectTypeOf(foo).toBeString();
});
`,
errors: [
{
Expand Down Expand Up @@ -181,8 +204,23 @@ test('awaited expect', async () => {
line: 56,
column: 3,
},
{
messageId: 'missingPadding',
line: 61,
column: 3,
},
{
messageId: 'missingPadding',
line: 63,
column: 3,
},
{
messageId: 'missingPadding',
line: 65,
column: 3,
},
],
} satisfies TSESLint.InvalidTestCase<'missingPadding', never>;
} satisfies InvalidTestCase<'missingPadding', never>;

ruleTester.run(RULE_NAME, rule, {
valid: [testCase.output],
Expand Down

0 comments on commit 1138003

Please sign in to comment.