Skip to content

Commit

Permalink
Tmeta example (#86)
Browse files Browse the repository at this point in the history
* fix: get-func-name package vulnerability fixed

* fix: fixed npx testcafe chrome examples command by adding -a node server/index.js

* fix: fixed npx testcafe chrome examples command by adding -a node server/index.js

* double quotes added to examples start command

* update: added a new example on strict type checking for meta

* fix: comments removed

* update: example type-check-for-meta updated with generic and custom typing

* fix: npm run examples fixed

* Update examples/type-check-for-meta/index.ts

Co-authored-by: aleks-pro <[email protected]>

* Update examples/type-check-for-meta/index.ts

Co-authored-by: aleks-pro <[email protected]>

* Update examples/type-check-for-meta/index.ts

Co-authored-by: aleks-pro <[email protected]>

* Update examples/type-check-for-meta/custom-typing.ts

Co-authored-by: aleks-pro <[email protected]>

* fix: pr comments fixed

* Update examples/type-check-for-meta/index.ts

Co-authored-by: aleks-pro <[email protected]>

* fix: pr comments fixed

* Meta type check README

* typo fix

---------

Co-authored-by: aleks-pro <[email protected]>
Co-authored-by: Eugene Titerman <[email protected]>
  • Loading branch information
3 people authored May 31, 2024
1 parent 4e90e83 commit 2841ab6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/type-check-for-meta/README.md
Original file line number Diff line number Diff line change
@@ -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<T>(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"'.
```
13 changes: 13 additions & 0 deletions examples/type-check-for-meta/custom-typing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type MetaOptions = {
device: 'mobile' | 'desktop' | 'tablet';
skip: boolean;
};

declare module 'testcafe' {
global {
interface TestFn {
meta(options: MetaOptions): this;
meta<T>(options: T): this;
}
}
}
29 changes: 29 additions & 0 deletions examples/type-check-for-meta/index.ts
Original file line number Diff line number Diff line change
@@ -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<MetaOptions>({ device: 'desktop', skip: false })
('test with meta device: desktop and skip: false', async t => {
await t.wait(1000);
});

test
.meta<MetaOptions>({ device: 'tablet', skip: false })
('test with meta device: tablet and skip: false', async t => {
await t.wait(1000);
});

0 comments on commit 2841ab6

Please sign in to comment.