Skip to content

Instantly share code, notes, and snippets.

@msteckyefantis
Last active April 26, 2016 04:13
Show Gist options
  • Save msteckyefantis/cd4b88974b06047085dbdca0205d6c3c to your computer and use it in GitHub Desktop.
Save msteckyefantis/cd4b88974b06047085dbdca0205d6c3c to your computer and use it in GitHub Desktop.
NodeJs testing example, 'requiring' chai, run with terminal command: mocha <location of file> (e.g. mocha test.js)
'use strict';
const expect = require( 'chai' ).expect;
// testing example function f (you normally "require" the file you want to test)
function f( x ) {
return( x + x + x + x );
}
// the "describe" function works when you run this file with mocha
// "describe" gives what you're testing a description
describe( 'testing function f', function() {
// "its" are sections within your "describe" that contain the specific tests you want to do
it( 'with x = 5', function() {
let test_x = 5;
let expected_value_of_f = 4 * test_x;
expect( f( test_x ) ).to.equal( expected_value_of_f );
});
it( 'with x = 0', function() {
let test_x = 0;
let expected_value_of_f = 0;
expect( f( test_x ) ).to.equal( expected_value_of_f );
});
});
/*
Links:
Chai ----> https://github.com/chaijs/chai-http
Mocha ---> https://mochajs.org/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment