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

Add support for test logging #1452

Merged
merged 6 commits into from
Aug 5, 2017
Merged

Add support for test logging #1452

merged 6 commits into from
Aug 5, 2017

Conversation

nowells
Copy link
Contributor

@nowells nowells commented Jul 13, 2017

There are times when you wish to include comments in your tests that will be shown in context of the test result and not streamed to stdout like console.log statements are, where they will likely be out of order of the tests they are associated with.

I will happily add tests, and change the API, etc. I just want to get general 👍 or 👎 to the concept of adding comments before I flesh out the tests and other cleanup.

One of the main use cases I have for this, is I am using ava as a runner for my SauceLabs/BrowserSrack/Selenium tests, and I would like to have the link to the job results from SauceLabs/BrowserStack be output in context with the test run. There are certainly many other usecases for this, however, just to give a relevant example of when this is useful.

Example output

--verbose

1__tmux_and_zoom_meeting_id__571-235-5176

--tap

TAP version 13                                                                                                                                                              [30/1823]
# file-one › passing test
ok 1 - file-one › passing test
  a comment for passing test
# file-two › passing test
ok 2 - file-two › passing test
  a comment for passing test
# file-one › failing test
not ok 3 - file-one › failing test
  ---
    name: AssertionError
    assertion: is
    values:
      'Difference:': |-
        - true
        + false
    at: 'Test.t [as fn] (file-one.test.js:11:7)'
  ...
  a comment for failing test
# file-two › failing test
not ok 4 - file-two › failing test
  ---
    name: AssertionError
    assertion: is
    values:
      'Difference:': |-
        - true
        + false
    at: 'Test.t [as fn] (file-two.test.js:11:7)'
  ...
  a comment for failing test

1..4
# tests 4
# pass 2
# fail 2

if (test.error) {
return ' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message);
Copy link
Contributor Author

@nowells nowells Jul 13, 2017

Choose a reason for hiding this comment

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

This whole stream of changes looks more noisy than it is. This is simply switching from an immediate return approach to appending to a lines.push approach so that we can concatinate comments after the test summary.

@novemberborn
Copy link
Member

Looks like you've inadvertently implemented #420! 👍

This looks great. I think it should work with the (default) mini reporter too, since that still prints failing tests.

Bike shedding consensus from #420 was to use t.log() rather than t.comment().

@nowells
Copy link
Contributor Author

nowells commented Jul 20, 2017

@novemberborn thanks for the review! I will:

  • Switch from comment to log
  • Add log output to mini reporter on failure
  • Add tests for t.log

I should hopefully have this up to you for review by the end of the day. Cheers!

@nowells
Copy link
Contributor Author

nowells commented Jul 20, 2017

@novemberborn let me know if my rework looks good, and if you think there are other types of tests that I should add. Thanks!

@sindresorhus sindresorhus changed the title Add support for test comments. Add support for test logging Jul 25, 2017
@sindresorhus
Copy link
Member

@nowells Can you document it in the readme?

@sindresorhus
Copy link
Member

I think it should work with the (default) mini reporter too, since that still prints failing tests.

How would that work? The comments would need context.

@nowells
Copy link
Contributor Author

nowells commented Jul 25, 2017

Can you document it in the readme?

Absolutely! Done.

How would that work? The comments would need context.

The way I implemented it, in mini reporter the log messages show up in failure cases (arguably the only place you really care to see them in that context) but in success cases I do not display them.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

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

This is almost there @nowells. Awesome work!

const appendLogs = () => {
if (test.logs) {
test.logs.forEach(log => {
output.push(` ${log}`);
Copy link
Member

Choose a reason for hiding this comment

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

If log contains linebreaks, should the indentation apply on those new lines? If so, this should use https://www.npmjs.com/package/indent-string (like elsewhere in the reporters). May not be a bad idea to use that anyway.

Copy link
Member

Choose a reason for hiding this comment

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

should the indentation apply on those new lines?

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I'm going to make it so that it will result in something like this, where I add the i figure to the first line.

  ✔ file-one › passing test
    ℹ a comment for passing test
      with a newline
    ℹ a comment for passing test without a newline


if (test.logs) {
test.logs.forEach(log => {
output += ' ' + colors.information(figures.info) + ' ' + colors.log(log) + '\n';
Copy link
Member

Choose a reason for hiding this comment

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

Same question here around linebreaks in the log value.

readme.md Outdated
@@ -875,6 +875,10 @@ Plan how many assertion there are in the test. The test will fail if the actual

End the test. Only works with `test.cb()`.

###### `t.log(message)`

Print a log message contextually alongside the test result instead of immediately streaming the message to stdout like `console.log`.
Copy link
Member

Choose a reason for hiding this comment

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

Along the lines of https://nodejs.org/api/console.html#console_console_log_data_args, perhaps:

Print a log message contextually alongside the test result instead of immediately printing it to stdout like console.log.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

nowells added 4 commits July 25, 2017 23:09
There are times when you wish to include comments in your
tests that will be shown in context of the test result
and not streamed to stdout like console.log statements are.
* [x] Switched from `t.comment` to `t.log`
* [x] Added mini reporter comments on failure.
* [x] Added tests.
Added indentString support.
Fixed up readme.
@nowells
Copy link
Contributor Author

nowells commented Jul 26, 2017

The tests that are failing appear to be failing on master as well?

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

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

@nowells sorry for the delay. This looks good, though I think you should avoid rewriting the indent-string output. Luckily the indent value can be customized, so with some creative use of its API we can avoid the rewriting.

const logLinesWithLeadingFigure = logLines.replace(
/^ {6}/,
` ${colors.information(figures.info)} `
);
Copy link
Member

Choose a reason for hiding this comment

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

This might be better as:

const logLines = indentString(colors.log(log), 1, {
  indent: `    ${colors.information(figures.info)} `,
  includeEmptyLines: true
});

(Though that indent value should probably be a constant, but feel free to redeclare it in the verbose logger.)

See indent-string's API. Including empty lines would be good given the figure prefix.

You'll have to update the indent-string dependency to 3.2.0 since the includeEmptyLines feature is pretty new.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@novemberborn the reason I didn't do that was I only wanted to i symbol to show up once per log line (to indicate where a log message starts)

    * this is a message that will have multiple lines, but there only the first line
      will have the `*` to indicate a log message, and the indent of the other lines
      is at the indent level of the first log message (after the `*`)
    * Some other log message

Does that make sense? Or would you rather it be

    * this is a message that will have multiple lines, but there only the first line
    * will have the `*` to indicate a log message, and the indent of the other lines
    * is at the indent level of the first log message (after the `*`)
    * Some other log message, but how do I know that.

Copy link
Member

Choose a reason for hiding this comment

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

Oh! No that's fine, I misread the code.

I suppose the replacement string could be a constant, but on the other hand it's clearer if it's near the indentation values. So hey :)

@novemberborn
Copy link
Member

The tests that are failing appear to be failing on master as well?

I'm hoping #1477 fixes that.

@nowells
Copy link
Contributor Author

nowells commented Jul 31, 2017

thanks for the great feedback and guiding me through this @novemberborn!

@novemberborn novemberborn merged commit 14f7095 into avajs:master Aug 5, 2017
@sindresorhus
Copy link
Member

Thanks for working on this @nowells 🙌

@nowells
Copy link
Contributor Author

nowells commented Aug 5, 2017

@sindresorhus thanks for making such a wonderful testing system!!! I look forward to helping out on other features as well. ❤️

kevva pushed a commit that referenced this pull request Sep 13, 2017
There are times when you wish to include comments in your tests that will be shown in the context of the test result, and not streamed to `stdout` like `console.log` statements are.

Fixes #420.
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.

3 participants