Code Monkey home page Code Monkey logo

gjstest's Introduction

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.

Example

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_);
}
registerTestSuite(UserInfoTest);

addTest(UserInfoTest, function formatsUSPhoneNumber() {
  // Expect a call to the database function with the argument 0xdeadbeef. When
  // the call is received, return the supplied string.
  expectCall(this.getInfoFromDb_)(0xdeadbeef)
    .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() {
  expectCall(this.getInfoFromDb_)(0xdeadbeef)
    .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
user_info_test.js:32
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.

gjstest's People

Contributors

egoog avatar elau8465 avatar eschoeffler-google avatar geoffhowie avatar inazz avatar jacobsa avatar mchesneyatwork avatar mzhaom avatar okdave avatar pguillory avatar robertbastian avatar shicks avatar stephenfarrar avatar tjgq avatar trevorade avatar ulan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gjstest's Issues

setTimeout

Is there a way to implement setTimeout? I am using the Q library for using promisses and the library needs the setTimeout function.

Build with bazel

The targets.mk seems to be generated from BUILD files. Now that bazel is open source, can you guys just open source the bazel BUILD files too? Thanks!

Whats wrong here?

That is the output from gjstest. It is a simple array concatenation like arr = [1,2,3].concat[4,5,6] given to a callback with apply(foo, arr) and tested with expectCall(...)(1, 2, 3, 4, 5, 6).

events.js:17: Call matches no expectation.
(No arguments.)

Tried expectation at events.test.js:77, but number of arguments didn't match:
Arg 0: 1
Arg 1: 2
Arg 2: 3
Arg 3: 4
Arg 4: 5
Arg 5: 6

Unsatisfied expectation at events.test.js:77:
Arg 0: 1
Arg 1: 2
Arg 2: 3
Arg 3: 4
Arg 4: 5
Arg 5: 6

Expected 1 call; called 0 times.

Windows support?

Does it work on Windows? Installation guide mentions only *nix and OS X...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.