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

Add support for <rootDir> #6

Open
ajclaasen opened this issue Jun 15, 2022 · 1 comment
Open

Add support for <rootDir> #6

ajclaasen opened this issue Jun 15, 2022 · 1 comment

Comments

@ajclaasen
Copy link

ajclaasen commented Jun 15, 2022

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

Footnotes

  1. https://jestjs.io/docs/configuration#rootdir-string

@ajclaasen ajclaasen changed the title Brittle path when transformer is a local file instead of an installed package Brittle path when transformer is a local file Jun 15, 2022
@ajclaasen ajclaasen changed the title Brittle path when transformer is a local file Brittle path when transformer is local file Jun 15, 2022
@ajclaasen ajclaasen changed the title Brittle path when transformer is local file Add support for <rootDir> Jun 15, 2022
@ajclaasen ajclaasen changed the title Add support for <rootDir> Add support for <rootDir> Jun 15, 2022
@anc95
Copy link
Owner

anc95 commented Jun 16, 2022

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants