testcafe icon indicating copy to clipboard operation
testcafe copied to clipboard

Error reading .testcaferc.js config file

Open lyqht opened this issue 2 years ago • 3 comments

What is your Scenario?

I would like to use the userVariables as a means to access my environment variables.

.testcaferc.js

import toggles from "../src/config/toggles";
module.exports = {
  userVariables: {
    featureXEnabled: toggles.isFeatureXEnabled()
  }
}

toggles file

class Toggles {
  public get featureXEnabled(): string {
    return get(window, "__ENV.REACT_APP_INTERVENTIONS_TOGGLE", "off");
  }
}

export default new Toggles();

What is the Current behavior?

> NODE_ENV=test TZ='Asia/Singapore' BASE_URL=localhost:3000 testcafe chrome ./e2e/cohort.test.ts --dev --disable-page-caching --config-file ./e2e/.testcaferc.js

An error has occurred while reading the "/Users/lyqht/proj/e2e/.testcaferc.js" configuration file.

Testcafe ignores this error and proceeds running the tests which will fail.

  1) TypeError: Cannot read property 'featureX' of null

What is the Expected behavior?

Be able to read the env file

TestCafe version

1.18.5

lyqht avatar Apr 18 '22 07:04 lyqht

Hi @lyqht,

The .testcaferc.js file (and its dependencies) should be written using the CommonJS syntax.

const toggles = require("../src/config/toggles");

module.exports = {
  userVariables: {
    featureXEnabled: toggles.isFeatureXEnabled()
  }
}

miherlosev avatar Apr 19 '22 06:04 miherlosev

Then why do the docs use import?

image

qualityshepherd avatar Jul 11 '22 18:07 qualityshepherd

In this example, the CommonJS syntax is used in the upper part of the documentation on the configuration: https://testcafe.io/documentation/402638/reference/configuration-file#javascript. But yes, there is a mistake in the Test Hooks part. We'll fix it.

Aleksey28 avatar Jul 12 '22 06:07 Aleksey28

The documentation has been updated. It may take a while for the website to reflect these changes.

titerman avatar Oct 19 '22 11:10 titerman

Hi! I'm trying to do it but I got the same issue. I'm using the CJS form:

imagen

But i got the same result

imagen

This is the module:

imagen

I also tried to change all files to JS but it's the same result.

Could anyone help me out with this please?

Victorgabarullo avatar Mar 29 '23 20:03 Victorgabarullo

Hi @Victorgabarullo,

Utils scripts for the config file should be created with the same rules as the config file. The config file supports only CommonJS syntax. It means that you can't use import/export from ESM and you can't use TypeScript.

Aleksey28 avatar Mar 31 '23 08:03 Aleksey28

Thanks @Aleksey28 I'll fix my code in that case. Thank you for the response

Victorgabarullo avatar Apr 03 '23 13:04 Victorgabarullo