diff --git a/examples/type-check-for-meta/README.md b/examples/type-check-for-meta/README.md new file mode 100644 index 0000000..04cee7a --- /dev/null +++ b/examples/type-check-for-meta/README.md @@ -0,0 +1,43 @@ +# Type Checks for Test Metadata + +You can restrict the range of acceptable values or value types for your test metadata. This approach simplifies test maintainance and development. + +1. The `custom-typing.ts` file defines the `MetaOptions` data type. + + ```ts + type MetaOptions = { + device: 'mobile' | 'desktop' | 'tablet'; + skip: boolean; + }; + ``` + +2. The `TestFn` interface modifies the `test.meta` TestCafe method. The method now expects arguments of the `MetaOptions` data type. + + ```ts + declare module 'testcafe' { + global { + interface TestFn { + meta(options: MetaOptions): this; // restricted to MetaOptions + meta(options: T): this; // generic fallback + } + } + } + ``` + +3. To apply these rules, import the `./custom-typing` file into `index.ts`. + + ```ts + import './custom-typing'; + ``` + +When TestCafe compiles the `index.ts` test, it checks `test.meta` parameters against the list of known keys and values. + +The compilation fails if your test metadata includes an unknown key or an unacceptable value: + +```plaintext +Argument of type '{ device: "desktop"; skip: false; priority: any; }' is not assignable to parameter of type'MetaOptions'. +``` + +```plaintext +Type '"cloud"' is not assignable to type '"mobile" | "desktop" | "tablet"'. +``` diff --git a/examples/type-check-for-meta/custom-typing.ts b/examples/type-check-for-meta/custom-typing.ts new file mode 100644 index 0000000..8fd2ed1 --- /dev/null +++ b/examples/type-check-for-meta/custom-typing.ts @@ -0,0 +1,13 @@ +type MetaOptions = { + device: 'mobile' | 'desktop' | 'tablet'; + skip: boolean; +}; + +declare module 'testcafe' { + global { + interface TestFn { + meta(options: MetaOptions): this; + meta(options: T): this; + } + } +} diff --git a/examples/type-check-for-meta/index.ts b/examples/type-check-for-meta/index.ts new file mode 100644 index 0000000..794bd0a --- /dev/null +++ b/examples/type-check-for-meta/index.ts @@ -0,0 +1,29 @@ +import './custom-typing'; + +fixture `Fixture with meta device: desktop` + .meta({ device: 'desktop' }) + .page `https://devexpress.github.io/testcafe/example`; + +test + .meta({ device: 'mobile', skip: false }) + ('test with meta device: mobile and skip: false', async t => { + await t.wait(1000); + }); + +test + .meta({ device: 'mobile', skip: false }) + ('test with meta device: mobile and skip: false', async t => { + await t.wait(1000); + }); + +test + .meta({ device: 'desktop', skip: false }) + ('test with meta device: desktop and skip: false', async t => { + await t.wait(1000); + }); + +test + .meta({ device: 'tablet', skip: false }) + ('test with meta device: tablet and skip: false', async t => { + await t.wait(1000); + });