-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
4e90e83
commit 2841ab6
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"'. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |