Skip to content

Commit

Permalink
chore: Format recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Aug 26, 2023
1 parent b00c3e3 commit a122c85
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,27 @@ This plugin bundles two configurations to work with both `@playwright/test` or

## Global Settings

The plugin reads global settings from your ESLint configuration's shared data under the `playwright` key. It supports the following settings:

- `additionalAssertFunctionNames`: an array of function names to treat as assertion functions for the case of rules like `expect-expect`, which enforces the presence of at least one assertion per test case. This allows such rules to recognise custom assertion functions as valid assertions. The global setting applies to all modules. The [`expect-expect` rule accepts an option by the same name](./rules/expect-expect.md#additionalassertfunctionnames) to enable per-module configuration (.e.g, for module-specific custom assert functions).
The plugin reads global settings from your ESLint configuration's shared data
under the `playwright` key. It supports the following settings:

- `additionalAssertFunctionNames`: an array of function names to treat as
assertion functions for the case of rules like `expect-expect`, which enforces
the presence of at least one assertion per test case. This allows such rules
to recognise custom assertion functions as valid assertions. The global
setting applies to all modules. The
[`expect-expect` rule accepts an option by the same name](./rules/expect-expect.md#additionalassertfunctionnames)
to enable per-module configuration (.e.g, for module-specific custom assert
functions).

You can configure these settings like so:

```json
{
"settings": {
"playwright": {
"additionalAssertFunctionNames": ["assertCustomCondition"]
}
"settings": {
"playwright": {
"additionalAssertFunctionNames": ["assertCustomCondition"]
}
}
}
```

Expand Down
18 changes: 11 additions & 7 deletions docs/rules/expect-expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ test('should work with callbacks/async', async () => {

### `additionalAssertFunctionNames`

An array of function names to treat as assertion functions. Only standalone functions are supported. Configure globally acceptable assert function names using [the global setting](../global-settings.md). You can also customize assert function names per-file. For example:
An array of function names to treat as assertion functions. Only standalone
functions are supported. Configure globally acceptable assert function names
using [the global setting](../global-settings.md). You can also customize assert
function names per-file. For example:

```ts
/* eslint playwright/expect-expect: ["error", { "additionalAssertFunctionNames": ["assertScrolledToBottom"] }] */
Expand All @@ -55,14 +58,15 @@ function assertScrolledToBottom(page) {
describe('scrolling', () => {
test('button click', async ({ page }) => {
// ...
await assertScrolledToBottom(page)
})
await assertScrolledToBottom(page);
});

test('another way to scroll', async ({ page }) => {
// ...
await assertScrolledToBottom(page)
})
})
await assertScrolledToBottom(page);
});
});
```

The rule option and the global setting are merged. On a file level, both are considered.
The rule option and the global setting are merged. On a file level, both are
considered.
3 changes: 2 additions & 1 deletion src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function isAssertionCall(
export default {
create(context) {
const unchecked: ESTree.CallExpression[] = [];
const additionalAssertFunctionNames = getAdditionalAssertFunctionNames(context)
const additionalAssertFunctionNames =
getAdditionalAssertFunctionNames(context);

function checkExpressions(nodes: ESTree.Node[]) {
for (const node of nodes) {
Expand Down
16 changes: 11 additions & 5 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ export const getAmountData = (amount: number) => ({

interface AssertFunctionNamesOptions {
additionalAssertFunctionNames?: string[];
};
}

export function getAdditionalAssertFunctionNames(context: Rule.RuleContext): string[] {
const globalSettings = (context.settings as Settings).playwright?.additionalAssertFunctionNames ?? [] as string[]
const ruleSettings = (context.options[0] as AssertFunctionNamesOptions | undefined)?.additionalAssertFunctionNames ?? [] as string[]
export function getAdditionalAssertFunctionNames(
context: Rule.RuleContext
): string[] {
const globalSettings =
(context.settings as Settings).playwright?.additionalAssertFunctionNames ??
([] as string[]);
const ruleSettings =
(context.options[0] as AssertFunctionNamesOptions | undefined)
?.additionalAssertFunctionNames ?? ([] as string[]);

return [...globalSettings, ...ruleSettings]
return [...globalSettings, ...ruleSettings];
}
4 changes: 2 additions & 2 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export type KnownCallExpression = ESTree.CallExpression & {

export interface Settings {
playwright?: {
additionalAssertFunctionNames?: readonly string[]
}
additionalAssertFunctionNames?: readonly string[];
};
}
30 changes: 22 additions & 8 deletions test/spec/expect-expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ runRuleTester('expect-expect', rule, {
`,
errors: [{ messageId: 'noAssertions' }],
settings: {
playwright: { additionalAssertFunctionNames: ['wayComplexCustomCondition'] },
playwright: {
additionalAssertFunctionNames: ['wayComplexCustomCondition'],
},
},
},
{
Expand All @@ -40,7 +42,9 @@ runRuleTester('expect-expect', rule, {
})
`,
errors: [{ messageId: 'noAssertions' }],
options: [{ additionalAssertFunctionNames: ['wayComplexCustomCondition'] }],
options: [
{ additionalAssertFunctionNames: ['wayComplexCustomCondition'] },
],
},
{
name: 'Global settings no false positives',

Check warning on line 50 in test/spec/expect-expect.spec.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Object properties should be sorted alphabetically

Check warning on line 50 in test/spec/expect-expect.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Object properties should be sorted alphabetically
Expand All @@ -51,9 +55,13 @@ runRuleTester('expect-expect', rule, {
`,
errors: [{ messageId: 'noAssertions' }],
settings: {
playwright: { additionalAssertFunctionNames: ['wayComplexGlobalCustomCondition'] },
playwright: {
additionalAssertFunctionNames: ['wayComplexGlobalCustomCondition'],
},
},
options: [{ additionalAssertFunctionNames: ['wayComplexRuleCustomCondition'] }],
options: [
{ additionalAssertFunctionNames: ['wayComplexRuleCustomCondition'] },
],
},
],
valid: [
Expand Down Expand Up @@ -87,8 +95,10 @@ runRuleTester('expect-expect', rule, {
})
`,
settings: {
playwright: { additionalAssertFunctionNames: ['assertCustomCondition'] }
}
playwright: {
additionalAssertFunctionNames: ['assertCustomCondition'],
},
},
},
{
name: 'Rule settings only',

Check warning on line 104 in test/spec/expect-expect.spec.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Object properties should be sorted alphabetically

Check warning on line 104 in test/spec/expect-expect.spec.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Object properties should be sorted alphabetically
Expand All @@ -111,9 +121,13 @@ runRuleTester('expect-expect', rule, {
})
`,
settings: {
playwright: { additionalAssertFunctionNames: ['assertCustomCondition'] },
playwright: {
additionalAssertFunctionNames: ['assertCustomCondition'],
},
},
options: [{ additionalAssertFunctionNames: ['wayComplexCustomCondition'] }],
options: [
{ additionalAssertFunctionNames: ['wayComplexCustomCondition'] },
],
},
],
});

0 comments on commit a122c85

Please sign in to comment.