testcafe
testcafe copied to clipboard
Error reading .testcaferc.js config file
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
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()
}
}
Then why do the docs use import
?

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.
The documentation has been updated. It may take a while for the website to reflect these changes.
Hi! I'm trying to do it but I got the same issue. I'm using the CJS form:
But i got the same result
This is the module:
I also tried to change all files to JS but it's the same result.
Could anyone help me out with this please?
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.
Thanks @Aleksey28 I'll fix my code in that case. Thank you for the response