镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Allow .cjs extension for the configuration file #6911

Closed

Description

@tipy01

What is your Scenario?

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

Activity

  1. JordanAtBridgit commented on Mar 4, 2022

    @JordanAtBridgit

    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 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.

  2. alexfi1in commented on Mar 7, 2022

    @alexfi1in
    Contributor

    Hi folks,

    Thanks for pointing this out to us. It's a good enhancement of the Configuration subsystem.

  3. alex-fournier commented on Apr 6, 2023

    @alex-fournier

    Hello, a PR has been submitted: #7614

  4. removed
    STATE: Need responseAn issue that requires a response or attention from the team.
    on Apr 10, 2023
  5. added a commit that references this issue on May 3, 2023
    2e6dee3
  6. github-actions commented on May 5, 2023

    @github-actions

    Release v2.6.0-rc.1 addresses this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions