-
-
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
What is the official way to run jest programmatically? #5048
Comments
I guess my main question is: is the For example this commit 200b032 changed the |
I think there's no official way, that's why it's not referenced in the docs and changelog. @cpojer what do you think about an "officially supported" way of invoking Jest as a node process? |
To add a bit of context - I'm adding jest into our grunt-based build process. I basically have these options:
If there is currently not an official way to run jest from node, then adding it would involve more than just a documentation change (probably some more thinking about the public API, adding tests and documentation, etc.). While I personally think it might be useful to add an official way to run jest programmatically, it's up to the jest maintainers to decide whether to add support for this or not. @thymikee I'm not entirely sure what you're asking (writing about what?). But yes, I'm happy to help. |
@thymikee yeah, I'm not opposed, Jest already has an API that can be used but it requires people to read the source to understand how it works, so it's not optimal. The other thing is that we may choose to break the API from major to major. |
FWIW, I'd love this - I think exposing a consistent JS API to run tests in jest-editor-support would mean that |
I'm gonna close this because the issue is a question. If you'd like to use Jest's programmatic API, check out the main module of jest-cli. If you'd like it to do things that it doesn't currently do, please send as a well-tested PR that adds or changes behavior. |
const jest = require('jest'); jest.run(argv); |
I'm also interested in running jest programmatically from node.js, sadly it's not documented anywhere and the API doesn't look very thoughtful for this use case. |
It's not. That's something we want to improve though (although a bit down the priority list at the moment) |
@SimenB I've just found out that Jest is working pretty fine in this scenario actually, the only problem is the documentation and typings support. I've posted a small example on how to use Jest in this scenario: @cpojer Is there an issue where the progress on this feature could be tracked? |
No issue currently. We'll try to split up |
@SimenB that sounds great. Thank you for your hard work! We are calling Jest as part of our build framework inside of the existing node.js process. The current implementation works great for us, we get the console output as well as results object with more information regarding tests outcome. My only suggestion would be to have a better naming for the API and a typings support, so the module could be used in the TypeScript environment directly. |
We're going to be migrating to TS (#7554), so typings will definitely be there 🙂 |
Just in case anybody is wondering, I was able to successfully do this:
EDIT: Fixed typo. |
Teeny, tiny start here: #7696. Not ready for consumption (don't rely on us not breaking its API without bumping major), but it will happen at some point 🙂 |
Small typo Jest should be jest |
Please don't reach into the We've created a |
@SimenB hey that sounds really good. Along with the new code a tutorial would be very nice. Thank you. |
Whenever we've landed on an API we're happy with, we'll document it properly as well 🙂 |
@SimenB also I'd be happy to be your use case tester as I'm sure others here would be. 💯 |
@SimenB I would also love some documentation on this area. Looking forward to it! 👍 |
The core team will be meeting up in London later this month, and I've written this issue down as one of the things I'd like to discuss at that time. So hopefully we'll have a plan for programmatic API after that 🙂 |
@SimenB ❤️ it's very nice to hear. Is there any way to affect the priority on this issue? |
Until we gave a plan (which we'll hopefully have in 3 weeks or so), no. In the meantime, maybe you can provide som API suggestions? What do you want from a programmatic API? The first thing that comes to mind as difficult (the way things currently are) is to trigger events in watch mode - the only interface is internal FS watchers and watch plugins, nothing that can be interacted with from the outside. But is that something you need, or is just spinning up Jest and So if we can focus on "run jest" and "run tests multiple times (and interact with a running version of Jest)" as separate things, that'd probably be good. For one-time runs, do you need an argv parser? Should you be able to provide partial config, or mix in defaults yourself? |
For comparison, see Jasmine import * as Jasmine from 'jasmine';
const jasmine = new Jasmine();
jasmine.loadConfigFile('spec/support/jasmine.json'); // or jasmine.loadConfig
jasmine.configureDefaultReporter(...); // by default, console
jasmine.execute(); |
Whenever this gets implemented it would be awesome if we could leverage |
I'm not sure if this is needed to get what I want, but I landed here searching for it. I love jest, and I want to write a web server which will run jest tests against user supplied input, instead of fixtures checked into version control. consider a function everyone implements now the web server will run the I was not able to figure out how to do this with Jest, so I ended up implementing it with vanilla JS functions... and its feeling a bit awkward... Here I apply the approach to demonstrate normative statements of a specification are testable with positive and negative tests... https://w3c.github.io/did-test-suite/#did-parameters If anyone has a better solution to this problem that does not involve running jest programmatically, I would be very grateful... here is the repo with my current solution: https://github.com/w3c/did-test-suite EDIT: I ended up pooling some of the code from the comments here, and building this: https://github.com/transmute-industries/vc.transmute.world/tree/master/packages/jest-test-server |
Any plans to make a programatic api of some sort? |
Came here wanted to test my vscode extension E2E UI with Jest, but finding the |
Any progress here? I want to run njsTrace. However, it requires Jest to be run similar to Jasmine
|
See #9233 for a request for custom instrumentations. |
I'm not sure that's 100% related. So when using a test suite, like jest, you will have to run your own "test runner", say create a "test-runner.js" file, which will include njsTrace and call inject, then you will have to somehow "require" jest and run your tests. |
@socketopp not really, you can use the RunCLI method to invoke tests, but it's not supported. Why on earth they haven't invested in a simple programmatic interface is beyond me. |
Hi people! I have done some tests and it seems to work without problems. I can build the configuration and pass it to the runCli function, useful for us. We want to know if we are making a bad approach. Thanks! |
@manuman94 it's not supported and could break at any time, that said it's the "best" approach if you need to run tests using jest. |
@JustinGrote oh! What a pity, it would be awesome to count on a reliable API to do this. Thanks for your response. We will be using this for the moment, but we are waiting for the final solution as many of you. Greetings |
@socketopp for your particular use case of using njsTrace, you can plug into jest's instrumentation framework. jest will have It is a pity that Jest didn't take the same approach as React, but is instead is a tangled framework that does everything from compilation to module resolution to test helpers. |
So apparently it is still a no-no, am I wrong? |
Just to support this feature 👍 |
FWIW Create React App is depending on this implicit API: https://github.com/facebook/create-react-app/blob/3880ba6cfd98d9f2843217fd9061e385274b452f/packages/react-scripts/scripts/test.js#L129 |
Is jest team(?) still working on this? my use case: I am just calling the jest runCLI function to do that for now. Thank you |
@tachyon83 PR #14062 for this was opened in April 2023 and conversation slowed until halting in late June. It's worth following that PR as well for updates, but I'm sure this issue will be updated as well. Otherwise I'm not optimistic for any fix within 6 months--someone would have to take initiative and that's only happened a couple times in the 5+ years this issue has been open. |
I'm particularly interested in an interface that would let met configure a jest instance and run tests multiple times, and cancel on an abort signal. Something like const jest = new Jest(options);
const signal = AbortSignal.timeout(60_000);
for (await (const _ of setInterval(1_000, { signal })) {
jest.run({ signal })
} |
My question is how to run jest programmatically. There are some examples on the internet:
https://stackoverflow.com/a/30562836/1652273
https://stackoverflow.com/a/33669530/1652273
#3848
All of them reference requiring
jest
orjest-cli
and callingrunCLI()
, but it does not seem to be documented anywhere (I haven't found anything in the jest docs nor is there any documentation for the jest-cli package).Is this the official supported way? Thanks.
The text was updated successfully, but these errors were encountered: