使用 Typescript 输入 TestCafe userVariables 配置?

问题描述 投票:0回答:1

我一直在尝试使用项目中的

.testcaferc.js
配置文件来整合一些到目前为止已经分散的配置。

关于 userVariables 配置,我不确定如何输入它以使该对象不是

unknown
。每当我需要使用
.testcaferc.js
时,我都可以直接从
userVariable
导入变量,但这不是 docs 中显示的示例。

有人知道解决这个问题的首选方法吗?我应该在这里使用声明文件吗?我真的很喜欢这个全局配置文件的想法,并且很想让它发挥作用,但到目前为止,使用 Typescript 似乎有点困难,尤其是在使用全局钩子时。

谢谢!

typescript testing automated-tests e2e-testing testcafe
1个回答
1
投票

在测试编译期间不可能生成这些类型。但是,您可以使用所有所需的类型定义自己的接口。

项目 est\用户变量\index.ts:

import { userVariables } from 'testcafe'

export interface TypedUserVariables {
   [key: string]: unknown;

   result?: string
}

export const typedUserVariables = userVariables as TypedUserVariables;

项目est est.ts:

import { Selector } from 'testcafe';
import { typedUserVariables } from './user-variables';

test('Test', async t => {
   const typedStringVariable = typedUserVariables.result; //Will be a string here
})

请告诉我这种方法是否适合您。

问候, 阿尔特米

© www.soinside.com 2019 - 2024. All rights reserved.