Skip to content

Commit cf4e4c1

Browse files
authored
Add JUnit reporter for unit tests (microsoft#104354)
1 parent e10aa18 commit cf4e4c1

2 files changed

Lines changed: 40 additions & 26 deletions

File tree

test/unit/browser/index.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const glob = require('glob');
1010
const fs = require('fs');
1111
const events = require('events');
1212
const mocha = require('mocha');
13+
const MochaJUnitReporter = require('mocha-junit-reporter');
1314
const url = require('url');
1415
const minimatch = require('minimatch');
1516
const playwright = require('playwright');
@@ -37,30 +38,44 @@ if (argv.help) {
3738
}
3839

3940
const withReporter = (function () {
40-
const reporterPath = path.join(path.dirname(require.resolve('mocha')), 'lib', 'reporters', argv.reporter);
41-
let ctor;
41+
if (argv.tfs) {
42+
{
43+
return (browserType, runner) => {
44+
new mocha.reporters.Spec(runner);
45+
new MochaJUnitReporter(runner, {
46+
reporterOptions: {
47+
testsuitesTitle: `${argv.tfs} ${process.platform}`,
48+
mochaFile: process.env.BUILD_ARTIFACTSTAGINGDIRECTORY ? path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${browserType}-${argv.tfs.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`) : undefined
49+
}
50+
});
51+
}
52+
}
53+
} else {
54+
const reporterPath = path.join(path.dirname(require.resolve('mocha')), 'lib', 'reporters', argv.reporter);
55+
let ctor;
4256

43-
try {
44-
ctor = require(reporterPath);
45-
} catch (err) {
4657
try {
47-
ctor = require(argv.reporter);
58+
ctor = require(reporterPath);
4859
} catch (err) {
49-
ctor = process.platform === 'win32' ? mocha.reporters.List : mocha.reporters.Spec;
50-
console.warn(`could not load reporter: ${argv.reporter}, using ${ctor.name}`);
60+
try {
61+
ctor = require(argv.reporter);
62+
} catch (err) {
63+
ctor = process.platform === 'win32' ? mocha.reporters.List : mocha.reporters.Spec;
64+
console.warn(`could not load reporter: ${argv.reporter}, using ${ctor.name}`);
65+
}
5166
}
52-
}
5367

54-
function parseReporterOption(value) {
55-
let r = /^([^=]+)=(.*)$/.exec(value);
56-
return r ? { [r[1]]: r[2] } : {};
57-
}
68+
function parseReporterOption(value) {
69+
let r = /^([^=]+)=(.*)$/.exec(value);
70+
return r ? { [r[1]]: r[2] } : {};
71+
}
5872

59-
let reporterOptions = argv['reporter-options'];
60-
reporterOptions = typeof reporterOptions === 'string' ? [reporterOptions] : reporterOptions;
61-
reporterOptions = reporterOptions.reduce((r, o) => Object.assign(r, parseReporterOption(o)), {});
73+
let reporterOptions = argv['reporter-options'];
74+
reporterOptions = typeof reporterOptions === 'string' ? [reporterOptions] : reporterOptions;
75+
reporterOptions = reporterOptions.reduce((r, o) => Object.assign(r, parseReporterOption(o)), {});
6276

63-
return (runner) => new ctor(runner, { reporterOptions })
77+
return (_, runner) => new ctor(runner, { reporterOptions })
78+
}
6479
})()
6580

6681
const outdir = argv.build ? 'out-build' : 'out';
@@ -137,7 +152,7 @@ async function runTestsInBrowser(testModules, browserType) {
137152
console[msg.type()](msg.text(), await Promise.all(msg.args().map(async arg => await arg.jsonValue())));
138153
});
139154

140-
withReporter(new EchoRunner(emitter, browserType.toUpperCase()));
155+
withReporter(browserType, new EchoRunner(emitter, browserType.toUpperCase()));
141156

142157
// collection failures for console printing
143158
const fails = [];

test/unit/electron/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { join } = require('path');
99
const path = require('path');
1010
const mocha = require('mocha');
1111
const events = require('events');
12-
// const MochaJUnitReporter = require('mocha-junit-reporter');
12+
const MochaJUnitReporter = require('mocha-junit-reporter');
1313
const url = require('url');
1414

1515
const defaultReporterName = process.platform === 'win32' ? 'list' : 'spec';
@@ -136,13 +136,12 @@ app.on('ready', () => {
136136

137137
if (argv.tfs) {
138138
new mocha.reporters.Spec(runner);
139-
// TODO@deepak the mocha Junit reporter seems to cause a hang when running with Electron 6 inside docker container
140-
// new MochaJUnitReporter(runner, {
141-
// reporterOptions: {
142-
// testsuitesTitle: `${argv.tfs} ${process.platform}`,
143-
// mochaFile: process.env.BUILD_ARTIFACTSTAGINGDIRECTORY ? path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${argv.tfs.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`) : undefined
144-
// }
145-
// });
139+
new MochaJUnitReporter(runner, {
140+
reporterOptions: {
141+
testsuitesTitle: `${argv.tfs} ${process.platform}`,
142+
mochaFile: process.env.BUILD_ARTIFACTSTAGINGDIRECTORY ? path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${argv.tfs.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`) : undefined
143+
}
144+
});
146145
} else {
147146
const reporterPath = path.join(path.dirname(require.resolve('mocha')), 'lib', 'reporters', argv.reporter);
148147
let Reporter;

0 commit comments

Comments
 (0)