Skip to content

Commit 0f76ce7

Browse files
authored
To report code coverage of tests run with "npm run test" (#106)
To report code coverage of tests run with "npm run test" (#106)
1 parent c9caa51 commit 0f76ce7

File tree

5 files changed

+1354
-25
lines changed

5 files changed

+1354
-25
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ tmp
55
autolispext*.zip
66
package.nls.*.json
77

8+
.nyc_output
9+
.vscode-test
10+
coverage
11+
812
utils/HelpAbstractionGenerator/**/*.pdb
913
utils/HelpAbstractionGenerator/**/*.xml
1014
utils/HelpAbstractionGenerator/.vs

.vscodeignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.git/**
2+
.nyc_output/**
23
.vscode/**
34
.vscode-test/**
5+
coverage/**
46
extension/out/test/**
57
extension/out/**/*.map
68
extension/src/**

extension/src/test/suite/index.ts

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import * as path from 'path';
22
import * as Mocha from 'mocha';
33
import * as glob from 'glob';
44

5-
export function run(): Promise<void> {
5+
import * as fs from 'fs-extra';
6+
import * as baseConfig from "@istanbuljs/nyc-config-typescript";
7+
8+
import 'ts-node/register';
9+
import 'source-map-support/register';
10+
11+
const NYC = require('nyc');
12+
13+
export async function run(): Promise<void> {
614
// Create the mocha test
715
const mocha = new Mocha({
816
ui: 'tdd'
@@ -11,28 +19,61 @@ export function run(): Promise<void> {
1119

1220
const testsRoot = path.resolve(__dirname, '..');
1321

14-
return new Promise((c, e) => {
15-
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
16-
if (err) {
17-
return e(err);
18-
}
19-
20-
// Add files to the test suite
21-
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
22-
23-
try {
24-
// Run the mocha test
25-
mocha.run(failures => {
26-
if (failures > 0) {
27-
e(new Error(`${failures} tests failed.`));
28-
} else {
29-
c();
30-
}
31-
});
32-
} catch (err) {
33-
console.error(err);
34-
e(err);
35-
}
36-
});
22+
const nyc = await setupNYC();
23+
24+
// Add all test files to mocha
25+
const testFiles = glob.sync('**/*.test.js', { cwd: testsRoot });
26+
testFiles.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
27+
28+
const failures: number = await new Promise(resolve => mocha.run(resolve));
29+
30+
await reportCodeCoverage(nyc);
31+
32+
if (failures > 0) {
33+
throw new Error(`${failures} tests failed.`);
34+
}
35+
}
36+
37+
async function setupNYC() {
38+
let nyc = new NYC({
39+
...baseConfig,
40+
all: true,
41+
cwd: path.join(__dirname, '..', '..', '..'),
42+
exclude: ["out/test/**"],
43+
include: ["out/**/*.js"],
44+
instrument: true,
45+
reporter: ['text-summary', 'html'],
46+
hookRequire: true,
47+
hookRunInContext: true,
48+
hookRunInThisContext: true,
49+
silent: false
3750
});
38-
}
51+
await nyc.wrap();
52+
53+
// Delete the 'coverage' folder first to make sure the HTML report is only for current run of npm test
54+
const tempDirectory = nyc.tempDirectory();
55+
if(fs.existsSync(tempDirectory)) {
56+
fs.removeSync(tempDirectory);
57+
}
58+
await nyc.createTempDirectory();
59+
60+
return nyc;
61+
}
62+
63+
async function reportCodeCoverage(nyc) {
64+
await nyc.writeCoverageFile();
65+
66+
let textReport = '';
67+
68+
let currentWrite = process.stdout.write;
69+
process.stdout.write = (s) => { textReport = textReport + s; return true; };
70+
71+
await nyc.report.bind(nyc)();
72+
73+
process.stdout.write = currentWrite;
74+
75+
console.log(textReport);
76+
console.log("--------------------------------------------------------");
77+
console.log("Open coverage folder to check detailed report in HTML.");
78+
console.log("--------------------------------------------------------");
79+
}

0 commit comments

Comments
 (0)