Skip to content

Commit 4b674b6

Browse files
committed
test: writing some unit tests for new classes
1 parent 755367d commit 4b674b6

File tree

6 files changed

+185
-5
lines changed

6 files changed

+185
-5
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"scripts": {
1111
"lint": "eslint . --cache",
1212
"test": "npm run lint && npm run test:js",
13-
"test:js": "mocha -R spec test --bail",
14-
"test:coverage": "nyc --reporter=html --reporter=lcov mocha -- -R spec test --bail",
13+
"test:js": "mocha --bail --recursive",
14+
"test:coverage": "nyc --reporter=html --reporter=lcov mocha -- --recursive",
1515
"release": "semantic-release"
1616
},
1717
"bin": {

test/lcov-result-merger-cmd-spec.js renamed to test/test-e2e/lcov-result-merger-cmd-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
getActual,
99
getTempLcovFilePath,
1010
cleanFileDirectory,
11-
} = require('./helpers');
11+
} = require('../helpers');
1212

1313
chai.should();
1414

test/merge-coverage-report-file-streams-spec.js renamed to test/test-e2e/merge-coverage-report-file-streams-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const fastGlob = require('fast-glob');
44
const chai = require('chai');
5-
const { getActual, getExpected } = require('./helpers');
6-
const { mergeCoverageReportFilesStream } = require('../index.js');
5+
const { getActual, getExpected } = require('../helpers');
6+
const { mergeCoverageReportFilesStream } = require('../../index.js');
77

88
chai.should();
99

test/test-unit/BRDA-spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-env mocha */
2+
3+
const chai = require('chai');
4+
chai.should();
5+
6+
const BRDA = require('../../lib/BRDA');
7+
8+
describe('Unit | BRDA', function () {
9+
it('should be constructable with the new operator', async function () {
10+
const instance = new BRDA(10, 20, 30, 40);
11+
12+
instance.should.be.instanceOf(BRDA);
13+
instance.should.have.property('lineNumber', 10);
14+
instance.should.have.property('blockNumber', 20);
15+
instance.should.have.property('branchNumber', 30);
16+
instance.should.have.property('hits', 40);
17+
});
18+
19+
it('should be able to parse a string into meaningful values', async function () {
20+
BRDA.parseString('1,2,3,4').should.eql([1, 2, 3, 4]);
21+
BRDA.parseString('5,6,7,-').should.eql([5, 6, 7, '-']);
22+
});
23+
24+
it('should output a valid string representation of its content', async function () {
25+
new BRDA(10, 20, 30, 40).toString().should.equal('BRDA:10,20,30,40\n');
26+
});
27+
28+
it('should accurately increment its hit count', async function () {
29+
let instance = new BRDA(1, 2, 3, '-');
30+
instance.should.have.property('hits', '-');
31+
32+
instance.addHits('-');
33+
instance.should.have.property('hits', '-');
34+
35+
instance.addHits(1);
36+
instance.should.have.property('hits', 1);
37+
38+
instance = new BRDA(1, 2, 3, 4);
39+
instance.should.have.property('hits', 4);
40+
41+
instance.addHits('-');
42+
instance.should.have.property('hits', 4);
43+
44+
instance.addHits(1);
45+
instance.should.have.property('hits', 5);
46+
});
47+
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* eslint-env mocha */
2+
3+
const chai = require('chai');
4+
chai.should();
5+
6+
const CoverageFile = require('../../lib/CoverageFile');
7+
8+
describe('Unit | CoverageFile', function () {
9+
it('should be constructable with the new operator', async function () {
10+
const instance = new CoverageFile('test-lcov.info');
11+
12+
instance.should.be.instanceOf(CoverageFile);
13+
instance.should.have.property('filename', 'test-lcov.info');
14+
instance.should.have.property('BRDARecords').eql([]);
15+
instance.should.have.property('DARecords').eql([]);
16+
});
17+
18+
it('should allow DA records to be added', async function () {
19+
const instance = new CoverageFile('test-lcov.info');
20+
21+
instance.DARecords.should.have.length(0);
22+
23+
instance.addDA(10, 1);
24+
instance.DARecords.should.have.length(1);
25+
instance.DARecords[0].should.have.property('lineNumber', 10);
26+
instance.DARecords[0].should.have.property('hits', 1);
27+
28+
instance.parseDA('20,2');
29+
instance.DARecords.should.have.length(2);
30+
instance.DARecords[1].should.have.property('lineNumber', 20);
31+
instance.DARecords[1].should.have.property('hits', 2);
32+
});
33+
34+
it('should add hits to an existing DA record when one exists, instead of creating a duplicate', async function () {
35+
const instance = new CoverageFile('test-lcov.info');
36+
37+
instance.addDA(10, 1);
38+
instance.DARecords.should.have.length(1);
39+
instance.DARecords[0].should.have.property('lineNumber', 10);
40+
instance.DARecords[0].should.have.property('hits', 1);
41+
42+
instance.addDA(10, 3);
43+
instance.DARecords.should.have.length(1);
44+
instance.DARecords[0].should.have.property('lineNumber', 10);
45+
instance.DARecords[0].should.have.property('hits', 4);
46+
});
47+
48+
it('should allow BRDA records to be added', async function () {
49+
const instance = new CoverageFile('test-lcov.info');
50+
51+
instance.BRDARecords.should.have.length(0);
52+
53+
instance.addBRDA(10, 20, 30, 40);
54+
instance.BRDARecords.should.have.length(1);
55+
instance.BRDARecords[0].should.have.property('lineNumber', 10);
56+
instance.BRDARecords[0].should.have.property('blockNumber', 20);
57+
instance.BRDARecords[0].should.have.property('branchNumber', 30);
58+
instance.BRDARecords[0].should.have.property('hits', 40);
59+
60+
instance.parseBRDA('1,2,3,4');
61+
instance.BRDARecords.should.have.length(2);
62+
instance.BRDARecords[1].should.have.property('lineNumber', 1);
63+
instance.BRDARecords[1].should.have.property('blockNumber', 2);
64+
instance.BRDARecords[1].should.have.property('branchNumber', 3);
65+
instance.BRDARecords[1].should.have.property('hits', 4);
66+
});
67+
68+
it('should add hits to an existing BRDA record when one exists, instead of creating a duplicate', async function () {
69+
const instance = new CoverageFile('test-lcov.info');
70+
71+
instance.addBRDA(10, 20, 30, 1);
72+
instance.BRDARecords.should.have.length(1);
73+
instance.BRDARecords[0].should.have.property('lineNumber', 10);
74+
instance.BRDARecords[0].should.have.property('blockNumber', 20);
75+
instance.BRDARecords[0].should.have.property('branchNumber', 30);
76+
instance.BRDARecords[0].should.have.property('hits', 1);
77+
78+
instance.addBRDA(10, 20, 30, 3);
79+
instance.BRDARecords.should.have.length(1);
80+
instance.BRDARecords[0].should.have.property('lineNumber', 10);
81+
instance.BRDARecords[0].should.have.property('blockNumber', 20);
82+
instance.BRDARecords[0].should.have.property('branchNumber', 30);
83+
instance.BRDARecords[0].should.have.property('hits', 4);
84+
});
85+
86+
it('should output a valid string representation of its content', async function () {
87+
const instance = new CoverageFile('test-lcov.info');
88+
89+
instance.addBRDA(10, 20, 30, 40);
90+
instance.addDA(50, 60);
91+
92+
instance
93+
.toString()
94+
.should.equal(
95+
'SF:test-lcov.info\n' +
96+
'DA:50,60\n' +
97+
'BRDA:10,20,30,40\n' +
98+
'end_of_record\n'
99+
);
100+
});
101+
});

test/test-unit/DA-spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-env mocha */
2+
3+
const chai = require('chai');
4+
chai.should();
5+
6+
const DA = require('../../lib/DA');
7+
8+
describe('Unit | DA', function () {
9+
it('should be constructable with the new operator', async function () {
10+
const instance = new DA(10, 20);
11+
12+
instance.should.be.instanceOf(DA);
13+
instance.should.have.property('lineNumber', 10);
14+
instance.should.have.property('hits', 20);
15+
});
16+
17+
it('should be able to parse a string into meaningful values', async function () {
18+
DA.parseString('1,2').should.eql([1, 2]);
19+
});
20+
21+
it('should output a valid string representation of its content', async function () {
22+
new DA(10, 20).toString().should.equal('DA:10,20\n');
23+
});
24+
25+
it('should accurately increment its hit count', async function () {
26+
const instance = new DA(1, 2);
27+
instance.should.have.property('hits', 2);
28+
29+
instance.addHits(1);
30+
instance.should.have.property('hits', 3);
31+
});
32+
});

0 commit comments

Comments
 (0)