Skip to content

Commit ba75aee

Browse files
committed
Unit Testing using mocha-chai-ts-istanbul
1 parent 4cb1106 commit ba75aee

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "js-unit-test-examples-mocha-chai-ts-istanbul",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "ts-node node_modules/istanbul/lib/cli.js cover -e .ts node_modules/mocha/bin/_mocha -- test/**/*.test.ts"
11+
},
12+
"author": "",
13+
"license": "Unlicense",
14+
"devDependencies": {
15+
"@types/mocha": "^2.2.38",
16+
"@types/node": "^7.0.4",
17+
"chai": "^3.5.0",
18+
"istanbul": "^1.1.0-alpha.1",
19+
"mocha": "^3.2.0",
20+
"ts-node": "^2.0.0",
21+
"typescript": "^2.1.5"
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export class MathLib {
2+
add(a, b): Number {
3+
return a + b;
4+
}
5+
6+
multiply(a, b): Number {
7+
return a * b;
8+
}
9+
10+
fibonacci(length): Number[] {
11+
const sequence = [0, 1];
12+
for (let i = 2; i <= length; ++i) {
13+
sequence[i] = sequence[i - 1] + sequence[i - 2];
14+
}
15+
return sequence;
16+
};
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="../node_modules/@types/node/index.d.ts" />
2+
/// <reference path="../node_modules/@types/mocha/index.d.ts" />
3+
4+
import { expect } from 'chai';
5+
import { MathLib } from '../src';
6+
7+
describe('MathLib', () => {
8+
describe('add', () => {
9+
it('adds two numbers together', () => {
10+
const mathLib = new MathLib();
11+
const result = mathLib.add(1, 2);
12+
expect(result).to.equal(3);
13+
});
14+
});
15+
16+
describe('multiply', () => {
17+
it('multiply two numbers', () =>{
18+
const mathLib = new MathLib();
19+
const result = mathLib.multiply(3, 3);
20+
expect(result).to.equal(9);
21+
});
22+
});
23+
24+
describe('fibonacci', () =>{
25+
it('generates a valid fibonacci sequence', () =>{
26+
const mathLib = new MathLib();
27+
const result = mathLib.fibonacci(12);
28+
expect(result[12]).to.equal(144);
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)