Fork me on GitHub

Unit Tests.

Write browser-based JavaScript tests to easily ensure code functionality through unit testing.


1. Copy and paste

<script src="https://cdn.alloyui.com/3.0.1/aui/aui-min.js"></script>

This ultra-small bit of JavaScript gives us access to all AlloyUI modules. You are going to need this.


2. Getting started!

Let's create a YUI instance and load the test module to begin.

YUI.use(
    'test',
    function(Y) {
        // Test cases are written here
    }
);

3. Writing Test Cases

Create a TestCase object with Y.Test.Case constructor and pass it an object with individual test cases. Keep in mind that each test should test a specific piece of functional code.

Test methods can be named using the traditional manner of prepending the word test to the method name, or using a "friendly name," which is a sentence containing at least one space that describes the test's purpose.

var yourTestCase = new Y.Test.Case(
    {
        name: "TestCase Name",

        // ------ Tests ------
        testSomething: function() {
            // code goes here
        },

        "something should is going to be tested here": function() {
            // code goes here
        }
    }
);

setUp() and tearDown()

Sometimes it is necessary to setup information before a test is run and clean up the information after the test is run. The setUp() method is run before each and every test in the test case and likewise the tearDown() method is run after each test is run.

var yourTestCase = new Y.Test.Case(
    {
        name: "TestCase Name",

        // ------ Setup and Tear Down ------
        setUp: function() {
            // initialize data to be used in each test
        },

        tearDown: function() {
            // remove data after each test
        },

        // ------ Tests ------
        testSomething: function() {
            // code goes here
        },

        "something should is going to be tested here": function() {
            // code goes here
        }
    }
);

Assertions

We now have the basic structure of a test, but have not actually tested anything. Assertions allow us to check the validity of a particular action or function. In general, they accept a test condition and an optional message to display if the test fails.

There are many types of assertions available, all of which can be found at http://yuilibrary.com/yui/docs/test/#assertions. But for now, we will just look at some of the basics.

var carsTestCase = new Y.Test.Case(
    {
        name: "Cars Test",

        setUp: function() {
            this.car = {
                color: ['red', 'black', 'silver', 'white'],
                make: "Toyota",
                model: 'Camry',
                year: 2013
            };
        },

        tearDown: function() {
            delete this.car;
        },

        'car colors should be listed in an array' function() {
            Y.Assert.isArray(this.car.color);
        },

        testCarMake: function() {
            Y.Assert.isEqual("Toyota", this.car.make, "The car make should be 'Toyota'");
        },

        testCarModel: function () {
            Y.assert("Camry" == this.car.model, "Model should be 'Camry'");
        },

        'year should be a number': function() {
            Y.Assert.isNumber(this.car.year, "Year should be a number");
        }
    }
);

Great! Now that we have a TestCase set up, we can add it to a TestSuite.


4. Test Suites

If we are testing a large application that requires many test cases, a TestSuite allows us to organize a collection of TestCase together. Initializing TestSuite and adding TestCase to them is easy.

var suite = new Y.Test.Suite("Name of Your TestSuite");

suite.add(
    new Y.TestCase(
        {
            // code goes here
        }
    )
);

// Adding carsTestCase to the testSuite:
suite.add(carsTestCase);

Multiple TestSuite can also be organized together into a parent TestSuite in the same way.


5. Running Tests

Congratulations! You now have all the tools to write a unit test. All we have to do is run it!

Y.Test.Runner.add(suite);

Y.Test.Runner.run();

For more information about writing unit tests, checkout out YUI's test documentation.