Jest: Support an "extends" option within config

Created on 13 May 2017  路  9Comments  路  Source: facebook/jest

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'],

2) Support multiple packages:

extends: ['foo', 'bar'],

3) Support relative or absolute files:

extends: './config/jest.json',
// OR
extends: [
  './config/jest.json',
  './node_modules/foo/jest.js',
],

4) JSON, JSON5, and JS configurations would all merge recursively.

extends: [
  'foo', // jest-preset.json?
  './config/jest.json5',
  './node_modules/foo/jest.js',
],

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

Enhancement Feature Request

Most helpful comment

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

All 9 comments

Bumping this as it would be extremely useful.

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

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

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

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

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

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings