Skip to content

Commit

Permalink
feat: Add allowConditional option to no-skipped-test (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton authored Oct 22, 2023
1 parent 2927305 commit 34b3f7b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/rules/no-skipped-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,31 @@ test.describe('two tests', () => {
test('two', async ({ page }) => {});
});
```

## Options

```json
{
"playwright/no-skipped-test": [
"error",
{
"allowConditional": false
}
]
}
```

### `allowConditional`

Setting this option to `true` will allow using `test.skip()` to
[conditionally skip a test](https://playwright.dev/docs/test-annotations#conditionally-skip-a-test).
This can be helpful if you want to prevent usage of `test.skip` being added by
mistake but still allow conditional tests based on browser/environment setup.

Example of **correct** code for the `{ "allowConditional": true }` option:

```javascript
test('foo', ({ browserName }) => {
test.skip(browserName === 'firefox', 'Still working on it');
});
```
20 changes: 20 additions & 0 deletions src/rules/no-skipped-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default {
create(context) {
return {
CallExpression(node) {
const options = context.options[0] || {};
const allowConditional = !!options.allowConditional;
const { callee } = node;

if (
Expand All @@ -19,6 +21,12 @@ export default {
) {
const isHook = isTest(node) || isDescribeCall(node);

// If allowConditional is enabled and it's not a test/describe hook,
// we ignore any `test.skip` calls that have no arguments.
if (!isHook && allowConditional && node.arguments.length) {
return;
}

context.report({
messageId: 'noSkippedTest',
node: isHook ? callee.property : node,
Expand Down Expand Up @@ -52,6 +60,18 @@ export default {
noSkippedTest: 'Unexpected use of the `.skip()` annotation.',
removeSkippedTestAnnotation: 'Remove the `.skip()` annotation.',
},
schema: [
{
additionalProperties: false,
properties: {
allowConditional: {
default: false,
type: 'boolean',
},
},
type: 'object',
},
],
type: 'suggestion',
},
} as Rule.RuleModule;
4 changes: 4 additions & 0 deletions test/spec/no-skipped-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,9 @@ runRuleTester('no-skipped-test', rule, {
'this.skip();',
'this["skip"]();',
'this[`skip`]();',
{
code: 'test.skip(browserName === "firefox", "Still working on it");',
options: [{ allowConditional: true }],
},
],
});

0 comments on commit 34b3f7b

Please sign in to comment.