Skip to content

Instantly share code, notes, and snippets.

@shiftyp
Last active August 29, 2015 14:17
Show Gist options
  • Save shiftyp/9be535ceba8480124924 to your computer and use it in GitHub Desktop.
Save shiftyp/9be535ceba8480124924 to your computer and use it in GitHub Desktop.
Mocha Unit Testing Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mocha Example</title>
<link href="//cdnjs.cloudflare.com/ajax/libs/mocha/2.2.1/mocha.min.css" rel="stylesheet">
</head>
<body>
<div id="mocha"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/mocha/2.2.1/mocha.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chai/2.1.1/chai.min.js" type="text/javascript"></script>
<script src="./math.test.js" type="text/javascript"></script>
</body>
</html>
var expect = chai.expect;
mocha.setup('bdd');
describe('Math', function(){
describe('#max', function() {
it('should return the maximum value given n values', function(){
expect(Math.max(1)).to.equal(1);
expect(Math.max(1, 2)).to.equal(2);
expect(Math.max(1, 2, 3)).to.equal(3);
});
it('should return NaN when given a value that cannot be parsed to a number', function(){
expect(isNaN(Math.max(1, {}))).to.be.true;
expect(isNaN(Math.max(1, undefined))).to.be.true
});
it('should return the appropriate maximum for values that can be parsed to a number', function(){
expect(Math.max(1, null)).to.equal(1);
expect(Math.max(2, true)).to.equal(2);
expect(Math.max(1, false)).to.equal(1);
expect(Math.max(1, '2')).to.equal(2);
});
});
describe('#min', function() {
it('should return the minimum value given n values', function(){
expect(Math.min(1)).to.equal(1);
expect(Math.min(1, 2)).to.equal(1);
expect(Math.min(1, 2, 3)).to.equal(1);
});
it('should return NaN when given a value that cannot be parsed to a number', function(){
expect(isNaN(Math.min(1, {}))).to.be.true;
expect(isNaN(Math.min(1, undefined))).to.be.true
});
it('should return the appropriate minimum for values that can be parsed to a number', function(){
expect(Math.min(1, null)).to.equal(0);
expect(Math.min(2, true)).to.equal(1);
expect(Math.min(1, false)).to.equal(0);
expect(Math.min('1', 2)).to.equal(1);
});
});
});
mocha.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment