Skip to content

Commit af5cf92

Browse files
author
Simon Renoult
committed
test: add tests for CLI
Also adds a timeout for mocha since running these new tests take a few seconds.
1 parent 6049669 commit af5cf92

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"prepare": "npm run lint && npm run build",
2626
"postversion": "git push --tags",
2727
"pretest": "npm run lint",
28-
"test": "mocha --require ts-node/register test/**/*.ts",
28+
"test": "mocha --timeout=5000 --require=ts-node/register test/**/*.ts",
2929
"test:ci": "nyc --reporter=lcov npm test"
3030
},
3131
"keywords": [

test/io/statistics.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { execSync } from "child_process";
2+
import TestRepositoryFixture from "../fixtures/test-repository.fixture";
3+
import { expect } from "chai";
4+
5+
describe("CLI", () => {
6+
context("when format is json", () => {
7+
it("generates the appropriate output", () => {
8+
// Given
9+
const repo = new TestRepositoryFixture();
10+
repo
11+
.addFile({ name: "a.js", lines: 2, commits: 2 })
12+
.addFile({ name: "b.ts", lines: 1, commits: 1 })
13+
.writeOnDisk();
14+
15+
// When
16+
const result = execSync(
17+
`npx ts-node bin/code-complexity.ts ${repo.location} --format=json`,
18+
{ encoding: "utf8" }
19+
).trim();
20+
21+
// Then
22+
expect(result).to.deep.equal(
23+
JSON.stringify([
24+
{ path: "a.js", churn: 2, complexity: 2, score: 4 },
25+
{ path: "b.ts", churn: 1, complexity: 1, score: 1 },
26+
])
27+
);
28+
});
29+
});
30+
31+
context("when format is csv", () => {
32+
it("generates the appropriate output", () => {
33+
// Given
34+
const repo = new TestRepositoryFixture();
35+
repo
36+
.addFile({ name: "a.js", lines: 2, commits: 2 })
37+
.addFile({ name: "b.ts", lines: 1, commits: 1 })
38+
.writeOnDisk();
39+
40+
// When
41+
const result = execSync(
42+
`npx ts-node bin/code-complexity.ts ${repo.location} --format=csv`,
43+
{ encoding: "utf8" }
44+
).trim();
45+
46+
// Then
47+
expect(result).to.deep.equal(
48+
["file,complexity,churn,score", "a.js,2,2,4", "b.ts,1,1,1"].join("\n")
49+
);
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)