diff --git a/docs/rules/no-skipped-test.md b/docs/rules/no-skipped-test.md index 9a61005..dc9f6ae 100644 --- a/docs/rules/no-skipped-test.md +++ b/docs/rules/no-skipped-test.md @@ -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'); +}); +``` diff --git a/src/rules/no-skipped-test.ts b/src/rules/no-skipped-test.ts index e014025..3a2b45e 100644 --- a/src/rules/no-skipped-test.ts +++ b/src/rules/no-skipped-test.ts @@ -10,6 +10,8 @@ export default { create(context) { return { CallExpression(node) { + const options = context.options[0] || {}; + const allowConditional = !!options.allowConditional; const { callee } = node; if ( @@ -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, @@ -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; diff --git a/test/spec/no-skipped-test.spec.ts b/test/spec/no-skipped-test.spec.ts index 0c25fce..2581abc 100644 --- a/test/spec/no-skipped-test.spec.ts +++ b/test/spec/no-skipped-test.spec.ts @@ -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 }], + }, ], });