Giter Club home page Giter Club logo

javascript_testing_framework's Introduction

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.

javascript_testing_framework's People

Contributors

ldong avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.