GithubHelp home page GithubHelp logo

mit-6170-fa15-homework3's Introduction

Using Mocha

Check out the Mocha documentation for more info.

Step 1 - Install Mocha Command Line Tool

Install the Mocha command line tool globally.

npm install -g mocha

You may need to run this command with higher permissions if it gives you an error. On Linux/MacOS, that would be:

sudo npm install -g mocha

See here for how to run command as administrator on Windows

Step 2 - Create a Test Directory and File

Create a directory called test - all your tests will go in here.

Inside that directory, let's make a sample file, call it test.js.

Step 3 - Write a Test

Put the following code inside test.js.

var assert = require("assert");

// Array is the module under test.
describe('Array', function() {
  // indexOf is the method under test.
  describe('#indexOf()', function () {
    
    // This is a test, we indicate what we're testing for.
    it('should return -1 when the value is not present', function () {
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    });


    // Another test.
    it('should find values that exist', function() {
      assert.equal(0, [1, 2, 3].indexOf(1));
      assert.equal(2, [1, 2, 3].indexOf(3));
    });

  }); // End describe indexOf.

  // map is the method under test.
  describe('#map', function() {
    
    // This is a test.
    it('should map values given a function', function() {
      assert.deepEqual([2, 4, 6], [1, 2, 3].map(function(x) { return 2 * x; }));
    });


    // Another test.
    it('should work on empty arrays', function() {
      assert.deepEqual([], [].map(function(x) { return 2 * x; }));
    });

  }); // End describe map.

}); // End describe Array.

Step 4 - Run the Tests

Navigate to the test directory in the command line and run

mocha

You should see a result like this:

  Array
    #indexOf()
      ✓ should return -1 when the value is not present
      ✓ should find values that exist
    #map
      ✓ should map values given a function
      ✓ should work on empty arrays


  4 passing (11ms)

mit-6170-fa15-homework3's People

Contributors

akashkrishnan avatar hariharsubramanyam avatar

Watchers

 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.