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

Support an "extends" option within config #3564

Closed
milesj opened this issue May 13, 2017 · 13 comments
Closed

Support an "extends" option within config #3564

milesj opened this issue May 13, 2017 · 13 comments

Comments

@milesj
Copy link

milesj commented May 13, 2017

Do you want to request a feature or report a bug?

New feature.

What is the current behavior?

It does not exist. Perhaps it can replace preset?

What is the expected behavior?

Currently, we have a preset configuration option that allows us to extend a pre-defined configuration from an NPM package. This works for the most part, but is quite lacking in functionality, specifically:

  • Only 1 preset is allowed at a time.
  • It requires a jest-preset.json to exist in an NPM package.
  • It does not support JS files (jest-preset.config.js) or JSON5 files (jest-preset.json5).
  • It does not support relative or absolute file system paths.
  • It should recursively merge all extended configs.

My proposal is to support all the above mentioned, similar to babel or eslint.

A few examples:

  1. Support single packages:
extends: 'foo',
// OR
extends: ['foo'],
  1. Support multiple packages:
extends: ['foo', 'bar'],
  1. Support relative or absolute files:
extends: './config/jest.json',
// OR
extends: [
  './config/jest.json',
  './node_modules/foo/jest.js',
],
  1. JSON, JSON5, and JS configurations would all merge recursively.
extends: [
  'foo', // jest-preset.json?
  './config/jest.json5',
  './node_modules/foo/jest.js',
],
  1. Parse configs depending on extension. If it ends in .json or .json5, parse it with JSON5. If it ends with .js, simply require it and use the exported object.

Why the feature request?

I would like to extend the react-native preset, as well as my personal preset, but I currently cannot do this. This proposal also aligns the build tool with other existing build tools.

If this is a valid feature request, I can look into submitting a PR.

@milesj
Copy link
Author

milesj commented Jul 18, 2017

Bumping this as it would be extremely useful.

@cpojer
Copy link
Member

cpojer commented Jul 18, 2017

Jest supports a .js config file, so we don't need this.

@cpojer cpojer closed this as completed Jul 18, 2017
@milesj
Copy link
Author

milesj commented Jul 18, 2017

@cpojer jest-preset.json within node modules does not however.

@basickarl
Copy link

basickarl commented Aug 6, 2019

@cpojer May you eloborate that this is already in the .js file. I'll give my setup and how I'd like to configure it.

I have "development" and "production" test configurations. The development test configurations are for the developers when developing the code. The production test configurations are for the CI pipeline tested against compiled code which will be running later on in production environments e.g. minimizing files, removing comments, removing sourcemapping etc..

I would like for the development configuration to have certain different values on just a few of the fields, hence the extend. All out configurations are written in JSON and we would like to continue to have everything in JSON.

I had a look at the projects field, however I'd like to somehow define which projects to run when invoking jest.

@SimenB
Copy link
Member

SimenB commented Aug 6, 2019

There are no plans to do any merging beyond preset. You'll need to use a js file, where it's easy to use object spread or Object.assign to change a few properties. Jest itself does this for CI: https://github.com/facebook/jest/blob/e740de246b3a5ef4a7692f2f58bf8223323e4daf/jest.config.ci.js

@goldhand
Copy link

goldhand commented Mar 5, 2020

Would be nice to support this. While using js and Object.assign is easy, I'd prefer to avoid the extra config files in monorepos if possible. Being able to describe "jest": {"extends": "<rootDir>/../jest.base.js"} in package.json would be a little cleaner IMO

@sguillia
Copy link

Object.assign is great if one already knows where the existing configuration lives.

If one doesn't know, one has to write code to search for jest.config.js or walk the parent directories to find a package.json with the top-level "jest" key.

We can use jest --showConfig to have Jest look up the config for us. But Jest is unable to ingest the output of --showConfig.

@goldhand
Copy link

goldhand commented Aug 3, 2020

Any chance this could be reopened @cpojer ? See my previous explanation above for why .js is not enough for some use cases.

@olesiaMartushkanova
Copy link

I totally agree with @goldhand , having multiple config files also means having more work to maintain this multiple files.
Could we please have something like this implemented: jest": {"extends": "<rootDir>/../jest.base.js"} , @cpojer

@askirmas
Copy link

askirmas commented May 6, 2021

jest.config is not tsconfig, lmao

@olesiaMartushkanova
Copy link

jest.config is not tsconfig, lmao

But would be good if it had the same cool features.

@dimitropoulos
Copy link
Contributor

following up from #11399:

Only 1 preset is allowed at a time.

❌ This still seems to be the case with presets

It requires a jest-preset.json to exist in an NPM package.

✔️ This is not the case, from a deeply nested package I am pointing to a file at the root and it's working

It does not support JS files (jest-preset.config.js) or JSON5 files (jest-preset.json5).

✔️ All of my files are jest.config.js and it's working so far.

It does not support relative or absolute file system paths.

✔️ I am using a relative path, e.g. preset: '../../../jest.config.js' and it's working.

It should recursively merge all extended configs.

♻️ From reading the changelog, it seems like there has been some work on this like #9027 and #5505 so it's at least partially done (both of which were opened some time after this PR).

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants