Skip to content

Commit 9a0806e

Browse files
committed
Unit Testing using jest
1 parent 45b7a7b commit 9a0806e

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-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-jest",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"author": "",
10+
"license": "Unlicense",
11+
"devDependencies": {
12+
"jest": "^18.1.0"
13+
},
14+
"jest": {
15+
"collectCoverage": true,
16+
"collectCoverageFrom": ["src/**/*.js"]
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const MathLib = require('../src');
2+
3+
describe('MathLib', () => {
4+
describe('add', () => {
5+
it('adds two numbers together', () => {
6+
const mathLib = new MathLib();
7+
const result = mathLib.add(1, 2);
8+
expect(result).toBe(3);
9+
});
10+
});
11+
12+
describe('multiply', () => {
13+
it('multiply two numbers', () => {
14+
const mathLib = new MathLib();
15+
const result = mathLib.multiply(3, 3);
16+
expect(result).toBe(9);
17+
});
18+
});
19+
20+
describe('fibonacci', () => {
21+
it('generates a valid fibonacci sequence', () => {
22+
const mathLib = new MathLib();
23+
const result = mathLib.fibonacci(12);
24+
expect(result[12]).toBe(144);
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)