Skip to content

Merge preset globals with jest.config.js globals #9026

Closed
@catfireparty

Description

@catfireparty

🚀 Feature Proposal

Merge the globals property from any preset into the project configuration rather than overwriting it.

Motivation

Some third party libraries (e.g. ts-jest) use the globals property to provide configuration. It would be useful to be able to provide shared configuration and only override certain fields within the globals.

Example

The preset could look something like this :

module.exports = {
  globals: {
    'ts-jest': {
      stringifyContentPathRegex: '\\.html$',
      astTransformers: [
        require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')
      ],
      isolatedModules: true
    }
  }
}

And the project config could simply contain the following :

module.exports = {
  globals: {
    'ts-jest': {
      tsConfig: './tsconfig.spec.json',
    }
  }
}

(Or similar)

This would result in an options object which contained :

  globals: {
    'ts-jest': {
      tsConfig: './tsconfig.spec.json',
      stringifyContentPathRegex: '\\.html$',
      astTransformers: [
        require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')
      ],
      isolatedModules: true
    }
  }

This isn't limited to the configuration of third party libraries, but this may be the most obvious use case. A simple repro repo that illustrates how globals currently gets clobbered is available at https://github.com/jdpearce/jest-globals-test

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions