Skip to content

Instantly share code, notes, and snippets.

@t3chnoboy
Created July 28, 2014 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t3chnoboy/ce9bbea38692694f3bbc to your computer and use it in GitHub Desktop.
Save t3chnoboy/ce9bbea38692694f3bbc to your computer and use it in GitHub Desktop.

#Mocha

Much better async tests (via 'done')

Jasmine

describe("an async spec", function(){
 
  beforeEach(function(){
    var done = false;
 
    function doStuff(){
      // simulate async stuff and wait 10ms
      setTimeout(function(){
        done = true;
      }, 10); 
    }
 
    runs(doStuff);
 
    waitsFor(function(){
      return done;
    });
  });
 
  it("did stuff", function(){
    expect(done).toBe(true);
  });
 
});

Mocha

describe("an async spec", function(){

 beforeEach(function(done){
     setTimeout(function(){
       flag = true;
       done();
     }, 500);
  });

  it("did stuff", function(){
    expect(flag).to.be(true);
  });
 
});

Better assertions. More verbose syntax with support for language chains

Mocha

http://chaijs.com/api/bdd/
https://github.com/shouldjs/should.js#chaining-assertions

Jasmine

https://github.com/pivotal/jasmine/wiki/Matchers

Better output (string diffs, optional stack traces, many reporters)

http://visionmedia.github.io/mocha/#string-diffs
http://visionmedia.github.io/mocha/#reporters

Global require (allows to get rid of messy relative paths)

We can get rid of this:

I18n = require("../../../helpers/i18n_helper.js")
sessionHelper = require("../../../helpers/session_helper.js")
tabsHelper = require("../../../helpers/tab_helper.js")
Environment = require("../../../environments/local.json")
utils = require "../../../helpers/utils.js"
TableTester = require("../../../helpers/table_helper.js")
gridHelper = require("../../../helpers/grid_helper.js")

Supports beforeAll, afterAll

Jasmine

   it "creates sample data", ->
      # generate sample data
   it "removes sample data", ->
      # remove sample data

Mocha

    beforeAll ->
      # generate sample data

    # ... tests

    afterAll ->
      # remove sample data

Sources and discussions

http://www.techtalkdc.com/which-javascript-test-library-should-you-use-qunit-vs-jasmine-vs-mocha/
https://github.com/yeoman/yeoman/issues/117
https://github.com/angular/protractor/issues/350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment