-
-
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
Allow config.transform[regex] to be a function for jest.runCLI(config) #7398
Conversation
Codecov Report
@@ Coverage Diff @@
## master #7398 +/- ##
==========================================
+ Coverage 66.79% 67.66% +0.87%
==========================================
Files 242 242
Lines 9371 9374 +3
Branches 5 5
==========================================
+ Hits 6259 6343 +84
+ Misses 3110 3029 -81
Partials 2 2
Continue to review full report at Codecov.
|
This makes it easier to test the function
@rickhanlonii regarding your comment #7288 (comment)
I agree that supporting JSON/String only configurations for use by the cli is a very important use case. That's why this PR was implemented as a non breaking change with full backwards compatibility for that use case. In fact, prior to this PR passing an object instead of String to There were also no tests of the If you're interested those new tests can be found here: |
@@ -53,7 +57,8 @@ export async function run(maybeArgv?: Argv, project?: Path) { | |||
exit(1); | |||
throw error; | |||
} | |||
} | |||
return Promise.resolve(results); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Promise.resolve(results); | |
return results; |
I don't think we can do this - the config must be json serializable since we pass it to workers |
Hi Simeon thanks for the review! It looks like Rick Hanlon has moved on to other projects and I wonder if anyone at Facebook will keep maintaining Jest?? Can you please elaborate on such a sweeping statement about workers? Ex: How are workers used by jest and why does this require JSON? And even if that holds true, is it worth restricting how Jest can be used? Not having jest be runnable except via CLI actually makes it unusable for my use cases. We shouldn’t limit how it can be used with out good reason. It’s written in JavaScript so it’s be nice if it could be executed using JavaScript. JSON is very limiting! 😉 |
Co-Authored-By: jharris4 <[email protected]>
It is open source ❤️ we can keep maintain the project without relying on Rick Hanlon 👏
@jharris4 this is where I stopped 😄 It does require JSON because it can't serialize JavaScript prototypes. Read #7279 (comment) Now that I understand, it is a limitation.
Trade off, fix NodeJS itself, that will be amazing 👏 I believe that #7288 is a better way to go. Yes, JSON is limited, but no, you can do many things by passing data around, rarely you will need an actual JavaScript object to be passed around for these cases, I am curious to know your use cases so we can figure out how to properly implement the feature that will fit your requirements. |
Workers are how jest parallelizes execution across cpu cores. And in order to talk between different processes, you need to serialize the data. This is how So while we're not technically bound to the config being JSON-serializable, we're bound to it being serializable to a string, which functions are not. EDIT: You can do
We definitely want to improve the programmatic usage, but passing in functions in configuration is not something we can do due to the technical limitations mentioned above. One thing I've had on my todo block (pretty far down) for a long time is rip |
@yordis thanks for your input. My use case is that we've put together a build tool that has the config for babel/rollup/webpack/jest all in one file (I know sounds crazy but it's working nicely!). So it was bothering me that Jest essentially required a special separate file that then needs to separately parse the master config just to setup babel. But I see now that it is indeed a limitation of the @SimenB thanks for the explanation about the workers. I dug into the code a bit this morning, and you are indeed correct that config transform as functions do get swallowed (Silently!) by the send call to the child process forked by the jest-worker package. I may play around a little bit more to see if I can get past this limitation somehow, but otherwise I guess this PR should be ignored (closed?) since it won't work with parallel test runs. As for your |
Sounds like my CLI, we are doing the same with the same use cases pretty much. Today you could serialize the values into an environment variable and deserialize later on your Jest config. Here is an example. There is no workaround for dealing with this serialization problem. At the end of the day, you could put some factory function on your Jest transformer that takes the configuration and recreate the Babel config. const { createTransformer } = require('babel-jest');
const { createConfig } = require('my-package/babelConfigFactory');
const options = JSON.parse(process.env.JEST_BABEL_OPTIONS);
const babelOptions = createConfig(options);
module.exports = createTransformer(babelOptions); So would be rare that you have to pass a full JavaScript object between workers. In the future, you will remove the hacky serialization using environment variables but will need to keep the factory function. Just keep your configuration as simple as possible so the factory function can't depend on JavaScript unserializable objects. Let me know if you need any help, if your tool is open source I can give you a hand or direction. |
Wow, neutrino looks super interesting! It indeed has many similar ideas to what we've been working on. We've been building our libraries and web apps using an internal tool that we named The idea is that any project/package using porter has one central
Each of the functions: These configs can then be executed by the respective tool using cli commands like: The idea is that the
And the
Basically I created this PR so that the |
If you make it work, that would be awesome, but I think it's such a fundamental thing that's it's impossible to work around. Happy to be proven wrong, though! Happy to reopen (and please continue the discussion! 🙂) |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Currently with
jest.runCLI
you can only pass JSON parseable strings like this:If we want to create a utility function to run jest with a custom configuration like:
We run into the problem that
createConfigFromOptions
can only configure the preprocessor transforms by specifying path to files, leading to the need to split the configuration across multiple source files.With this PR that workflow is much nicer in that you can just do:
No extra source files required for the transform to work! :-)
Several tests were added to show that it works properly, including a new test that actually calls
jest.runCLI
from within a test!