Skip to content

Commit fd46c26

Browse files
authored
Enable JUnit report output during testRunner processing. (#1901)
Reporting support for VSTS primarily. - Add reporter & reporterOptions to testRunner - Add support for JUnit output file
1 parent 4ddebe2 commit fd46c26

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

news/3 Code Health/1897.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add JUnit file output to enable CI integration with VSTS.

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,8 +1926,8 @@
19261926
"@types/chai-arrays": "^1.0.2",
19271927
"@types/chai-as-promised": "^7.1.0",
19281928
"@types/del": "^3.0.0",
1929-
"@types/download": "^6.2.2",
19301929
"@types/dotenv": "^4.0.3",
1930+
"@types/download": "^6.2.2",
19311931
"@types/event-stream": "^3.3.33",
19321932
"@types/fs-extra": "^5.0.1",
19331933
"@types/get-port": "^3.2.0",
@@ -1971,6 +1971,7 @@
19711971
"is-running": "^2.1.0",
19721972
"istanbul": "^0.4.5",
19731973
"mocha": "^5.0.4",
1974+
"mocha-junit-reporter": "^1.17.0",
19741975
"node-has-native-dependencies": "^1.0.2",
19751976
"relative": "^3.0.2",
19761977
"remap-istanbul": "^0.10.1",

src/test/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export const IS_APPVEYOR = process.env.APPVEYOR === 'true';
88
export const IS_TRAVIS = process.env.TRAVIS === 'true';
99
export const IS_VSTS = process.env.TF_BUILD !== undefined;
1010
export const IS_CI_SERVER = IS_TRAVIS || IS_APPVEYOR || IS_VSTS;
11+
12+
// allow the CI server to specify JUnit output...
13+
export const MOCHA_REPORTER_JUNIT: boolean = IS_CI_SERVER && process.env.MOCHA_REPORTER_JUNIT !== undefined;
14+
export const MOCHA_CI_REPORTFILE: string = MOCHA_REPORTER_JUNIT && process.env.MOCHA_CI_REPORTFILE !== undefined ? process.env.MOCHA_CI_REPORTFILE.toString() : './junit-out.xml';
15+
export const MOCHA_CI_PROPERTIES: string = MOCHA_REPORTER_JUNIT && process.env.MOCHA_CI_PROPERTIES !== undefined ? process.env.MOCHA_CI_PROPERTIES.toString() : '';
16+
1117
export const TEST_TIMEOUT = 25000;
1218
export const IS_MULTI_ROOT_TEST = isMultitrootTest();
1319
export const IS_CI_SERVER_TEST_DEBUGGER = process.env.IS_CI_SERVER_TEST_DEBUGGER === '1';

src/test/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ if ((Reflect as any).metadata === undefined) {
55
}
66

77
import { IS_CI_SERVER, IS_CI_SERVER_TEST_DEBUGGER,
8-
IS_MULTI_ROOT_TEST, IS_VSTS } from './constants';
8+
IS_MULTI_ROOT_TEST, IS_VSTS, MOCHA_CI_PROPERTIES,
9+
MOCHA_CI_REPORTFILE, MOCHA_REPORTER_JUNIT } from './constants';
910
import * as testRunner from './testRunner';
1011

1112
process.env.VSC_PYTHON_CI_TEST = '1';
@@ -22,11 +23,28 @@ const testFilesSuffix = process.env.TEST_FILES_SUFFIX;
2223
// Hack, as retries is not supported as setting in tsd.
2324
const options: testRunner.SetupOptions & { retries: number } = {
2425
ui: 'tdd',
25-
useColors: !IS_VSTS,
26+
useColors: true,
2627
timeout: 25000,
2728
retries: 3,
2829
grep,
2930
testFilesSuffix
3031
};
32+
33+
// VSTS CI doesn't display colours correctly (yet).
34+
if (IS_VSTS) {
35+
options.useColors = false;
36+
}
37+
38+
// CI can ask for a JUnit reporter if the environment variable
39+
// 'MOCHA_REPORTER_JUNIT' is defined, further control is afforded
40+
// by other 'MOCHA_CI_...' variables. See constants.ts for info.
41+
if (MOCHA_REPORTER_JUNIT) {
42+
options.reporter = 'mocha-junit-reporter';
43+
options.reporterOptions = {
44+
mochaFile: MOCHA_CI_REPORTFILE,
45+
properties: MOCHA_CI_PROPERTIES
46+
};
47+
}
48+
3149
testRunner.configure(options, { coverageConfig: '../coverconfig.json' });
3250
module.exports = testRunner;

src/test/testRunner.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ let mocha = new Mocha(<any>{
4949
useColors: true
5050
});
5151

52-
export type SetupOptions = MochaSetupOptions & { testFilesSuffix?: string };
52+
export type SetupOptions = MochaSetupOptions & {
53+
testFilesSuffix?: string;
54+
reporter?: string;
55+
reporterOptions?: {
56+
mochaFile?: string;
57+
properties?: string;
58+
};
59+
};
60+
5361
let testFilesGlob = 'test';
5462
let coverageOptions: { coverageConfig: string } | undefined;
5563

0 commit comments

Comments
 (0)