-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Babel config without .babelrc #1468
Comments
Would it be possible to get a config here to set these babel options? What do you think would be the best practice for this? |
Would love to know the answer on this here as well |
You can wrap babel-jest and create your own preprocessor by just passing in the options you want. That should be sufficient. On Fri, Aug 19, 2016 at 9:04 PM +0200, "Josh Story" <[email protected]mailto:[email protected]> wrote: Would love to know the answer on this here as well You are receiving this because you are subscribed to this thread. |
and how do I get that into jest? |
@ccorcos you can specify your custom preprocessor in the config |
@DmitriiAbramov I'm in a similar situation. Working on a modular build system and need to be able to pass in some runtime configuration to the scriptPreprocessor. But since If, instead, it were to accept a Thanks! |
You can make your own preprocessor that simply does this:
and then point scriptPreprocessor to that file. See https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js#L53 This makes the simple case simple and the hard case possible. I don't think we'll make scriptPreprocessor a function and the current solution should work well enough. |
@cpojer thanks for taking the time to reply! Sorry but maybe I'm not doing a good job of explaining what's happening. So, what you have suggested is what I am doing. However, I don't see a way of passing in (at run-time) parameters that would affect /* jestConfig.js */
module.exports = {
...
scriptPreprocessor: path.resolve('./myCustomPreprocessor.js') // no way to pass in runtime options!
...
}; /* myCustomPreprocessor.js */
// pseudo-code, this doesn't actually work obviously
module.exports = (options /* <-- how to specify?? */) => {
const babelOptions = options; // assuming some merging, conditional logic, etc.
return babelJest.createTransformer(babelOptions);
}
Assuming |
@ccorcos right, but notice that CRA is not passing in any runtime options. It's all just loading static files. |
I don't understand what you are saying with "runtime". The preprocessor is required by Jest and is used to transform files. When it is being required you can do whatever you want to create your babel options object that you pass on to |
@tizmagik as i understand you're trying to pass a parameter from your build process (e.g. spawnProcess('jest', {env: {ADD_CUSTOM_TRANSFORMATION: 1}}); then in your customPreprocessor: if (process.env.ADD_CUSTOM_TRANSFORMATION === 1) {
plugins.push(require('my-custom-transformation-plugin');
} |
Ah yes, I see what you mean @tizmagik. You want to be able to something like this: scriptPreprocessor: require('config/jest/transform.js')({development: true, some_feature: false}), but all we have is this: scriptPreprocessor: path.resolve('config/jest/transform.js'), |
@ccorcos yes, exactly! What @DmitriiAbramov suggested would work, but I would rather avoid having to simulate/affect some global state. Would you accept a PR for this change? |
I still don't fully follow here. If you pass in the options to the scriptPreprocessor from the outside I see no problem to instead create them from the inside. Just factor out the piece of code that creates your babel config:
we are using this approach all over at FB and it works well for us. |
Yeah but that not a runtime configuration.
|
@tizmagik i think the biggest thing here is being able to pass config to child processes, when we run tests in parallel. see https://github.com/facebook/jest/blob/master/packages/jest-cli/src/TestRunner.js#L255 |
oh interesting. you accept only file path for parallelism... seems like we should be able to solve this issue if the args are serializable. then we have something kind of like how webpack loaders work: {
loader: 'babel',
query: {
presets: ['es2015'],
}
} |
@ccorcos can you show a real example of why my proposal won't work? In practice it worked really well for us. It requires you to only invert a few things and will probably lead to a better separation of concerns. |
Here's a pseudocode example: const buildOptions = parseCliArgs(process.argv)
const babelConfig = generateBabelConfig(buildOptions)
runJest(babelConfig) The |
Why do your build options change so much and why are they generated? I feel like there must be a simpler way with more static build configuration that's stored in a file somewhere. |
because I have a complicated (legacy) build system and have to support several different ways of doing things. |
For those landing on this issue and wondering what ./jest.transform.js// Custom Jest transform implementation that wraps babel-jest and injects our
// babel presets, so we don't have to use .babelrc.
module.exports = require('babel-jest').createTransformer({
presets: ['node7', 'react', 'stage-2'], // or whatever
}); ./jest.config.json (or "jest" entry in
|
Other option could be to keep var webpackConfig = require('../../webpack.config.js')
var babelConfig = webpackConfig.module.rules[1].query
module.exports = require('babel-jest').createTransformer(babelConfig) |
@nfarina thank you so much for your solution above. I struggled for hours this morning trying to get Jest to recognize |
Trying the solution posted by @nfarina, but it appears as if Jest is ignoring my transform. I even tried manually deleting the Jest cache with --clearCache. |
@sparkbuzz not sure if you're still blocked, but here's how I used @nfarina 's idea: jest.config.js module.exports = {
verbose: true,
transform: { '^.+\\.js$': '<rootDir>/jestPreprocess.js' },
}; jestPreprocess.js const babelOptions = { presets: ['env'] };
module.exports = require('babel-jest').createTransformer(babelOptions); package.json
Hope that helps or sparks a solution for you. The issue might be that you're using |
For anyone stuck on this - the approach we took was: // transformer.js
const { util, transform } = require('babel-core');
module.exports = {
process(src, filename, config) {
return util.canCompile(filename) ?
transform(src, Object.assign({}, { filename }, config.globals.BABEL_OPTIONS)) :
src;
}
}; And then pass But beware we also had to stringify the options passed to |
I have a use case to be able to call To make this work I opened the following PR: #7398 |
Sorry for reviving this thread, but I would really like to see an official documented way to provide babel options to jest outside of |
@onlywei you don't have to use You can setup a custom transform in your jest config that can do whatever you want (with the caveat that it has to point to a file or package that does the transform). If you provide a little more information about what you're trying to do I can try and be a little more specific in suggesting a solution... ;-) |
Another possibility (if using Jest 24, currently in beta) is to check if If you really don't want a config file, using |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks. // babel.config.js
module.exports = function (api) {
const isJestEnv = api.env() === 'test'
// This line is required AFTER any calls of api's method.
api.cache.forever()
// Nothing changed? TRY `jest --no-cache` as @nfarina mentioned above.
return {
presets: [
'@vue/app'
],
plugins: (
// Activate the require-context-hook plugin at the only Jest env,
// to avoid `fs.readdirSync is not defined` error
// on the Webpack-generated code.
isJestEnv ? [
'require-context-hook'
] : []
)
}
} |
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. |
I'm building a modular build system that ideally is going to be its own npm package soon enough. However right now, its just a parallel directory:
One of the goals is to separate out all the build dependencies. I want to everything to just work, so I'm not using a
.babelrc
file and instead linking to babel presets inside the webpack config:So I was hoping that somehow I could configure and run Jest on the a targeted directory without specified configurations that doesnt rely on directory structure.
The text was updated successfully, but these errors were encountered: