User:Joe0509/sandbox

Mocha is a JavaScript test framework running on node.js, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library.

Installation
Mocha is a powerful testing framework for Node.js so it can be easily installed mocha by npm command. Npm, the node package manager, is the default package manager for the JavaScript runtime environment Node.js. There are 3 steps to install Mocha:
 * 1) Setup file package.json with npm init
 * 2) Install: $ npm install -g mocha
 * 3) Check the local install version to make sure install successfully: $ mocha --version

Getting Start
Mocha is easy to get start and using to test. Just use “mocha” command to test your Javascript code. For example, First, create a file named indexSpec.js: And then just run mocha indexSpec.js in the command line:

Assertion Libraries
Assert module provides some assertion test that can be used to test invariants. There are a lot of assertion libraries for a node. You can use any assertion library in Mocha if you want, it will work when it throws an error. Mocha provides its users for five assertion libraries:


 * should.js
 * express.js
 * chai
 * better-assert
 * unexpected

should.js
Should.js is BDD style assertions for node.js -- test framework. As for this assertion library, should is a readable framework-agnostic assertion library. It is used to keep your test code clean and make sure your error message can help your coding.

express.js
Expect.js is used to minimalistic BDD-style assertions for Node.js and the browser. This library can be used in lots of the common internet browser, such as IE, Firefox, Safari, Chrome, Opera. expect.js also compatible with all test frameworks. In addition, this is also standalone. Single global with no prototype extensions or shims. Compared with should.js, expect.js API changes related to browser compatibility.

chai
Chai is a BDD/TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. There are several interface provided by Chai in order to allow developer use the most comfortable one. BDD styles provide an expressive language and readable style. As for TDD assert style, it provides more classical. And there are many plugins extend Chai assertions for developers to use.

better-assert
Better-assert is a c-style assert for node.js, and it will report the expression string as the error message. It use callite for self document failure messages.

unexpected
Unexpected assertion is an extensible BDD assertion toolkit. It can be extensible and run fast. This assertion library provides users helpful error message and also helps them correct when they misspells assertions. The same as the other assertions, this one also compatible with all test frameworks. Furthermore, unexpected assertion supports asynchronous assertions using promises, single global with no prototype extensions or shims and cross-browser: works on Chrome, Firefox, Safari, Opera, IE6+, (IE6-IE8 with es5-shim).

Synchronous Code
Synchronous means it waits for each operation to complete after that only it executes the next operation. Test synchronous code is much easier than test asynchronous code. When testing synchronous code, just omit the callback and then Mocha will automatically continue with the next step of the test.

Asynchronous Code
Asynchronous will never wait for each operation to complete, rather it executes all operations in the first GO only. The result of each operation will be handled once the result is available. So, test synchronous code is difficult because users always want to test things after they complete. But to know when it completed is not directly possible in Javascript, if want to test asynchronous, first attempts are led it to rely heavily on timeouts. When testing it with Mocha, simply invoke the callback when your test is complete. So, users need to add a callback to it, in order to let Mocha know that it should wait for the completion. There exist a function called done can helps. Besides use function done to a callback, developers can also return a Promise. This is useful if the code you are testing return promises instead of taking callbacks.

Arrow Functions
Compared to function expression, arrow function has a shorter syntax and lexically binds the value. And arrow function are always anonymous. When testing arrow functions in Mocha, it’s discouraged when passing to Mocha. Their lexical binding of the this value makes them unable to access the Mocha context, and statements like this.timeout(1000) will not work inside an arrow function.

Hooks
Hook is some logic, typically a function or a few statements, which is executed when the associated event happens. Hook is a RESTful and extendable Backend as a Service that provides an instant backend to develop sites and apps faster, with dead-simple integration for javascript, iOS, Android. It has lots of features: Mocha also has hooks that are executed in different parts of suites, before the whole suite and before each test. Mocha has hooks like before, after, befroeEach, afterEach to set up preconditions and clean up your tests. All hooks can be invoked with an optional description, making it easier to pinpoint errors in your tests. If hooks are given named functions, those names will be used if no description is supplied. All hooks support asynchronous modes just like a regular test-case.
 * Multitenancy (same instance may be used for many apps)
 * User authentication (register, login, reset password)
 * Data persistence through collections
 * Data storage through many providers
 * Real-time communication through WAMP subprotocol (WebSockets).
 * Package management through composer

You may also pick any file and add "root"-level hooks. For example, add beforeEachoutside of all describe blocks. This will cause the callback to beforeEach to run before any test case, regardless of the file it lives in (this is because Mocha has a hidden describe block, called the "root suite"). Furthermore, If you need to perform asynchronous operations before any of your suites are run, you may delay the root suite. Simply run Mocha with the --delay flag. This will provide a special function, run, in the global context.

Pending Test
Pending test-cases are simply those without callback. And pending tests will be reported as such:

Exclusive Tests
Exclusive allow users to run the specified suite or test case by appending. only is the function for this feature.

Inclusive Tests
This is the opposite of the exclusive test. By appending, skip is the function. You can make Mocha ignore these suites or test case.

Dynamically Generating Tests
Mocha provide dynamically generate test function: function.prototype.call and function expressions to define suites and test cases. Users don’t need to use special syntax.

Usage and examples
$ mocha . 1 test complete (1ms) For asynchronous testing, invoke the callback, and Mocha will wait for completion.