Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

feat: enable reporters to show relative paths of tests (#5292)

Dinika81ea6667e928
diff --git a/docs-next/src/content/docs/reporters/xunit.mdx b/docs-next/src/content/docs/reporters/xunit.mdx
index 026c0b31..47b33063 100644
--- a/docs-next/src/content/docs/reporters/xunit.mdx
+++ b/docs-next/src/content/docs/reporters/xunit.mdx
@@ -12,3 +12,6 @@ By default, it will output to the console.
To write directly to a file, use `--reporter-option output=filename.xml`.
To specify custom report title, use `--reporter-option suiteName="Custom name"`.
+
+The reporter shows absolute file paths by default.
+To show relative paths instead, use `--reporter-option showRelativePaths`.
diff --git a/docs/index.md b/docs/index.md
index 3ee3dd82..21f758bc 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -2075,6 +2075,8 @@ By default, it will output to the console. To write directly to a file, use `--r
To specify custom report title, use `--reporter-option suiteName="Custom name"`.
+The reporter shows absolute file paths by default. To show relative paths instead, use `--reporter-option showRelativePaths`.
+
### Third-Party Reporters
Mocha allows you to define custom reporters. For more information see the [wiki][mocha-wiki-more-reporters].
diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js
index b24aa06b..5c5339b9 100644
--- a/lib/reporters/xunit.js
+++ b/lib/reporters/xunit.js
@@ -104,7 +104,7 @@ function XUnit(runner, options) {
);
tests.forEach(function (t) {
- self.test(t);
+ self.test(t, options);
});
self.write('</testsuite>');
@@ -152,13 +152,13 @@ XUnit.prototype.write = function (line) {
*
* @param {Test} test
*/
-XUnit.prototype.test = function (test) {
+XUnit.prototype.test = function (test, options) {
Base.useColors = false;
var attrs = {
classname: test.parent.fullTitle(),
name: test.title,
- file: test.file,
+ file: testFilePath(test.file, options),
time: test.duration / 1000 || 0
};
@@ -215,4 +215,12 @@ function tag(name, attrs, close, content) {
return tag;
}
+function testFilePath(filepath, options) {
+ if (options && options.reporterOptions && options.reporterOptions.showRelativePaths) {
+ return path.relative(process.cwd(), filepath);
+ }
+
+ return filepath;
+}
+
XUnit.description = 'XUnit-compatible XML output';
diff --git a/test/reporters/xunit.spec.js b/test/reporters/xunit.spec.js
index d167daae..a566af28 100644
--- a/test/reporters/xunit.spec.js
+++ b/test/reporters/xunit.spec.js
@@ -592,4 +592,69 @@ describe('XUnit reporter', function () {
expect(lines[0], 'to contain', defaultSuiteName);
});
});
+
+ describe('showRelativePaths reporter option', function () {
+ const projectPath = path.join('home', 'username', 'demo-project');
+ const relativeTestPath = path.join('tests', 'demo-test.spec.js');
+ const absoluteTestPath = path.join(projectPath, relativeTestPath);
+
+ var expectedWrite = '';
+ const fakeThis = {
+ write: function (str) {
+ expectedWrite = expectedWrite + str;
+ }
+ };
+
+ const failingTest = {
+ state: STATE_FAILED,
+ title: expectedTitle,
+ file: absoluteTestPath,
+ parent: {
+ fullTitle: function () {
+ return expectedClassName;
+ }
+ },
+ duration: 1000,
+ err: {
+ actual: 'foo',
+ expected: 'bar',
+ message: expectedMessage,
+ stack: expectedStack
+ }
+ };
+
+ beforeEach(function () {
+ sinon.stub(process, 'cwd').returns(projectPath);
+ });
+
+ afterEach(function () {
+ sinon.restore();
+ expectedWrite = '';
+ });
+
+ it('shows relative paths for tests if showRelativePaths reporter option is set', function () {
+ const options = {
+ reporterOptions: {
+ showRelativePaths: true
+ }
+ };
+ const xunit = new XUnit(runner, options);
+
+ xunit.test.call(fakeThis, failingTest, options);
+
+ expect(expectedWrite, 'not to contain', absoluteTestPath);
+ expect(expectedWrite, 'to contain', relativeTestPath);
+ });
+
+ it('shows absolute paths for tests by default', function () {
+ const options = {};
+ const xunit = new XUnit(runner);
+
+ xunit.test.call(fakeThis, failingTest, options);
+
+ expect(expectedWrite, 'to contain', absoluteTestPath);
+ // Double quote included to ensure printed paths don't start with relative path. Example printed line: <testcase classname="suite" name="test" file="some/tesfile.js" time="0"/>
+ expect(expectedWrite, 'not to contain', `"${relativeTestPath}`);
+ });
+ });
});