Skip to content

Commit 926136c

Browse files
committed
Unit Testing using mocha-chai
1 parent 9a0806e commit 926136c

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "js-unit-test-examples-mocha-chai",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha"
11+
},
12+
"author": "",
13+
"license": "Unlicense",
14+
"devDependencies": {
15+
"chai": "^3.5.0",
16+
"mocha": "^3.2.0"
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = function () {
2+
this.add = function (a, b) {
3+
return a + b;
4+
};
5+
6+
this.multiply = function (a, b) {
7+
return a * b;
8+
};
9+
10+
this.fibonacci = function (length) {
11+
var sequence = [0, 1];
12+
for (var i = 2; i <= length; ++i) {
13+
sequence[i] = sequence[i - 1] + sequence[i - 2];
14+
}
15+
return sequence;
16+
};
17+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
describe('MathLib', function () {
2+
var expect = require('chai').expect;
3+
var MathLib = require('../src/index.js');
4+
5+
describe('add', function () {
6+
it('adds two numbers together', function () {
7+
var mathLib = new MathLib();
8+
var result = mathLib.add(1, 2);
9+
expect(result).to.equal(3);
10+
});
11+
});
12+
13+
describe('multiply', function () {
14+
it('multiply two numbers', function () {
15+
var mathLib = new MathLib();
16+
var result = mathLib.multiply(3, 3);
17+
expect(result).to.equal(9);
18+
});
19+
});
20+
21+
describe('fibonacci', function () {
22+
it('generates a valid fibonacci sequence', function () {
23+
var mathLib = new MathLib();
24+
var result = mathLib.fibonacci(12);
25+
expect(result[12]).to.equal(144);
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)