-
-
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
Running tests in the browser #139
Comments
Specifically I just want the ability to run a single jest test file |
There was some discussion about this on the mailing list. |
is there a way that jest could make use of the jasmine spec runner? |
Anyone has any update on this? Is there any work in progress? |
We migrated our tests to Mocha for this reason, among others. I'd highly recommend doing the same (or to Jasmine). While we still can't run our tests in-browser (they depend on |
We migrated our tests to mocha as well. We use browserify to run them in the browser and jsdom to run them on the command line. |
Thanks a lot for the feedback guys 😄 |
Nothing done here yet, as the auto-mocking requires the node runtime environment. |
Why do not provide a working version without auto-mocking? I remember https://remysharp.com/2014/05/30/commonjs-with-devtools-live-edit so it should not be impossible to do this limited version. I know PR are welcome! |
i would also really appreciate using the specrunner! |
The important issue here is the ability to use the browser to debug the unit test in the browser. It's very useful to debug the code you're testing, rather than a run-and-see-if-it-works setup. Really helps in a TDD environment |
if there is no possibility to test in browsers, how can we debug? |
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed. |
Any news? |
It's a truism: if you're going to run your code in the browser then you should test it in the browser, too. Running your tests under node with jsdom isn't the same thing. It's just going to give you headaches. My preferred approach for testing client-side JS is currently Mocha + mocha-phantomjs for running tests on the command line, but there are many ways to do it. |
+1 |
So is there no simple way to run jest (using jasmine 2) through karma? What is so difficult about it if Jasmine already can? Seems like a MVP requirement for any test runner for unit testing enterprise apps. |
Oh I haven't commented here. The new architecture of jest proposed in #599 will make it easier to build a server environment around Jest. This is unfortunately not a priority for Facebook however, so I'm not going to actively push for this. If anyone wants to work on it, I'd be happy to help. |
thanks Chris. What about running Jest on node webkit and chromimum web driver? |
Seems like that is the same concept? |
Yeah I guess so. I'll see what I can dig up on it. |
I tried just running in the browser with jest like so: https://github.com/QuantumInformation/JestBrowserTest but I get this error in the console: Uncaught (in promise) SyntaxError: Unexpected token H |
Merging this task into #848. |
One very interesting information from this thread is that it seems running unit tests in browsers not to be a priority for Facebook. Which means: Facebook, one of the biggest website in the world does not need to run unit testing in browsers, only in nodejs, with jsdom and auto mocks. Could maybe one Facebook engineer explain how they came with this reasoning. Is facebook using solely jest for unit testing of their components base? As for me I believe most important bugs nowadays will come from your own coding, not because of a bug in a browser. Unit testing in node with mocks should be sufficient to test the flow of your program. Once you are able to mock any browser quirk then you can mock it in nodejs. If you need to test that your login form is working in latest chrome then it's a job for selenium and a whole different subject. |
@vvo Hey! I'm happy to share some thoughts. Five years ago we actually used to have a browser unit test suite (that I used to work on, hah!) but it wasn't good, slow to use and not how people built code at FB. We then built a command line testing utility with a mock-dom and people loved the iteration speed and how well it worked. The browser test suite was killed and we never looked back. To cover browser testing we do a lot of things ranging from web driver tests, manual QA and especially employee dogfooding. These things have in general provided more value than a flaky browser test engine that is hard to scale for thousands of tests. However, I do recognize that people enjoy writing tests using a browser environment. Node's debugging story is getting a lot better and JS frameworks provide the necessary abstractions to take care of this. There is an ongoing conversation about the potential of Jest running in the browser some day: #848. |
Awesome feedback, thanks a lot :) |
I'll just add my two cents here. I've started working on a new project with React. I've decided to write unit tests for my Redux code only. Instead of browser tests I am giving a go to shiny new toy called React Storybook which essentially allows me to create show case (story) of my components and how would they behave/render based on various inputs. What I like most about it is the way how it forces you to create decoupled small components. You have to think about that single component only when designing it. Of course it cannot be compared to webdriver tests as it's not automated in any way. Also it's harder to imagine using it for a huge project like Facebook with hundreds of components. On other hand you have a nice "catalog" of available components and you can immediately see its use cases. It is probably gonna be even bigger beast once https://storybooks.io/ is ready. And who knows, perhaps one it will be automated some way too, eg. comparing changes and validating it hasn't changed from last time. |
I was just bitten by this. I have a test passing in Jest, but the behavior is different in the browser. This is specifically because the https://stackoverflow.com/questions/52141486/testing-url-usage-for-browser-in-node-js |
So, true! So much pain to do something simple like testing ES6 code in a browser environment. FACEBOOK, please just make a Facebook browser. A browser that would run ES6 code instead of the whole transpile BS. It is time for Javascript to mature and have a real solid standard. The browser will be good for your stock price too. Trust me! If you make a Facebook browser, I will go buy your stocks immediately. Yao from Stockenja |
Can't agree more with running tests in a real browser. I use good old |
Noting this as a blocker for testing Ember with Jest |
For the people interested in testing React in the browser:
Those are the best solutions I had experience with when I couldn't use Node.js and therefore Jest. |
For anyone still interested in running Jest in the browser, check out jest-lite which lets you do just that. 🙂 |
Hello, for anyone interested in running his tests with Jest in Browserstack, I released this environment: https://www.npmjs.com/package/jest-environment-browserstack |
Dear Sirs, Is anyone working on that? Are there any estimations when jest will be runnable in-browser? Are there any crucial obstacles to that? Thanks! |
If anyone needs to run tests in a browser, just use Karma! Jest is great if you are testing only Node.js APIs. You can pair Karma with the Take a look at Note that very soon you won't need Tip: use the TLDR: If you need real APIs from real environments, instead of mocks, use Karma. It has a higher learning curve than Jest, but in my opinion it is worth it. |
Nowadays there are better solutions such as jest-puppeteer or jest-playwright |
@FezVrasta jest-puppeteer and jest-playwright are for a different purpose: they are for controlling a browser from outside, using code inside your Jest tests which are running in Node (not in a Browser). Those tools do not run your Jest code inside a browser. Instead they allow you to script a browser from the outside. That serves the use cases of end-to-end or integration testing, and I would totally recommend Jest for that purpose. 👍 What I mentioned above about using Karma is good for unit tests whose code needs to run inside a browser environment (as opposed to Node with fake browser APIs provided by JSDom or Undom). As an example, if you have functions that directly access WebGL APIs, or certain CSSOM APIs, or other browser APIs that otherwise Jest (via JSDom, Undom) does not currently provide or only partially implements, and your unit tests will trigger those functions (which would otherwise crash Jest) then you'll want to run your unit tests inside an actual browser that has those APIs. Some people spent valuable time making fake canvas and webgl mocks. They could have instead simply ran tests in a browser. You may just want to use a real browser environment for your unit tests so that you have higher confidence that your stuff works properly in browsers. The jest-puppeteer and jest-playwright tools won't serve that purpose. Do you see what I mean? I hope I can convince you to change your thumbs down to a thumbs up. :) |
I don't see a reason why jest tests may not run in browser environment, while they do run in jsdom. A lot of node.js modules run in browser quite well. For really node.js-specific stuff, there may be indeed compat issues, but most jest tests should just work. |
For really node.js-specific stuff, Jest is great. It's already running your code in Node!
True, but Jest is a test runner that gives you a CLI, to run tests with, and it runs those tests in parallel using Node.js machinery, not browser machinery. How would you tell Jest CLI to run your tests in a browser? In the meantime, we can do it simply with Karma, with code executed in Electron, Chrome, and other browsers. I definitely would like to see where #848 goes, but that's been open for 4 years, and I don't want to try any of the working hacks there, when Karma just works.
karma.config.js |
Let me clarify this: Jest is not running inside JSDom. Jest is running inside Node.js after the fake globals have already been created by jsdom or undom. This by no means you can simply run Jest inside a browser, as Jest is designed to run inside Node, with fake global variables created before your tests run. |
For example of APIs that are missing in JSDom (and why you'd want to run in an actual browser environment), see these bugs (or missing features):
If you run in a browser, and not in Node.js+JSDom or Node.js+Undom, then you will avoid all the problems of fake API with missing features and therefore causes your code not to work (which defeats the purpose of testing that code). JSDom and Undom are trying to... implement a browser... without implementing a browser. Eventually you hit edge cases that make that whole idea not worth it (in my opinion). If you're okay with all of the above issues, then use Jest. If you're not, I recommend Karma. |
I have published a post that worked for us to take our Jest tests and run them in a browser with minimal effort: https://github.com/tom-sherman/blog/blob/master/posts/02-running-jest-tests-in-a-browser.md#the-good-stuff Hopefully this will be helpful for some! |
I think making existing test suits using, for example, react-testing-library + jest runnable in real web browsers is super useful. @tom-sherman 's above approach or Jest electron runner looks promising for this purpose though. |
@tom-sherman Interesting post. How did tou manage to use the |
@tom-sherman @Shingaz and anyone who may end up here: I got
Note also that jest's
|
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. |
It would be great for a TDD setup to be able to run the tests in the browser and see the component you are trying to test. Is it possible to run jest in the browser?
The text was updated successfully, but these errors were encountered: