unit testing with mocha
DESCRIPTION
Helps to get started with testing in JavaScript using mocha.TRANSCRIPT
TESTING WITH MOCHA
@revathskumar
ABOUTRubyist / JavaScripter
Team Member / / Works at Blog at Twitter/Github - Google+:
Yeoman @keralarb @keralajs@whatznear
blog.revathskumar.com@revathskumar
+RevathSKumar
AGENDAMocha FeaturesBDD/TDD syntaxChai assert libraryTesting asynchronous codeTesting in Node.js and BrowserGrunt and gulp tasks
NEVER TRUST YOUR CODE
TESTING FRAMEWORKSMochaJasmineQUnit
MOCHA
MOCHA: FEATURESRuns on Node.js/BrowserSupports BDD/TDDChoose any assertion libraryChoose any Mocking libraryAsync and promise supportHighlights slow testsFile watcher supportOptionally run tests that match a regexp
MOCHA: TDDsuite('Calculator', function () {
suite('Add', function(){
test("using both positive numbers", function(){ // test assertion });
test("using both negative numbers", function(){ // test assertion }); });
});
MOCHA: SETUP & TEARDOWNsuite('Calculator', function () {
setup(function(){ // runs before test });
//test("add using both positive numbers", function(){ // test assertion //});
teardown(function(){ // runs after tests });
});
MOCHA: BDDdescribe('Calculator', function () {
describe('add', function () {
it('using both positive numbers',function(){ // test assertion });
it('using both negative numbers',function(){ // test assertion }); });
});
MOCHA: BEFORE & AFTERdescribe('Calculator', function () {
before(function(){
})
after(function(){
})
});
beforeEachafterEach
CHAI.JSchai.assertchai.expectchai.should
CHAI: ASSERT
chai.assert(expression, [message])chai.assert(actual, expected, [message])
ok()notOk()fail()isTrue()isFalse()isNUll()
CHAI: EXPECT
chai.expect(expected).to.not.equal(actual);chai.expect(expected).to.be.a('string');
okinclude()truefalseempty
CHAI: SHOULDvalue.should.be.an('object');value.should.equal(10);
value.should.be.a('string')
object.should.have.property('key')
NODE.JSnpm install -g mocha
npm install -g chai
NODE-TEST// calculator.jsCalculator = { add: function(a, b) { return a + b; }}
module.exports = Calculator
// test.jsvar Calc = require("./calculator");var assert = require("chai").assert
suite("add", function(){ test("two positive", function(){ assert(Calc.add(1,3), 4); });
test("positive and negaive", function(){ assert(Calc.add(-5,4), 1); })});
EXECUTE IN CLImocha --ui=tdd ./test-tdd.jsmocha --ui=tdd --watch ./test-tdd.jsmocha --ui=tdd --grep="positive" ./test-tdd.js
BROWSERbower install mocha
bower install chai
BROWSER-TESTCalc = { add: function(a, b){ return a + b; }}
mocha.setup('tdd');var assert = chai.assert;
suite("add", function(){ test("two positive", function(){ assert(Calc.add(1,3), 4); });
test("positive and negaive", function(){ assert(Calc.add(-5,4), 1); })});
mocha.run();
ASYNCHRONOUSCalc = { add: function(a, b, fn){ fn(a + b); }}
suite("add", function(){ test("two positive", function(done){ Calc.add(1,3, function(result){ assert(result, 4); done(); })
});});
PENDING TESTSMark as pendingtest without callback
suite("add", function(){ test("two positive");});
EXCLUSIVE TESTSRun only specific test/suitesimilar to grep
suite("add", function(){ test.only("two positive", function(){ assert(Calc.add(1,3), 4); });
test("positive and negaive", function(done){ assert(Calc.add(-5, 4), -1); })});
SKIP TESTSsuite("add", function(){ test.skip("two positive", function(){ assert(Calc.add(1,3), 4); });
test("positive and negaive", function(done){ assert(Calc.add(-5, 4), -1); })});
USING GRUNTnpm install -g grunt-cli
npm install grunt --save-dev
npm install grunt-mocha --save-dev
GRUNT CONFIGmodule.exports = function(grunt) { grunt.loadNpmTasks('grunt-mocha');
grunt.initConfig({ mocha: { test: { src: ['tests/**/*.html'], }, } });}
USING GULPnpm install -g gulp
npm install --save-dev gulp-mocha
GULP CONFIGvar gulp = require('gulp');var mocha = require('gulp-mocha');
gulp.task('default', function () { return gulp.src('test.js', {ui: tdd}) .pipe(mocha());});
REFERENCESMocha : Chai.js :
Grunt :
grunt-mocha :
Gulp :
gulp-mocha :
Testing AJAX :
code :
slides :
https://visionmedia.github.io/mocha/http://chaijs.com/
http://gruntjs.com/
https://github.com/kmiyashiro/grunt-mocha
http://gulpjs.com/
https://github.com/sindresorhus/gulp-mocha
http://blog.revathskumar.com/2013/03/testing-jquery-ajax-with-mocha-and-sinon.html
http://jsbin.com/veqop/6/edit
https://speakerdeck.com/revathskumar/testing-with-mocha