Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save ldong/b6350fdfbf8917ae9e4b to your computer and use it in GitHub Desktop.

Select an option

Save ldong/b6350fdfbf8917ae9e4b to your computer and use it in GitHub Desktop.
a list of javascript framework for testing.

JavaScript frameworks

Node

These frameworks/libraries are node plugins

A list of libraries

  • Chai -- BDD/TDD assertion library

  • Mocha -- Mocha is a feature-rich JavaScript test framework

  • Sinon -- Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework.

  • Sinon-chai -- Sinon–Chai provides a set of custom assertions for using the Sinon.JS spy, stub, and mocking framework with the Chai assertion library.

Chai

Chai has 3 , keywords should , expect , assert , types of tests.

See examples in './examples/chai' , run node should.js

Mocha

Mocha is the test runner runs Chai and some other tests, which has keywords and hooks.

The "hooks" are: before(), after(), beforeEach(), afterEach()

Mocha can also run tests asynchronously:

  1. Just add callback function to it() and usually named the function done.

  2. Just return a promise after the test runs

Add only to describe or it, i.e. describe.only() it.only() to explicitly state which test to run. Or use word skip describe.skip() it.skip() to explicitly state which test to omit.

There are 3 type of testing interfaces

  1. BDD: interface provides : describe(), it(), before(), after(), beforeEach(), and afterEach().

    describe('Array', function(){
      before(function(){
        // ...
      });
    
      describe('#indexOf()', function(){
        it('should return -1 when not present', function(){
          [1,2,3].indexOf(4).should.equal(-1);
        });
      });
    });
    
  2. TDD: interface provides : suite(), test(), setup(), and teardown().

    suite('Array', function(){
      setup(function(){
        // ...
      });
    
      suite('#indexOf()', function(){
        test('should return -1 when not present', function(){
          assert.equal(-1, [1,2,3].indexOf(4));
        });
      });
    });
    
  3. Exports: keys before, after, beforeEach, and afterEach are special-cased, object values are suites, and function values are test-cases. We don't really use it, so we can ignore it for now.

    module.exports = {
      before: function(){
        // ...
      },
    
      'Array': {
        '#indexOf()': {
          'should return -1 when not present': function(){
            [1,2,3].indexOf(4).should.equal(-1);
          }
        }
      }
    };
    

See a simple example in './examples/mocha', run mocha test.js

Go read the docs if you want to know more.

Sinon

Sinon is like python Mock module.

Oh, if you don't know what Mock is, please read this doc

There are so many things Sinon can mock of, for example:

  1. Spies, are functions that monitors the target function. Creates an anonymous function that records arguments, this value, exceptions and return values for all calls.

  2. Stubs, are functions (spies) with pre-programmed behavior.

  3. Mocks, Mocks (and mock expectations) are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations . A mock will fail your test if it is not used as expected.

  4. Fake timers, is a synchronous implementation of setTimeout and friends that Sinon.JS can overwrite the global functions with to allow you to more easily test code using them.

More : Fake timers, Fake XMLHttpRequest, Fake JSON-P, Assertions

Sinon-chai

Sinon-chai (Sinon.JS Assertions for Chai) is a plugin for Sinon.

It provides a set of custom assertions for using the Sinon.JS spy, stub, and mocking framework with the Chai assertion library.

@jennyscript
Copy link
Copy Markdown

Currently the tests use the Mocha BDD interface. Notes about installing and running the tests in DV2 can be found at the bottom of the ui readme https://github.com/synappio/DataValidation/blob/master/ui/README.md

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