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

[request] babel-jest support custom path for .babelrc #3845

Closed
ejfrancis opened this issue Jun 17, 2017 · 22 comments
Closed

[request] babel-jest support custom path for .babelrc #3845

ejfrancis opened this issue Jun 17, 2017 · 22 comments

Comments

@ejfrancis
Copy link

An option to provide an alternative path to the .babelrc file that babel-jest uses would be helpful. I'd like to use a .babelrc in my tests that isn't used in the build of my application code.

@thymikee
Copy link
Collaborator

You can use test env in your .babelrc for now, see the example here.

@vyshkant
Copy link

vyshkant commented Mar 5, 2018

@thymikee @ejfrancis
I think there could be a lot of reasons to move .babelrc out from the root directory. For example, I don't like to store "a million of files" in the project root, so I simply wanna move all the configuration files to a config directory.

But even if I moved jest config, I still can't move my .babelrc, because jest expects it is in the root dir.

Correct me if I'm wrong.

I think this issue should be reopened. I'll gladly write a PR if you agree with my arguments.

@thymikee
Copy link
Collaborator

thymikee commented Mar 5, 2018

I'll reopen, because that's something we should easily allow (so you don't have to write custom transformer). What's your idea to resolve this?

@thymikee thymikee reopened this Mar 5, 2018
@msokk
Copy link

msokk commented Mar 5, 2018

"test" env didn't work for me, had to use snippet from #5324

@rafaelmaruta
Copy link

rafaelmaruta commented Mar 7, 2018

I need this feature too. I put the npm script tests inside of a node module dependency. When I run it, it loses the .babelrc file:

"scripts": {
    "test": "jest src --no-cache --coverage",
    "test:watch": "yarn test --watch"
  },
  "jest": {
    "globals": {
      "sessionStorage": true
    },
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "^.+\\.(s)?css$": "identity-obj-proxy"
    },
    "roots": ["../../"],
    "setupTestFrameworkScriptFile": "<rootDir>/setupTests.js",
    "transform": {
      ".*": "babel-jest"
    }
  }

I think it is because I changed the roots, but I need to keep the .babelrc file together with this package.json, and not where the roots points

@vyshkant
Copy link

vyshkant commented Mar 8, 2018

I'll reopen, because that's something we should easily allow (so you don't have to write custom transformer). What's your idea to resolve this?

AFAIU, the best way is to do the same as in "ts-jest" — to create "babel-jest" section inside the "global" section of jest config file and define there a path to .babelrs:

{
  "globals": {
    "babel-jest": {
      "babelrcFile": "custom/path/to/.babelrc"
    }
  },
}

Then we will need to modify the jest/packages/babel-jest/src/index.js file so that the value from the config (if it is specified) was taken instead of the hard-coded value.

@thymikee I would be very grateful if you explain to me how the transformers are invoked, how and where the functions getCacheKey and process are called and what is the expected behavior of these functions.

@prescottprue
Copy link

I also would love a way to do this without a custom transformer (haven't gotten that working).

@SimenB
Copy link
Member

SimenB commented Mar 28, 2018

I wonder if babel/babel#7472 is enough after we upgrade to babel 7 - if babel can find your config then jest should be able to find your config (unless I misunderstand something)

@zzuhan
Copy link

zzuhan commented Apr 3, 2018

#1468 (comment) this solution work well

@merlinstardust
Copy link

This is also needed if you're working with Meteor where you typically have no .babelrc file at your root directory. Meteor takes care of everything under the hood.

@bsmith-cycorp
Copy link

Any movement on this? It's become doubly important since the addition of Babel 7's wonky config-lookup strategy.

@milesj
Copy link

milesj commented Oct 3, 2018

@bsmith-cycorp That will require this PR: #7016

@bsmith-cycorp
Copy link

I ended up just doing the hack suggested by @zzuhan. It made me die a little bit inside, but it works.

@fyn-dev
Copy link

fyn-dev commented Oct 7, 2018

For me tests is working with babel but build and run the code is not using babel at all. So the temporary solution I use is to move .babelrc to config folder and rename to tests.babelrc

then in package.json I did changes like this

"test": "cp config/tests.babelrc .babelrc; BABEL_ENV=test jest --no-cache"
"start": "rm -rf .babelrc || true; node dist/index.js"
....

and works well

@SimenB
Copy link
Member

SimenB commented Feb 3, 2019

I think a custom transformer using babel-jest is the correct solution here. babelJest.createTransformer(babelOptions).

Docs: https://jestjs.io/docs/en/tutorial-react#custom-transformers
babelOptions documented here.

To be able to pass config to babel-jest without a custom transformer, follow #7288.

@SimenB SimenB closed this as completed Feb 3, 2019
@mschipperheyn
Copy link

mschipperheyn commented Apr 13, 2019

Just as a follow up, for who runs into this. If you are supplying your custom babel config to webpack, as opposed to running it from babel.config.js you will definitely need to use the transformer approach that @SimenB suggests. I extracted my babelConfig config to a separate file that I could load from both my webpack config and my jest transformer file. That worked.

const { createTransformer } = require('babel-jest');
const {
	babelPresets,
	babelPlugins
} = require('../../babelConfig');

module.exports = createTransformer({
	presets: babelPresets,
	plugins: babelPlugins
});

@javierguzman
Copy link

I have my babel.config.js inside a config folder which is not grabbed by Jest. Is there anything I could do to tell Jest the path? Thank you in advance and regards.

@SimenB
Copy link
Member

SimenB commented Oct 29, 2019

Check the docs? The example there is rootMode rather than configFile, but still: https://jestjs.io/docs/en/configuration#transform-object-string-pathtotransformer-pathtotransformer-object

@javierguzman
Copy link

Thanks @SimenB !! It worked :) That was a long shot, I would not had figured it out just by reading the docs.

@cnt000
Copy link

cnt000 commented Feb 5, 2020

Hi all, I fix like this after Simen tip:

jest.config.js

module.exports = {
  transform: {
    '\\.js$': ['babel-jest', { configFile: './Configuration/babel.config.js' }]
  },
  roots: ['../']
};

babel.config.js

presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current'
        }
      }
    ]
  ]

An example maybe can help someone in the future.

@gicontz
Copy link

gicontz commented Jun 17, 2020

Hi all, I fix like this after Simen tip:

jest.config.js

module.exports = {
  transform: {
    '\\.js$': ['babel-jest', { configFile: './Configuration/babel.config.js' }]
  },
  roots: ['../']
};

babel.config.js

presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current'
        }
      }
    ]
  ]

An example maybe can help someone in the future.

This works like a charm <3 <3 thanksssss!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests