Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

coffeebeats/example-jest-bug-overwrite-transform-config

Repository files navigation

example-jest-bug-overwrite-transform-config

NOTE: This bug (#13769) was addressed in #13770.

Explanation

This example repository demonstrates a bug in jest (as of version 29.3.1).

When providing custom "transform" entries via the Jest configuration object, if two entries use the same processor, then any configuration passed to the first entry will be overwritten by the second entry.

This is caused by the implementation of transformer loading, where transformer configuration is cached by preprocessor filepaths alone; the cache does not consider the case where two different transform patterns use the same preprocessor with differing options:

async loadTransformers(): Promise<void> {
    await Promise.all(
      this._config.transform.map(
        async ([, transformPath, transformerConfig]) => {
          let transformer: Transformer | TransformerFactory<Transformer> =
            await requireOrImportModule(transformPath);

          if (transformer == null) {
            throw new Error(makeInvalidTransformerError(transformPath));
          }
          if (isTransformerFactory(transformer)) {
            transformer = transformer.createTransformer(transformerConfig);
          }
          if (
            typeof transformer.process !== 'function' &&
            typeof transformer.processAsync !== 'function'
          ) {
            throw new Error(makeInvalidTransformerError(transformPath));
          }
          const res = {transformer, transformerConfig};

          /*
           * The bug manifests here, where the 'transformPath' from
           * the latest 'transform' entry will overwrite 'res' completely.
           */
          this._transformCache.set(transformPath, res);
        },
      ),
    );

    this._transformsAreLoaded = true;
  }

How to test

There is a simple test case set up that simply imports a few files. A custom, no-op preprocessor is provided that will error if passed { fail: true }. The Jest configuration file provided has four different options, demonstrating how configurations can get overridden.

Simply uncomment one of the configurations provided jest.config.js and run yarn test.

About

An example repository which demonstrates a Jest bug where preprocessor configs are overwritten by the last entry.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published