Skip to content

Conversation

Dinika
Copy link
Contributor

@Dinika Dinika commented Feb 5, 2025

PR Checklist

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:

{
  "stats": {
    "suites": 1,
    "duration": 2
  },
  "tests": [
    {
      ...// other test properties
      "file": "test/dummy-test.spec.js",
    }
  ],
  "pending": [],
  "failures": [],
  "passes": [
    {
      ...// other test properties
      "file": "test/dummy-test.spec.js",
    }
  ]
}

In contrast, the output without showRelativePaths option set looks like this:

{
  "stats": {
    "suites": 1,
    "duration": 2
  },
  "tests": [
    {
       ...// other test properties
      "file": "/home/dinika/code/personal/mocha-examples/packages/relative-file-path/test/dummy-test.spec.js",
    }
  ],
  "pending": [],
  "failures": [],
  "passes": [
    {
      ... // other test properties
      "file": "/home/dinika/code/personal/mocha-examples/packages/relative-file-path/test/dummy-test.spec.js",
    }
  ]
}

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:

  • Only affects mocha reporters, not the rest of the library (for this reason I didn't want to change the value of file property of the Test object or add another property like relativeFile on the Test 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.

@Dinika Dinika marked this pull request as draft February 5, 2025 19:52
@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch from 167243a to 9455223 Compare February 13, 2025 13:38
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch from 9455223 to ddc88db Compare February 13, 2025 20:06
@Dinika Dinika changed the title DRAFT // Get test file path relative to config Enable reporters to show relative paths of tests Feb 20, 2025
@Dinika Dinika marked this pull request as ready for review February 20, 2025 15:40
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch from d52bc24 to 4a82dc6 Compare February 20, 2025 15:41
@Dinika Dinika changed the title Enable reporters to show relative paths of tests feat: Enable reporters to show relative paths of tests Feb 20, 2025
@Dinika Dinika changed the title feat: Enable reporters to show relative paths of tests feat: enable reporters to show relative paths of tests Feb 20, 2025
@Dinika
Copy link
Contributor Author

Dinika commented Mar 8, 2025

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! 😊

@JoshuaKGoldberg
Copy link
Member

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.

@JoshuaKGoldberg
Copy link
Member

Resolves #5289

PR Checklist

  • Addresses an existing open issue: fixes #000

P.S. the fixes #000 is where we typically put a link to the issue 🙂. https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue - I went ahead and edited it in just now.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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.

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP or other posters - more information needed label Mar 10, 2025
@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch from 8ab406b to e4ce28b Compare March 11, 2025 16:20
@Dinika Dinika requested a review from JoshuaKGoldberg March 11, 2025 16:33
@JoshuaKGoldberg JoshuaKGoldberg removed the status: waiting for author waiting on response from OP or other posters - more information needed label Mar 12, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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.

@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch 2 times, most recently from 388084e to 59de078 Compare March 13, 2025 07:43
Signed-off-by: Dinika Saxena <dinika@greyllama.cc>
@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch from 59de078 to 8c545b7 Compare March 13, 2025 07:43
Copy link
Member

@mark-wiemer mark-wiemer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@Dinika Dinika force-pushed the feat-5289/show-relative-filepath-in-reporters branch from 60b7640 to 8898907 Compare March 16, 2025 22:51
@@ -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`.
Copy link
Member

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

@JoshuaKGoldberg JoshuaKGoldberg merged commit 81ea666 into mochajs:main Mar 17, 2025
75 checks passed
@voxpelli voxpelli requested a review from Copilot June 12, 2025 07:57
Copy link

@Copilot Copilot AI left a 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’s test 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')), since testFilePath uses path.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');
Copy link
Preview

Copilot AI Jun 12, 2025

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.

Suggested change
const relativeTestPath = path.join('tests', 'demo-test.spec.js');
const relativeTestPath = path.relative(process.cwd(), absoluteTestPath);

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Feature: allow using test file's relative path in xunit reporter output
3 participants