A fast javascript unit testing framework that runs on the V8 engine.
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
base Include <cstddef> as required by ptrdiff_t. Jan 22, 2016
file Gave our build setup a makeover. Oct 6, 2011
gjstest Define `this` to be the test suite when using `addTest`. Feb 21, 2019
strings Made ascii_ctype.cc buildable with -std=c++11. May 19, 2015
third_party Imported gmock 1.7.0. Jan 8, 2014
util/gtl Switched fully to C++11 hash map/sets. Jan 8, 2014
webutil/xml Switched fully to C++11 hash map/sets. Jan 8, 2014
.gitignore Updated .gitignore. Apr 28, 2013
COPYING Added a license file. Sep 15, 2011
Makefile Makefile: also find homebrew libraries. Aug 9, 2017
README.markdown Made README.md use the newer addTest method for registering tests. Nov 12, 2014


Google JS Test is a fast javascript unit testing framework that runs on the V8 engine, without needing to launch a full browser. Features include:

  • Extremely fast test startup and execution time, without having to run a browser.
  • Clean, readable output in the case of both passing and failing tests.
  • A browser-based test runner that can simply be refreshed whenever JS is changed.
  • Style and semantics that resemble Google Test for C++.
  • A built-in mocking framework that requires minimal boilerplate code (e.g. no $tearDown or $verifyAll) with style and semantics based on the Google C++ Mocking Framework.

The trade-off is that since tests are run in V8 without a browser, there is no DOM available. You can still use Google JS Test for tests of DOM-manipulating code however; see "Is it for me?" for more details.


Below is an example of a basic test for a class called UserInfo, which accepts a database lookup function in its constructor.

function UserInfoTest() {
  // Each test function gets its own instance of UserInfoTest, so tests can
  // use instance variables to store state that doesn't affect other tests.
  // There's no need to write a tearDown method, unless you modify global
  // state.
  // Create an instance of the class under test here, giving it a mock
  // function that we also keep a reference to below.
  this.getInfoFromDb_ = createMockFunction();
  this.userInfo_ = new UserInfo(this.getInfoFromDb_);

addTest(UserInfoTest, function formatsUSPhoneNumber() {
  // Expect a call to the database function with the argument 0xdeadbeef. When
  // the call is received, return the supplied string.
    .willOnce(returnWith('phone_number: "650 253 0000"'));

  // Make sure that our class returns correctly formatted output.
  expectEq('(650) 253-0000', this.userInfo_.getPhoneForId(0xdeadbeef));

addTest(UserInfoTest, function returnsLastNameFirst() {
    .willOnce(returnWith('given_name: "John" family_name: "Doe"'));

  // Make sure that our class puts the last name first.
  expectEq('Doe, John', this.userInfo_.getNameForId(0xdeadbeef));

The test's output is clean and readable:

[ RUN      ] UserInfoTest.formatsUSPhoneNumber
[       OK ] UserInfoTest.formatsUSPhoneNumber
[ RUN      ] UserInfoTest.returnsLastNameFirst
Expected: 'Doe, John'
Actual:   'John Doe'

[  FAILED  ] UserInfoTest.returnsLastNameFirst
[ RUN      ] UserInfoTest.understandsChineseNames
[       OK ] UserInfoTest.understandsChineseNames

Getting Started

See Installing for information about installing Google JS Test on your system. Once you've done so, Getting started will take you through an end to end example of using Google JS Test. While writing your own tests, you can use the Matchers and Mocking pages for reference.