Skip to content

Provide options for assert.strictEqual to display full strings #19106

Description

@joyeecheung

When calling assert.strictEqual() on long strings (>128 chars) they will be cropped, and if the first different character appear after the 128th character, all we have is '128 chars of common prefix... strictEqual '128 chars of common prefix... which is not really debuggable. This is very common when comparing error messages and file contents. Maybe we can provide an environment variable to toggle the output to

'long string'

strictEqual

'long string'

So one does not have to alter the code to get the difference of such long strings.

Activity

  1. added
    assertIssues and PRs related to the assert subsystem.
    on Mar 3, 2018
  2. joyeecheung commented on Mar 3, 2018

    @joyeecheung
    MemberAuthor
  3. self-assigned this
    on Mar 3, 2018
  4. BridgeAR commented on Mar 3, 2018

    @BridgeAR
    Member

    We are on the same page that it is bad if the user can not see the difference. Providing a environment variable does not seem the best way to handle this though.

    I thought about adding options to assert.deepStrictEqual as well at some point but having solid defaults is still best for now out of my perspective. The output also differs depending on using the strict mode or legacy mode. The strict mode has a much better output already but it could still be improved.

    Specifically about limiting the output: I suggest to make it a bit smarter by checking where the input differs and to show the string from there on. As in:

    const a = `${'a'.repeat(200)}bbb${'a'.repeat(100)}bbbaaaaa`
    const b = `${'a'.repeat(200)}ccc${'a'.repeat(100)}cccaaaaa`
    assert.strictEqual(a, b)
    // AssertionError [ERR_ASSERTION]: Input A expected to strictEqual input B:
    // + expected - actual ...[n]... identical characters skipped
    //
    //  - '[197]...aaabbbaaa...[94]...aaabbbaaaaa'
    //  + '[197]...aaacccaaa...[94]...aaacccaaaaa'
    //
    //  StackTrace

    Input is much appreciated about making the output even clearer. I would only skip characters if there are more than 10 identical ones in a row.

  5. BridgeAR commented on Mar 3, 2018

    @BridgeAR
    Member

    I still believe we should limit the total output with my suggestion but we can raise that limit to e.g. 256 characters in total. Thoughts?

  6. BridgeAR commented on Mar 3, 2018

    @BridgeAR
    Member

    The downside of this is of course that theoretically the input could also look like this. I am still trying to figure out a way to visualize the difference between such cases. One way is to show colors but that will only work on a TTY that supports it and we can not always expect that.

  7. BridgeAR commented on Mar 3, 2018

    @BridgeAR
    Member
    const a = `${'a'.repeat(200)}bbb${'a'.repeat(100)}bbb[...]aaaaa`
    const b = `${'a'.repeat(200)}ccc${'a'.repeat(100)}ccc[...]aaaaa`
    assert.strictEqual(a, b)
    // AssertionError [ERR_ASSERTION]: Input A expected to strictEqual input B:
    // + expected - actual [...] identical characters skipped with the number below 
    //
    // - '[...]aaabbbaaa[...]aaabbb[...]aaaaa'
    // + '[...]aaacccaaa[...]aaaccc[...]aaaaa'
    //    ^ 197         ^ 94
    // 
    //  StackTrace

    This might be a better alternative?

  8. joyeecheung commented on Mar 3, 2018

    @joyeecheung
    MemberAuthor

    We can always try our best in the default but it is important to have a way to dump everything out if we are not clever enough IMO. Otherwise whenever we have edge cases that do not work well we will have to go back to fix assert before getting anything debuggable.

  9. BridgeAR commented on Mar 3, 2018

    @BridgeAR
    Member

    @joyeecheung so right now the default in strict mode shows the whole line while legacy mode shows a limited output. I suggest to change the output of the legacy mode to be identical with the strict mode for the functions with strict in their name (the new output is incompatible with loose comparison).

    On top of that I would like to change the default for one line outputs to the one above while I am fine with showing the whole output as a default and adding a environment variable to limit the output or the other way around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

assertIssues and PRs related to the assert subsystem.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions