adamgruber / mochawesome

A Gorgeous HTML/CSS Reporter for Mocha.js
https://gitter.im/mochawesome/general
MIT License
1.06k stars 160 forks source link

Filename inheritance option #380

Open ittaibaratz opened 2 years ago

ittaibaratz commented 2 years ago

Is your feature request related to a problem? Please describe.

We noticed that for Cypress based reports, the spec filename does not show up as shown on your example: image

Our tests: image

As shown above, the spec name does not show up for Cypress. When I inspected the generated Mochawesome JSON, I found that the file and fullFile fields are populated at the very top level, but not in the levels below, i.e.:

  "results": [
    {
      "uuid": "c9b3f06c-e9f9-4630-bce6-2f67334403eb",
      "title": "",
      "fullFile": "cypress/integration/ajo/dashboards.spec.ts",
      "file": "cypress/integration/ajo/dashboards.spec.ts",
      "beforeHooks": [],
      "afterHooks": [],
      "tests": [],
      "suites": [
        {
          "uuid": "132ab838-79ba-4f2f-a6b1-86a96d848810",
          "title": "Dashboards",
          "fullFile": "",
          "file": "",
          "beforeHooks": [],
          "afterHooks": [],
          "tests": [
            {
              "title": "shows a welcome message",
              "fullTitle": "Dashboards shows a welcome message",
              "timedOut": null,
              "duration": 4367,

To resolve, I've written the following workaround using recursive code in Cypress: (Not great, but works well)

    Cypress.on('test:before:run', (test: Cypress.ObjectLike, runnable: Mocha.Test) => {
      const getFile = (runner: Suite): string|undefined => {
        if (runner.file) {
          return runner.file;
        }

        if (runner.parent) {
          return getFile(runner.parent);
        }
        return undefined;
      };

      if (runnable.parent && !runnable.parent.file) {
        runnable.parent.file = getFile(runnable.parent);
      }
    });

image

Describe the solution you'd like

First, I am wondering if there's anything super obvious that I am missing here.

If there isn't, I wonder if it would make sense to add a configuration to mochawesome that allows inheriting the filename from the parent - Similar to what I did above, but when generating the JSON or HTML.

Describe alternatives you've considered

The alternative is the code I have above which works well. This is not urgent for me but I think it would be a nice addition for the Cypress community using Mochawesome. When testing 100s of specs, its extremely useful to have the spec filename in the report.

Additional context Add any other context or screenshots about the feature request here.