Testing your own code using Mocha and Chai (simple example)

To help you with @P1xt’s new algorithm competition, you may want to have your own simple test runner to ensure your functions return accurate results.

I used Mocha and Chai. Here’s my code and a walk through for using it.

Code 'test.js'
var should = require( 'chai' ).should();
var basic = require( './multJacksonBates' );

describe('basic', function() {
  it('should return 23 when passed 10', function() {
    basic(10).should.equal(23);
  })
  it('should return 78 when passed 20', function() {
    basic(20).should.equal(78);
  })
  it('should return 2318 when passed 100', function() {
    basic(100).should.equal(2318);
  })
  it('should return 23331668 when passed 10000', function() {
    basic(10000).should.equal(23331668);
  })
  it('should return 486804150 when passed 45678', function() {
    basic(45678).should.equal(486804150);
  })
})

Walk through

Pre-req: Install Node & NPM, you can open and type in your OS’s terminal / command line

  • Create a directory for your algorithms and testing, eg: mkdir algos
  • Navigate to said directory: cd algos
  • npm init and accept all defaults
  • npm install -g mocha
  • npm install mocha chai --save
  • Write a function you want to test, and export it:
// multJacksonBates.js
function multJacksonBates(n) {
  return 23;
}

module.exports = multJacksonBates;
  • Notice the code snippet for the test script requires the multJacksonBates function created here and names it basic
  • Test the script with mocha test.js
  • It should say the first test passed because it is hardcoded to return the right answer for basic(10).
  • Write different code in the function so that it passes all test cases. And change the name of the function.
  • You can add more functions and more tests by replicating the code snips above.

Additional resources

If you decide to write several functions and want to test them for efficiency, you can modify @P1xt’s benchmark script from here: http://forum.freecodecamp.com/t/algorithm-return-largest-numbers-in-arrays/16042/7?u=jacksonbates

You can find more information about Mocha and Chai at the following links:

Any suggestions for improving my testing are welcome!

I only did this properly for the first time today :slight_smile:

15 Likes

Hi, thanks for posting this! I saw your post in the other topic and thought about using mocha and chai as well, so this definitely helps.

No worries :slight_smile:

Definitely get the benchmarking set up as well. I got a x7 speed bump trying out one refactor, and then a x33 increase when I tried something else. I think I just fell in love with testing and refactoring!

1 Like

This may be a noob question but I don’t know where to write the test function and export it. I am new to this and have never used npm before.

Hi, so where are you now? Do you have node and npm installed, npm init, npm install -g mocha, npm install mocha chai --save?

For the function you want to test you can use the structure @JacksonBates used:

function functionName() {
  // do whatever you want
  return "Anything you want" // return something;
}

module.exports = functionName;

You can also copy the test.js, you will only have to change this: var basic = require( './multJacksonBates' ); to: var basic = require( './yourFileName' );

And of course depending on what your function does, you should change the tests.

Then run mocha test.js, and everything should work.

Not sure if this helps, but maybe you can tell what you have done already?

2 Likes

I got it working! Thank you so much! :smiley:

1 Like