-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: enable reporters to show relative paths of tests #5292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable reporters to show relative paths of tests #5292
Conversation
167243a
to
9455223
Compare
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
9455223
to
ddc88db
Compare
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
d52bc24
to
4a82dc6
Compare
Hi @JoshuaKGoldberg, I just wanted to follow up on this PR. There’s absolutely no rush—I just wanted to make sure this doesn’t get lost (in case the feature is still needed). Apologies for the direct mention, and thanks in advance for your time! 😊 |
Not lost, and I appreciate the ping - thanks for checking in! I've been swamped with non-Mocha work for the last few weeks but hope to get to this soon. |
P.S. the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A solid start! The implementation is pretty reasonable and I like the tests. Requesting changes on docs and a teeny bit more testing.
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
8ab406b
to
e4ce28b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Looking great, thanks for working with me! The code feels clean and works well. Nicely done! 👏
Approving with just a teeny docs nit we can fix before merging. I'll also want to leave this open for bit in case anybody from @mochajs/committers wants to take a look.
388084e
to
59de078
Compare
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
59de078
to
8c545b7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
60b7640
to
8898907
Compare
@@ -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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that #5246 is merged, I went ahead and copied this over. FYI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a showRelativePaths
reporter option for XUnit, allowing test file paths to be shown relative to the working directory instead of absolute.
Key changes:
- Implemented
testFilePath
helper and wired it into XUnit’stest
method. - Added unit tests for relative vs. absolute path output.
- Updated both docs and docs-next to mention the new option.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
test/reporters/xunit.spec.js | Added tests covering showRelativePaths behavior |
lib/reporters/xunit.js | Added testFilePath function and passed options |
docs/index.md | Documented the showRelativePaths option |
docs-next/src/content/docs/reporters/xunit.mdx | Added relative-path option to XUnit reporter docs |
Comments suppressed due to low confidence (3)
docs/index.md:2078
- [nitpick] Clarify that
showRelativePaths
currently applies only to the XUnit reporter and include a usage example with the full flag syntax (e.g.,--reporter-option showRelativePaths=true
).
The reporter shows absolute file paths by default. To show relative paths instead, use `--reporter-option showRelativePaths`.
docs-next/src/content/docs/reporters/xunit.mdx:17
- [nitpick] Add a note that this option is specific to the XUnit reporter and demonstrate the complete CLI flag (e.g.,
--reporter-option showRelativePaths=true
) for clarity.
To show relative paths instead, use `--reporter-option showRelativePaths`.
lib/reporters/xunit.js:218
- Make sure the
path
module is imported at the top of this file (e.g.,const path = require('path')
), sincetestFilePath
usespath.relative
.
function testFilePath(filepath, options) {
|
||
describe('showRelativePaths reporter option', function () { | ||
const projectPath = path.join('home', 'username', 'demo-project'); | ||
const relativeTestPath = path.join('tests', 'demo-test.spec.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider computing the expected relative path with path.relative(process.cwd(), absoluteTestPath)
instead of hardcoding path.join
to ensure the test works consistently across different platforms.
const relativeTestPath = path.join('tests', 'demo-test.spec.js'); | |
const relativeTestPath = path.relative(process.cwd(), absoluteTestPath); |
Copilot uses AI. Check for mistakes.
PR Checklist
status: accepting prs
Overview
This PR adds a new reporter option
showRelativePaths
. If this option is set the selected reporter will show relative paths to test files instead of absolute paths (which is the default behavior).The paths are shown relative to the directory where mocha command is executed from.
For example, if this option is passed to the json reporter (using config file or CLI
mocha --reporter=json --reporter-option showRelativePaths=true
), the output should be similar to:In contrast, the output without
showRelativePaths
option set looks like this:Design
There were a number of different places where the computation for relative path could be done. In the end, I chose the strategy that:
file
property of theTest
object or add another property likerelativeFile
on theTest
object)Allows any reporter (even custom ones) to show relative paths - For this reason I've added the logic in the Base reporter.I've updated the code for xunit, json, and json-stream reporters (i.e. reporters that show test paths) to handle this option, however I've only added the tests for xunit. Please lemme know if more tests should be added or a different strategy is better suited for this feature.Based on the discussions in this thread the logic of computing relative paths is moved to lib/reporters/xunit.js file since only the xunit reporter is going to support the
showRelativePaths
option for now.How have I tested this PR
In addition to testing with the integration tests, I also used the mocha-examples repo to add a new package and try different reporters.