Frequently asked questions

Aaron Jacobs edited this page Aug 26, 2014 · 1 revision

What are the goals of Google JS Test?

Google JS Test is aimed at developers who want to do fast, expressive unit testing for things that really are units – self-contained chunks of code that don't have external dependencies like the browser, or have only external dependencies that can be mocked. The most important goals are:

  • Extremely fast iteration time. You should be able to write a test or change your code, then immediately see helpful results from Google JS Test without waiting for your tools.

  • Useful, readable output. The test runner should give you only helpful output, and it should be readable by a human at a glance. There should be nothing superfluous and nothing that gets in the way of the real output, like pages and pages of browser runner logging output.

  • Expressive assertions and mocking. There should be a powerful set of built-in ways to express test assertions as well as mock expectations and actions. The extra semantic information these convey should allow for even more useful test output. The set of matchers should be user-extensible.

Google JS Test is really fast—it takes only milliseconds to run tests. It has minimal output with exactly the information you need, and has a great set of matchers for assertions that also work with its built-in support for mocking.

Do I have to learn yet another testing framework API?

Maybe. Google JS Test's API is intentionally identical in spirit and almost identical in syntax to gtest and gmock, Google's C++ testing and mocking frameworks. If you're familiar with them, you should grok Google JS Test without any trouble at all. Even if you're not, the API is minimal and well-documented, and works as common sense would expect in most cases.

Can I use a browser to work on my tests locally?

No problem. Run gjstest with the html_output_file flag and it will write out an appropriate HTML file. For example:

gjstest --js_files=bar.js,foo.js,foo_test.js --html_output_file=foo_test.html

If you open the HTML file in your browser (this is extensively tested only in Chrome), you'll get a nice browser interface to your test. You can refresh it when you change the test, the code being tested, and its dependencies.

Why doesn't Google JS Test present a DOM to the code under test?

Google JS Test is built directly on top of Google's super-fast V8 JavaScript Engine, with no browser involved. In particular, there is no overhead of starting a browser, which saves a lot of time. This is what allows it to be so fast, but is also what prevents it from offering a real browser DOM to its tests. You can work around this and still use Google Js Test for code that manipulates the DOM however; see the question below.

Can I use Google JS Test to test my code that manipulates the DOM?

Yes! Google JS Test offers great built-in mocking facilities, and you can use this to pass in appropriate mock implementations of the DOM to your code under test. See "Is it for me?" for more information about this technique.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.