Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Allow presets to export function returning config object #15306

Open
Filipoliko opened this issue Sep 16, 2024 · 1 comment
Open

Comments

@Filipoliko
Copy link

Filipoliko commented Sep 16, 2024

🚀 Feature Proposal

Similar to current option for jest config file to export function returning an object (as described in docs)

/** @returns {Promise<import('jest').Config>} */
module.exports = async () => {
  return {
    verbose: true,
  };
};

Add same option to jest presets. Currently, if you export function in your jest-preset.js file, it is sort of ignored without showing any error. Due to lack of (or my inability to find) documentation about how to write your own jest presets, I assumed, that I can write preset file the same way as I would write my jest.config.js file, but I ran into this inconsistency.

Motivation

This feature will show a clear way, how to create async presets as you can simply export an async function and do all your async stuff inside. ESM supports top-level await, but it does not work in CJS and it has its own caveats. Exporting async function, which is awaited by jest, is a much cleaner solution.

This feature will also allow users to create jest-preset file by simply moving it to separate file / package and creating reference from jest.conf file. This is possible in most cases right now, but the scenario, where jest.conf is exporting function is not covered by jest-preset. This will make the preset feature more complete as it will reflect the jest.conf file behaviour.

Currently, you can do something like this as a workaround, but it is undocumented behaviour, which I'm worried, might break in future releases.

module.exports = (async () => {
  return {
    verbose: true,
  };
})();

Example

Let's have an npm package @my/package with a jest-preset.js file.

// @my/package/jest-preset.js

/** @returns {Promise<import('jest').Config>} */
module.exports = async () => {
  return {
    verbose: true,
  };
};

In my project jest.conf.js file, I should be able to do this.

// jest.conf.js

/** @type {import('jest').Config} */
const config = {
  preset: '@my/package',
};

module.exports = config;

This should set verbose: true in my jest test run.

Pitch

This feature would unify the behaviour of jest config and jest presets. It will also make it more straightforward to implement async jest presets.

If this feature request is approved, I would love to work on the PR.

@Filipoliko
Copy link
Author

I just noticed, that this has been proposed by @unional but it did not get any hype at that point of time. Maybe it will work out this time? There is at least 2 of us 😆

#14177

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

No branches or pull requests

1 participant