Skip to content

Commit ed5d7bc

Browse files
committed
feat: Rename additionalAssertFunctionNames to assertFunctionNames
BREAKING CHANGE: Rename the `additionalAssertFunctionNames` option to `assertFunctionNames`. The accepted values are the same, the setting name is the only change.
1 parent cc3159a commit ed5d7bc

File tree

4 files changed

+17
-102
lines changed

4 files changed

+17
-102
lines changed

src/rules/expect-expect.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import { Rule } from 'eslint';
22
import ESTree from 'estree';
33
import { dig, isExpectCall, isTestCall } from '../utils/ast';
4-
import { getAdditionalAssertFunctionNames } from '../utils/misc';
54

65
function isAssertionCall(
76
node: ESTree.CallExpression,
8-
additionalAssertFunctionNames: string[],
7+
assertFunctionNames: string[],
98
) {
109
return (
1110
isExpectCall(node) ||
12-
additionalAssertFunctionNames.find((name) => dig(node.callee, name))
11+
assertFunctionNames.find((name) => dig(node.callee, name))
1312
);
1413
}
1514

1615
export default {
1716
create(context) {
17+
const options = {
18+
assertFunctionNames: [] as string[],
19+
...((context.options?.[0] as Record<string, unknown>) ?? {}),
20+
};
21+
1822
const sourceCode = context.sourceCode ?? context.getSourceCode();
1923
const unchecked: ESTree.CallExpression[] = [];
20-
const additionalAssertFunctionNames =
21-
getAdditionalAssertFunctionNames(context);
2224

2325
function checkExpressions(nodes: ESTree.Node[]) {
2426
for (const node of nodes) {
@@ -36,7 +38,7 @@ export default {
3638
CallExpression(node) {
3739
if (isTestCall(node, ['fixme', 'only', 'skip'])) {
3840
unchecked.push(node);
39-
} else if (isAssertionCall(node, additionalAssertFunctionNames)) {
41+
} else if (isAssertionCall(node, options.assertFunctionNames)) {
4042
const ancestors = sourceCode.getAncestors
4143
? sourceCode.getAncestors(node)
4244
: context.getAncestors();
@@ -65,7 +67,7 @@ export default {
6567
{
6668
additionalProperties: false,
6769
properties: {
68-
additionalAssertFunctionNames: {
70+
assertFunctionNames: {
6971
items: [{ type: 'string' }],
7072
type: 'array',
7173
},

src/utils/misc.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
1-
import { Rule } from 'eslint';
2-
import { Settings } from './types';
3-
41
export const getAmountData = (amount: number) => ({
52
amount: amount.toString(),
63
s: amount === 1 ? '' : 's',
74
});
8-
9-
interface AssertFunctionNamesOptions {
10-
additionalAssertFunctionNames?: string[];
11-
}
12-
13-
export function getAdditionalAssertFunctionNames(
14-
context: Rule.RuleContext,
15-
): string[] {
16-
const globalSettings =
17-
(context.settings as Settings).playwright?.additionalAssertFunctionNames ??
18-
([] as string[]);
19-
const ruleSettings =
20-
(context.options[0] as AssertFunctionNamesOptions | undefined)
21-
?.additionalAssertFunctionNames ?? ([] as string[]);
22-
23-
return [...globalSettings, ...ruleSettings];
24-
}

src/utils/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export type KnownCallExpression = ESTree.CallExpression & {
1515

1616
export interface Settings {
1717
playwright?: {
18-
additionalAssertFunctionNames?: readonly string[];
18+
globalAliases?: {
19+
expect?: string[];
20+
test?: string[];
21+
};
1922
};
2023
}

test/spec/expect-expect.spec.ts

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,8 @@ runRuleTester('expect-expect', rule, {
2727
})
2828
`,
2929
errors: [{ messageId: 'noAssertions' }],
30-
name: 'Global settings no false positives',
31-
settings: {
32-
playwright: {
33-
additionalAssertFunctionNames: ['wayComplexCustomCondition'],
34-
},
35-
},
36-
},
37-
{
38-
code: dedent`
39-
test('should fail', async ({ page }) => {
40-
await assertCustomCondition(page)
41-
})
42-
`,
43-
errors: [{ messageId: 'noAssertions' }],
44-
name: 'Rule settings no false positives',
45-
options: [
46-
{ additionalAssertFunctionNames: ['wayComplexCustomCondition'] },
47-
],
48-
},
49-
{
50-
code: dedent`
51-
test('should fail', async ({ page }) => {
52-
await assertCustomCondition(page)
53-
})
54-
`,
55-
errors: [{ messageId: 'noAssertions' }],
56-
name: 'Global settings no false positives',
57-
options: [
58-
{ additionalAssertFunctionNames: ['wayComplexRuleCustomCondition'] },
59-
],
60-
settings: {
61-
playwright: {
62-
additionalAssertFunctionNames: ['wayComplexGlobalCustomCondition'],
63-
},
64-
},
30+
name: 'Custom assert function',
31+
options: [{ assertFunctionNames: ['wayComplexCustomCondition'] }],
6532
},
6633
],
6734
valid: [
@@ -94,11 +61,7 @@ runRuleTester('expect-expect', rule, {
9461
})
9562
`,
9663
name: 'Custom assert function',
97-
settings: {
98-
playwright: {
99-
additionalAssertFunctionNames: ['assertCustomCondition'],
100-
},
101-
},
64+
options: [{ assertFunctionNames: ['assertCustomCondition'] }],
10265
},
10366
{
10467
code: dedent`
@@ -107,40 +70,7 @@ runRuleTester('expect-expect', rule, {
10770
})
10871
`,
10972
name: 'Custom assert class method',
110-
settings: {
111-
playwright: {
112-
additionalAssertFunctionNames: ['assertCustomCondition'],
113-
},
114-
},
115-
},
116-
{
117-
code: dedent`
118-
test('should fail', async ({ page }) => {
119-
await assertCustomCondition(page)
120-
})
121-
`,
122-
name: 'Rule settings only',
123-
options: [{ additionalAssertFunctionNames: ['assertCustomCondition'] }],
124-
},
125-
{
126-
code: dedent`
127-
test('should fail', async ({ page }) => {
128-
await assertCustomCondition(page)
129-
})
130-
131-
test('complex failure', async ({ page }) => {
132-
await wayComplexCustomCondition(page)
133-
})
134-
`,
135-
name: 'Global and rule settings combine rather than override',
136-
options: [
137-
{ additionalAssertFunctionNames: ['wayComplexCustomCondition'] },
138-
],
139-
settings: {
140-
playwright: {
141-
additionalAssertFunctionNames: ['assertCustomCondition'],
142-
},
143-
},
73+
options: [{ assertFunctionNames: ['assertCustomCondition'] }],
14474
},
14575
],
14676
});

0 commit comments

Comments
 (0)