Skip to content

Commit 81ea666

Browse files
DinikaJoshuaKGoldberg
andauthoredMar 17, 2025··
feat: enable reporters to show relative paths of tests (#5292)
* DRAFT // Get test file path relative to config Signed-off-by: Dinika Saxena <dinika@greyllama.cc> * Use cwd for finding relative path of test fil * Use relative path options in json and json-stream reporters * Add integration test for relative paths in xunit reporter Signed-off-by: Dinika Saxena <dinika@greyllama.cc> * Make test more complete by checking for non existence * Keep showing absolute paths in json reporters * Add doc for showRelativePath xunit reporter option Signed-off-by: Dinika Saxena <dinika@greyllama.cc> * Move relative path calculation to the only reporter (xunit) that needs it * Fix typo in xunit doc for showRelativePaths option Signed-off-by: Dinika Saxena <dinika@greyllama.cc> * Clarify why testing for absence of relative paths need quotes * Copied new docs .md contents to docs-next .mdx --------- Signed-off-by: Dinika Saxena <dinika@greyllama.cc> Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
1 parent b1f1cb7 commit 81ea666

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed
 

‎docs-next/src/content/docs/reporters/xunit.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ By default, it will output to the console.
1212
To write directly to a file, use `--reporter-option output=filename.xml`.
1313

1414
To specify custom report title, use `--reporter-option suiteName="Custom name"`.
15+
16+
The reporter shows absolute file paths by default.
17+
To show relative paths instead, use `--reporter-option showRelativePaths`.

‎docs/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,8 @@ By default, it will output to the console. To write directly to a file, use `--r
20752075

20762076
To specify custom report title, use `--reporter-option suiteName="Custom name"`.
20772077

2078+
The reporter shows absolute file paths by default. To show relative paths instead, use `--reporter-option showRelativePaths`.
2079+
20782080
### Third-Party Reporters
20792081

20802082
Mocha allows you to define custom reporters. For more information see the [wiki][mocha-wiki-more-reporters].

‎lib/reporters/xunit.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function XUnit(runner, options) {
104104
);
105105

106106
tests.forEach(function (t) {
107-
self.test(t);
107+
self.test(t, options);
108108
});
109109

110110
self.write('</testsuite>');
@@ -152,13 +152,13 @@ XUnit.prototype.write = function (line) {
152152
*
153153
* @param {Test} test
154154
*/
155-
XUnit.prototype.test = function (test) {
155+
XUnit.prototype.test = function (test, options) {
156156
Base.useColors = false;
157157

158158
var attrs = {
159159
classname: test.parent.fullTitle(),
160160
name: test.title,
161-
file: test.file,
161+
file: testFilePath(test.file, options),
162162
time: test.duration / 1000 || 0
163163
};
164164

@@ -215,4 +215,12 @@ function tag(name, attrs, close, content) {
215215
return tag;
216216
}
217217

218+
function testFilePath(filepath, options) {
219+
if (options && options.reporterOptions && options.reporterOptions.showRelativePaths) {
220+
return path.relative(process.cwd(), filepath);
221+
}
222+
223+
return filepath;
224+
}
225+
218226
XUnit.description = 'XUnit-compatible XML output';

‎test/reporters/xunit.spec.js

+65
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,69 @@ describe('XUnit reporter', function () {
592592
expect(lines[0], 'to contain', defaultSuiteName);
593593
});
594594
});
595+
596+
describe('showRelativePaths reporter option', function () {
597+
const projectPath = path.join('home', 'username', 'demo-project');
598+
const relativeTestPath = path.join('tests', 'demo-test.spec.js');
599+
const absoluteTestPath = path.join(projectPath, relativeTestPath);
600+
601+
var expectedWrite = '';
602+
const fakeThis = {
603+
write: function (str) {
604+
expectedWrite = expectedWrite + str;
605+
}
606+
};
607+
608+
const failingTest = {
609+
state: STATE_FAILED,
610+
title: expectedTitle,
611+
file: absoluteTestPath,
612+
parent: {
613+
fullTitle: function () {
614+
return expectedClassName;
615+
}
616+
},
617+
duration: 1000,
618+
err: {
619+
actual: 'foo',
620+
expected: 'bar',
621+
message: expectedMessage,
622+
stack: expectedStack
623+
}
624+
};
625+
626+
beforeEach(function () {
627+
sinon.stub(process, 'cwd').returns(projectPath);
628+
});
629+
630+
afterEach(function () {
631+
sinon.restore();
632+
expectedWrite = '';
633+
});
634+
635+
it('shows relative paths for tests if showRelativePaths reporter option is set', function () {
636+
const options = {
637+
reporterOptions: {
638+
showRelativePaths: true
639+
}
640+
};
641+
const xunit = new XUnit(runner, options);
642+
643+
xunit.test.call(fakeThis, failingTest, options);
644+
645+
expect(expectedWrite, 'not to contain', absoluteTestPath);
646+
expect(expectedWrite, 'to contain', relativeTestPath);
647+
});
648+
649+
it('shows absolute paths for tests by default', function () {
650+
const options = {};
651+
const xunit = new XUnit(runner);
652+
653+
xunit.test.call(fakeThis, failingTest, options);
654+
655+
expect(expectedWrite, 'to contain', absoluteTestPath);
656+
// 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"/>
657+
expect(expectedWrite, 'not to contain', `"${relativeTestPath}`);
658+
});
659+
});
595660
});

0 commit comments

Comments
 (0)
Please sign in to comment.