crcollver
crcollver

Reputation: 13

Typing TestCafe userVariables configuration with Typescript?

I've been trying to work with the .testcaferc.js configuration file in my project to consolidate some configuration that has been spread out until now.

Regarding the userVariables configuration, I'm unsure of how to type this so that the object is not unknown. I could just import the variables from .testcaferc.js directly whenever I need to use a userVariable, but that is not the example that is shown in the docs.

Does anyone know the preferred way to go about this? Should I use a declaration file here? I really like the idea of this global configuration file and would love to get it to work, but so far it's been a bit difficult with Typescript it seems especially when working with global hooks.

Thanks!

Upvotes: 0

Views: 230

Answers (1)

Artem B
Artem B

Reputation: 91

It is not possible to generate these types during tests' compilation. However, you can define your own interface with all desired types.

project\test\user-variables\index.ts:

import { userVariables } from 'testcafe'

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

   result?: string
}

export const typedUserVariables = userVariables as TypedUserVariables;

project\test\test.ts:

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

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

Please let me know if this approach is suitable for you.

Regards, Artemy

Upvotes: 1

Related Questions