jest-chain-transform icon indicating copy to clipboard operation
jest-chain-transform copied to clipboard

Add support for `<rootDir>`

Open eboekhold opened this issue 3 years ago • 1 comments

Hey, thanks so much for the package!

I have a custom transform I want to chain after babel-jest, and being able to use this package to do so has been a great help to me!

This custom transform's file lives in my project directory as a separate .js file. When using it without chaining it, I could apply it by doing the following in jest.config.js:

const config = {
  transform: {
    '\\.[jt]sx?$': '<rootDir>/frontend/webpack_polyfill.js',
  },
};

However, using <rootDir> when chaining to another transformer using jest-chain-transform raises an exception:

const config = {
  transform: {
    '\\.[jt]sx?$': [
      'jest-chain-transform',
      {
        transformers: [
          'babel-jest',
          '<rootDir>/frontend/webpack_polyfill.js',
        ],
      },
    ],
  },
};

Error:

Cannot find module '<rootDir>/frontend/webpack_polyfill.js'
      at requireTransformer (../node_modules/jest-chain-transform/lib/transformer.js:51:24)
      at flatTransformers (../node_modules/jest-chain-transform/lib/transformer.js:66:33)
      at getFlattenTransformers (../node_modules/jest-chain-transform/lib/transformer.js:85:31)
      at Object.getCacheKey (../node_modules/jest-chain-transform/lib/transformer.js:94:32)
      at ScriptTransformer._getCacheKey (../node_modules/@jest/transform/build/ScriptTransformer.js:281:41)
      at ScriptTransformer._getFileCachePath (../node_modules/@jest/transform/build/ScriptTransformer.js:352:27)

I did manage to figure out that I could get the correct transform to be used by passing a path relative to ../node_modules/jest-chain-transform/lib/transformer.js:

transformers: [
  'babel-jest',
  '../../../spec/frontend/webpack_polyfill.js',
]

Although this does work, I do feel like this path is easy to break accidentally.

My first suggestion would be to add support for <rootDir>^1 to this package, so that transformers can be configured in a similar way as in the rest of jest's config.

Another option that would work for me is to make my transformer into a package. If I understand correctly, I then should be able to apply it by just configuring jest-chain-transform to import 'webpack-polyfill' instead of '../../../spec/frontend/webpack_polyfill.js'.

eboekhold avatar Jun 15 '22 12:06 eboekhold

Hello, I suggest you can use (assume your jest.config.js is under project root directory)

[
   path.join(__dirname, spec/frontend/webpack_polyfill.js)
]

which is more stable than ../../../spec/frontend/webpack_polyfill.js

And I will look into your proposal to support <rootDir>

anc95 avatar Jun 16 '22 01:06 anc95