jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: Allow `customExportConditions` to be set on a per-module basis

Open rschristian opened this issue 1 year ago • 0 comments

🚀 Feature Proposal

Allow users to set specific export conditions (perhaps as an override) for specific modules.

Motivation

Setting export conditions for every module at once is sometimes painting with too broad of a brush; often we need to make alterations for specific packages to ensure a particular build is (or isn't) consumed.

In Preact, for instance, we use exports.browser to point to an ESM build in a CJS package with a .js file extension. While this doesn't adhere to Node's semantics, it doesn't need to as it's not for use in Node. Using .js allows for better back compatibility with file servers that might not handle .mjs correctly. Jest, however, does apply Node semantics despite running in an emulation of a browser (jsdom).

While we can set "customExportConditions": [ "node", "node-addons" ] to get Preact itself working, this is a poor hack; many libraries do need to consume exports.browser in a jsdom environment, so overriding for all modules isn't ideal.

Ideally, Jest wouldn't apply Node semantics to non-Node environments, but failing that (and being more reasonable, as I imagine that'd be a mountain of work), it would be great if we could supply export conditions for specific modules.

Example

Perhaps something like the following, config-wise:

{
    "testEnvironmentOptions": {
        "customExportConditions": [
            {
                "moduleX": ["browser"]
            },
            "node",
            "node-addons"
        ]
    }
}

(Not sure if this is consistent with Jest's config scheme, but hopefully this can paint a clear enough picture)

Pitch

It would be an extension to an existing config option.

rschristian avatar Jan 25 '23 23:01 rschristian