Back to blog home page

Functional Testing with Mocha & Chai

Posted by on Aug 26, 2015

Full Stack JavaScript Code

In full stack JavaScript development involving JavaScript on either or both the client-side and the server-side, we would like to test function input-output behavior. In its simplest form, function input-output behavior specifies the output of the function in terms of its input.

For example, for a function adding two numbers:

Its input-output behavior specifies abstractly that,

Functions can executed either locally, or involve calls to remote services. A local execution of a function is the classical in-program execution that does not involve any server-side calls.

Such functions can be JavaScript functions, Node.js functions, and can be embedded in frameworks such as AngularJS.

Automated Testing

Testing abstract assertions such as the one above is not practical for most cases. Instead, automated testing attempts to automatically run test scripts that check correctness of important cases. If employed judiciously, automated testing can achieve several goals:

  • It documents the tests used in manual testing while coding
  • Regression testing
  • Continuous integration based on facts not intuition

Automated testing requires a test runner, a program that understands test scripts written in the language of the test runner. A test script includes several function invocations, and some assertions on the input-output behavior of the functions, and automated checking of assertions expressed in an assertion language.

Mocha and Chai

Here at Back&, We have found Mocha as a test runner, and Chai as an assertion language a powerful combination for full stack automated testing. In contrast to other test runners, such as Jasmine, Mocha is not tied to any particular assertion language. Chai itself comes with several flavours of BDD and TDD assertions. We found Chai’s BDD expect style to be very convenient for both local function execution, and for execution involving calls to remote services.

So how does a Mocha test script look?

Mocha Test

A mocha test is built ‘describe’ statements that can be nested. Each describe statement is a test scenario. To actually perform a test, use an ‘it’ statement:

The Chai assertion using ‘expect’ is used to make assertions on the input-output behavior of the function.

The ‘done’ callback is used to transfer to the next test.

Using Mocha with Chai in Backand

We have used Mocha with Chai in Backand, a Backend as a Service (BaaS) provider, that automatically creates a relational database together with a REST API from a JSON schema describing the database in familiar JavaScript terms. It was inspired by the Waterline ORM.

For example, this schema JSON,

creates a database with two relations,

using these ‘create table’ statements in MySQL:

Our Testing Needs

We had two major functions, ‘validate’ that tests whether a JSON is a valid relational schema, and ‘transform’ that transform one schema to another. Backand uses ‘transform’, when Backand users modify their database structure by providing a new JSON schema. The function create a sequence of MySQL statements to modify the underlying MySQL database.

While coding, we have accumulated a collection of test cases, and we found Mocha with Chair useful for documenting them. This was done immediately during coding. Having completed the first phase of coding, we expanded the use of automated testing to regression testing. We continuously expand the expressive power of the schema, for example into one-to-many relationships between relations in the database. Automated testing enabled convenient regression testing.

Writing Test

To install Mocha and Chai, we used ‘package.json’:

installing them with:

In our test file, ‘unit_functional_test.js’, we include Chai, and then our code:

Now we write a test to check that our validator validates as valid a valid schema.

We run the test with:

and get this output:

screen shot mocha

Another test to validate that default values of fields are correctly validated is:

Which in this case should detect the the ‘name’ column in the table ‘user’ has a non valid default value.

To test that the SQL statements to transform one database schema to another are correct, we use this test:

Testing Remote Service Calls

In full stack development, almost any module, be it on the client side or the server side, involves remote service calls. Such calls, while being asynchronous, are actually function invocations that yield an output for input. Mocha and Chai are just as convenient for testing such calls.

In Backand, we fetch the details of the MySQL database when you supply your username, password, and app name. This is described in the following test, where we use the ‘request’ package to make HTTP calls.

First, we require our module making remote service calls:

Then we make a call to get the authentication token for an authorised user and app. Once we get the token, we call the getConnectionInfo service. This test is intended to test the input-output behaviour of the getConnectionInfo service. So the assertions are on the output of that service.

We increase the standard Mocha timeout of 2000ms to 4000ms because a call to the server may take time.

Get a free hosted AngularJS backend with features such as user management, social signin, payment integration, security and more – GET STARTED NOW.