Skip to content
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

test_runner: add level-based diagnostic handling for reporter #55957

Closed
wants to merge 4 commits into from

Conversation

hpatel292-seneca
Copy link

This fixes #55922

Change summary

Updated the reporter.diagnostic to accept level parameter like this

  diagnostic(nesting, loc, message, level = 'info') {
    this[kEmitMessage]('test:diagnostic', {
      __proto__: null,
      nesting,
      message,
      level,
      ...loc,
    });
  }

Then I updated #handleEvent like this

 #handleEvent({ type, data }) {
    switch (type) {
      case 'test:fail':
        if (data.details?.error?.failureType !== kSubtestsFailed) {
          ArrayPrototypePush(this.#failedTests, data);
        }
        return this.#handleTestReportEvent(type, data);
      case 'test:pass':
        return this.#handleTestReportEvent(type, data);
      case 'test:start':
        ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
        break;
      case 'test:stderr':
      case 'test:stdout':
        return data.message;
      case 'test:diagnostic':  // Here I added logic
        const diagnosticColor =
          reporterColorMap[data.level] || reporterColorMap['test:diagnostic'];
        return `${diagnosticColor}${indent(data.nesting)}${
          reporterUnicodeSymbolMap[type]
        }${data.message}${colors.white}\n`;
      case 'test:coverage':
        return getCoverageReport(
          indent(data.nesting),
          data.summary,
          reporterUnicodeSymbolMap['test:coverage'],
          colors.blue,
          true
        );
    }
  }

And I am Updated reporterColorMap like this

const reporterColorMap = {
  __proto__: null,
  get 'test:fail'() {
    return colors.red;
  },
  get 'test:pass'() {
    return colors.green;
  },
  get 'test:diagnostic'() {
    return colors.blue;
  },
  get info() {
    return colors.blue;
  },
  get debug() {
    return colors.gray;
  },
  get warn() {
    return colors.yellow;
  },
  get error() {
    return colors.red;
  },
};

and color already contain logic for this colors

I also set the reporter.diagnostic call from test.js like this (level="Error")

if (actual < threshold) {
            harness.success = false;
            process.exitCode = kGenericUserError;
            reporter.diagnostic(
              nesting,
              loc,
              `Error: ${NumberPrototypeToFixed(
                actual,
                2
              )}% ${name} coverage does not meet threshold of ${threshold}%.`,
              'error'  // Level is set to error for red color
            );
          }

Here is Demo output:
image

Added a  parameter to  to allow
severity-based formatting for diagnostic messages. Defaults to 'info'.

This update enables better control over message presentation
(e.g., coloring) based on severity levels such as 'info', 'warn',
and 'error'.

Refs: nodejs#55922
Updated  to process the  parameter for
 events. Messages are now formatted with
colors based on the  (e.g., 'info', 'warn',
'error').

This change ensures diagnostic messages are visually distinct,
improving clarity and reducing debugging effort during test runs.

Refs: nodejs#55922
Enhanced  to include colors for the following
diagnostic levels:
 : blue
 : gray
 : yellow
 : red

Ensures consistency in color-coding across the reporter.

Refs: nodejs#55922
Updated coverage threshold checks in  to use the
parameter when calling . Errors now use the
'error' level for red-colored formatting.

This ensures coverage errors are highlighted effectively in the output.

Fixes: nodejs#55922
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Nov 22, 2024
@hpatel292-seneca
Copy link
Author

Hi @pmarchini, Here is the PR and the demo of it I tested locally. I am not sure where to add test case for the change, I read this https://github.com/nodejs/node/blob/main/doc/contributing/writing-tests.md but I am still not sure and I tried searching existing test case for

case 'test:diagnostic':
to look for reference but I cant find it.

@hpatel292-seneca
Copy link
Author

And I just noticed that my vscode formatter formatted the files I worked with. Do you want that or you want me to revert it back?

@pmarchini
Copy link
Member

And I just noticed that my vscode formatter formatted the files I worked with. Do you want that or you want me to revert it back?

Hey @hpatel292-seneca, about formatting absolutely, only changes strictly related to the issue must be present in the PR, also make sure you run linting via make commands.

Also, have you run the tests? I would expect these changes to potentially break some existing tests

@hpatel292-seneca
Copy link
Author

Hi @pmarchini, Run tests and I got few tests failing which I tried to look at but not able to understand exact error, but I tried building Node based on main branch without my changes and tried running test cases and it is also failing same test cases.

Here is screenshot of tests failing on main
image

And here is tests failing on my issue branch
image

All the tests which are failing are same. Maybe my pc is missing some configuration!

@pmarchini
Copy link
Member

I'm closing this PR as it duplicates:
#55964

Please avoid opening duplicate PRs 😊

@pmarchini pmarchini closed this Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a level parameter to test runner diagnostics
3 participants