You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow .cjs extension for the configuration file #6911
I work with a Node.js project with "type": "module" defined in package.json
So .js files are loaded as ES modules, while testcafe expects a module.exports statement (CommonJS shape) in .testcaferc.js
thus testcafe is not able to use that file.
If I set the .cjs extension to .testcaferc ("Regardless of the value of the "type" field, .mjs files are always treated as ES modules and .cjs files are always treated as CommonJS") testcafe won't recognize it.
What are you suggesting?
Some popular tools (eslint, prettier, mocha) allow .cjs extension to their configuration file.
It would be nice if testcafe allow the same.
What alternatives have you considered?
.json extension is the only alternative but it do not allow to create dynamic configuration files.
Additional context
No response
Reacted by JordanAtBridgit, groetzi, Mark and Alex Fournier
I find myself in a situation where I want to use global before/after hooks which, according to the docs, are configured through the JS version of the config file. When I converted the existing JSON config to the JS format, I started getting the error An error has occurred while reading the "path/to/.testcaferc.js" configuration file. I had to poke around in configuration-base.js to see that the error was reported as so: Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/.testcaferc.js from /path/to/node_modules/testcafe/lib/configuration/configuration-base.js not supported. .testcaferc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename .testcaferc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/jordan/repos/rp-web/uitests/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
I can't change the require() in configuration-base.js, and I have way too many existing import statements to just remove the type: "modules in my package.json. When I try changing the extension of my config file to .cjs, it gets loaded as a JSON file since the extension isn't recognized anymore. This leads to parsing errors, as the JS version is obviously not valid JSON.
If I may be so bold as to offer a suggestion, if the function that identifies the config as JS vs JSON were to also allow for the .cjs extension, this appears to work perfectly without requiring me to greatly refactor or otherwise change global settings in my code. I gave it a quick-and-dirty test drive locally, and it does seem to work fine, but that's obviously a long way from being properly tested.
If that approach sounds reasonable, I can open a PR.
Activity
JordanAtBridgit commentedon Mar 4, 2022
This is exactly what I came to inquire about.
I find myself in a situation where I want to use global before/after hooks which, according to the docs, are configured through the JS version of the config file. When I converted the existing JSON config to the JS format, I started getting the error
An error has occurred while reading the "path/to/.testcaferc.js" configuration file.I had to poke around in configuration-base.js to see that the error was reported as so:Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/.testcaferc.js from /path/to/node_modules/testcafe/lib/configuration/configuration-base.js not supported. .testcaferc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename .testcaferc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/jordan/repos/rp-web/uitests/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).I can't change the require() in
configuration-base.js, and I have way too many existingimportstatements to just remove thetype: "modulesin my package.json. When I try changing the extension of my config file to.cjs, it gets loaded as a JSON file since the extension isn't recognized anymore. This leads to parsing errors, as the JS version is obviously not valid JSON.If I may be so bold as to offer a suggestion, if the function that identifies the config as JS vs JSON were to also allow for the
.cjsextension, this appears to work perfectly without requiring me to greatly refactor or otherwise change global settings in my code. I gave it a quick-and-dirty test drive locally, and it does seem to work fine, but that's obviously a long way from being properly tested.If that approach sounds reasonable, I can open a PR.
alexfi1in commentedon Mar 7, 2022
Hi folks,
Thanks for pointing this out to us. It's a good enhancement of the
Configurationsubsystem.alex-fournier commentedon Apr 6, 2023
Hello, a PR has been submitted: #7614
Add possibility to load config file from cjs file (#7614)
github-actions commentedon May 5, 2023
Release v2.6.0-rc.1 addresses this.