From 90e7b9ebacbd597b7380522001eb6d17ee9c3d86 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 29 Aug 2024 14:16:49 -0700 Subject: [PATCH] chore: enforce tags format via typescript types (#32384) Leverage [template literal types](https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html). Fixes https://github.com/microsoft/playwright/issues/32382 --- packages/playwright/types/test.d.ts | 4 +++- tests/playwright-test/test-tag.spec.ts | 12 ++++++++++++ utils/generate_types/overrides-test.d.ts | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 0131cb16c9f7d..5efdca46582da 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -1825,8 +1825,10 @@ type TestDetailsAnnotation = { description?: string; }; +type TestDetailsTag = `@${string}`; + export type TestDetails = { - tag?: string | string[]; + tag?: TestDetailsTag | TestDetailsTag[]; annotation?: TestDetailsAnnotation | TestDetailsAnnotation[]; } diff --git a/tests/playwright-test/test-tag.spec.ts b/tests/playwright-test/test-tag.spec.ts index 9487e31ea3a42..0587cfe7a8cc7 100644 --- a/tests/playwright-test/test-tag.spec.ts +++ b/tests/playwright-test/test-tag.spec.ts @@ -147,6 +147,18 @@ test('should enforce @ symbol', async ({ runInlineTest }) => { expect(result.output).toContain(`Error: Tag must start with "@" symbol, got "foo" instead.`); }); +test('types should enforce @ symbol', async ({ runTSC }) => { + const result = await runTSC({ + 'stdio.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('test1', { tag: 'foo' }, () => { + }); + ` + }); + expect(result.exitCode).toBe(2); + expect(result.output).toContain('error TS2322: Type \'"foo"\' is not assignable to type \'`@${string}` | `@${string}`[] | undefined'); +}); + test('should be included in testInfo', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ 'a.test.ts': ` diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 5c108d7b2591a..76fecc524adfa 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -70,8 +70,10 @@ type TestDetailsAnnotation = { description?: string; }; +type TestDetailsTag = `@${string}`; + export type TestDetails = { - tag?: string | string[]; + tag?: TestDetailsTag | TestDetailsTag[]; annotation?: TestDetailsAnnotation | TestDetailsAnnotation[]; }