ficapy
4/18/2014 - 3:19 PM

The 5 best front-end developer tools blogpost: testing with Mocha and Jasmine

The 5 best front-end developer tools blogpost: testing with Mocha and Jasmine

// Function under test
function once(fn) {
    var returnValue, called = false;
    return function () {
        if (!called) {
            called = true;
            returnValue = fn.apply(this, arguments);
        }
        return returnValue;
    };
}

it("calls the original function", function () {
    var spy = sinon.spy();
    var proxy = once(spy);

    proxy();

    assert(spy.called);
});
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.length(3);
tea.should.have.property('flavors').with.length(3);
Q = require('q');

var AsyncProcess = (function() {
    function AsyncProcess() {}

    AsyncProcess.prototype.process = function() {
        var deferred;
        deferred = Q.defer();
        setTimeout((function() {
            deferred.resolve(42);
        }), 100);
        return deferred.promise;
    };

    return AsyncProcess;

})();

exports.AsyncProcess = AsyncProcess;
AsyncProcess = require('./async-process').AsyncProcess;
Chai = require('chai');

Chai.should();

describe('AsyncProcess', function() {
    var asyncProcess;

    beforeEach(function() {
        asyncProcess = new AsyncProcess();
    });

    it('should process 42', function(done) {
        deferred = asyncProcess.process();

        deferred.then(function(processed) {
            processed.should.be.equal(42);
            done();
        });
    });
});
AsyncProcess = require('./async-process').AsyncProcess

describe('AsyncProcess', function() {
    var asyncProcess;

    beforeEach(function() {
        asyncProcess = new AsyncProcess();
    });

    it('should process 42', function() {
        var done = false;
        var processed = null;

        deferred = asyncProcess.process();
        deferred.then(function(result) {
            done = true;
            processed = result;
        });

        waitsFor(function() {
            return done;
        }, "the async process to complete", 10000);

        runs(function() {
            expect(processed).toEqual(42);
        });
    });
});