GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — deepStrictEqual comparison output is unhelpful on prototype-only differences · Issue #22141 · nodejs/node

deepStrictEqual comparison output is unhelpful on prototype-only differences #22141

New issue
Copy link
New issue
Copy link
Closed
Closed
deepStrictEqual comparison output is unhelpful on prototype-only differences#22141
Copy link
Labels
assertIssues and PRs related to the assert subsystem.confirmed-bugIssues and PRs for confirmed bugs.

Description

@damianobarbati
damianobarbati
opened on Aug 5, 2018
Issue body actions
$ node -v
v10.8.0
$ node test.js
//test.js
const assert = require('assert').strict;

const DEBUG = process.env.DEBUG_ROUTER;

const pathnameToMatches = (pathname, template) => {
    DEBUG && console.log(pathname, '~', template);

    const regexp = templateToRegexp(template);
    const matches = pathname.match(regexp);

    DEBUG && console.log(pathname, '~', regexp, '=>', !matches ? false : (matches.groups || {}));

    if (!matches)
        return false;

    const result = matches.groups || {};
    for (const key in result)
        result[key] = result[key] || null;
    return result;
};

const templateToRegexp = template => {
    let pattern = template;
    DEBUG && console.log('processing =>', template);
    pattern = pattern.replace(/\/:(?<param>[\w\d]+?)(?=\/|$)/gi, '\\/(?<$<param>>[\\w\\d]+?)(?=\\/|$)');
    DEBUG && console.log('after required =>', pattern);
    pattern = pattern.replace(/\/:(?<param>[\w\d]+?)\?/gi, '(\\/(?<=\\/)(?<$<param>>[\\w\\d]+?)(?=\\/|$))?');
    DEBUG && console.log('after optional =>', pattern);
    pattern = '^' + pattern + '$';
    DEBUG && console.log('after boundaries =>', pattern);
    const regexp = new RegExp(pattern, 'i');
    DEBUG && console.log(template, '=>', pattern, '=>', regexp);
    return regexp;
};

try {
    assert.deepStrictEqual(pathnameToMatches('/user', '/user'), {});
    assert.deepStrictEqual(pathnameToMatches('/not-user', '/user'), false);
    assert.deepStrictEqual(pathnameToMatches('/user/123', '/user/:id'), { id: '123' });
    assert.deepStrictEqual(pathnameToMatches('/user/123/456', '/user/:id'), false);
    assert.deepStrictEqual(pathnameToMatches('/user', '/user/:id'), false);
    assert.deepStrictEqual(pathnameToMatches('/user', '/user/:id?'), { id: null });
    assert.deepStrictEqual(pathnameToMatches('/user/123', '/user/:id?'), { id: '123' });
    assert.deepStrictEqual(pathnameToMatches('/user/123/male', '/user/:id/:sex'), { id: '123', sex: 'male' });
    assert.deepStrictEqual(pathnameToMatches('/user/123', '/user/:id/:sex?'), { id: '123', sex: null });
    assert.deepStrictEqual(pathnameToMatches('/user/123', '/user/:id/:sex'), false);
    assert.deepStrictEqual(pathnameToMatches('/user/123', '/user/:id?/:sex?'), { id: '123', sex: null });
    assert.deepStrictEqual(pathnameToMatches('/user/123/female', '/user/:id?/:sex?'), { id: '123', sex: 'female' });
    assert.deepStrictEqual(pathnameToMatches('/user', '/user/:id?/:sex?'), { id: null, sex: null });

    console.log('🎉🎉🎉');
}
catch (error) {
    console.error(error);//.message, { actual: error.actual, expected: error.expected });
}

Throws a misterious:

{ AssertionError [ERR_ASSERTION]: Input objects not identical:

{
  id: '123'
}

    at Object.<anonymous> (/Users/damz/Desktop/react-router-xs/specs/pathnameToMatches.spec.js:41:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
  generatedMessage: true,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: { id: '123' },
  expected: { id: '123' },
  operator: 'deepStrictEqual' }

What does this mean? Where's the difference? 🤨

  actual: { id: '123' },
  expected: { id: '123' },
Reacted by Bap

Activity

Next
  1. ChALkeR
    added
    assertIssues and PRs related to the assert subsystem.
    on Aug 5, 2018
  2. ChALkeR commented on Aug 5, 2018

    @ChALkeR
    ChALkeR
    on Aug 5, 2018
    Member
    More actions

    @damianobarbati in your test, their prototypes differ. The object that is returned from the regexp in matches.groups does not have a prototype, { id: '123' } does.

    A simplified version of your testcase:

    const assert = require('assert').strict;
    
    function split(pathname) {
      const regexp = new RegExp("^/(?<id>\\d+?)$", 'i');
      const matches = pathname.match(regexp);
      return matches.groups;
    }
    
    try {
      assert.deepStrictEqual(
        split('/123', '/:id'),
        { id: '123' },
        'with prototype'
      );
    } catch (e) {
      console.error(e);
    }
    
    try {
      assert.deepStrictEqual(
        split('/123', '/:id'),
        Object.assign(Object.create(null), { id: '123' }),
        'without prototype'
      );
    } catch (e) {
      console.error(e);
    }

    Note that only the first of those two tests fails.

  3. ChALkeR
    added
    questionIssues asking questions about Node.js.
    on Aug 5, 2018
  4. ChALkeR commented on Aug 5, 2018

    @ChALkeR
    ChALkeR
    on Aug 5, 2018
    Member
    More actions

    That is documented: https://nodejs.org/api/assert.html#assert_comparison_details

    Moreover, that is what the main difference between assert.deepStrictEqual/assert.strict.deepEqual and the legacy deprecated assert.deepEqual is.

  5. ChALkeR
    closed this as completedon Aug 5, 2018
  6. ChALkeR
    added
    invalidIssues and PRs that are invalid.
    on Aug 5, 2018
  7. ChALkeR commented on Aug 5, 2018

    @ChALkeR
    ChALkeR
    on Aug 5, 2018
    Member
    More actions

    Replacing return result; with return Object.assign({}, result); fixes your tests.

  8. damianobarbati commented on Aug 5, 2018

    @damianobarbati
    damianobarbati
    on Aug 5, 2018
    Author
    More actions

    Oh I see now, groups has a prototype attached!

    The 2 misleading factors:

    • docs stating Only enumerable "own" properties are considered. but here prototype is considered for matching
    • useless actual/expected messages in error

    Thanks @ChALkeR!

  9. ChALkeR commented on Aug 5, 2018

    @ChALkeR
    ChALkeR
    on Aug 5, 2018
    Member
    More actions

    @damianobarbati No, matches.groups doesn't have a prototype (has null), { id: '123' } does.
    assert.strict.deepEqual(Object.create(null), {}) throws because the prototypes are not equal.

  10. ChALkeR commented on Aug 5, 2018

    @ChALkeR
    ChALkeR
    on Aug 5, 2018
    Member
    More actions
    $ node
    > const x = {}; x.id = '123'; x
    { id: '123' }
    > const y = Object.create(null); y.id = '123'; y
    {}
    > assert.strict.deepEqual(x, y)
    AssertionError [ERR_ASSERTION]: Input objects not identical:
    
    {}
    
    > Object.getPrototypeOf(x)
    {}
    > Object.getPrototypeOf(y)
    null
    > x.toString()
    '[object Object]'
    > y.toString()
    TypeError: y.toString is not a function

    matches.groups also wouldn't have a .toString() method, but { id: '123' } would, because its prototype has it.

  11. BridgeAR commented on Aug 5, 2018

    @BridgeAR
    BridgeAR
    on Aug 5, 2018
    Member
    More actions

    As far as I understand the main issue it is not about the comparison details but about the output and that is indeed not ideal. The main difficulty here is how to visualize the difference between such objects. I struggled with this as well and I would like to fix this. I just could not yet come up with a great solution so far.

    I am going to look into improving that though.

    Reacted by Bap and Tadeáš Valenta
  12. BridgeAR
    reopened this on Aug 5, 2018
  13. BridgeAR
    added
    confirmed-bugIssues and PRs for confirmed bugs.
    and removed
    invalidIssues and PRs that are invalid.
    questionIssues asking questions about Node.js.
    on Aug 5, 2018
  14. 2 remaining items

  15. ChALkeR commented on Aug 6, 2018

    @ChALkeR
    ChALkeR
    on Aug 6, 2018
    Member
    More actions

    @BridgeAR

    As far as I understand the main issue it is not about the comparison details but about the output and that is indeed not ideal.

    Ok, that makes sense. I renamed the issue then.

  16. antsmartian commented on Aug 13, 2018

    @antsmartian
    antsmartian
    on Aug 13, 2018
    Contributor
    More actions

    Hey @BridgeAR @ChALkeR , thought of giving a crack at this. I was seeing through the source code, and I found the issue is we are not showing up the error message properly when two objects are compared and one of those object's prototype is null.

    The fix should be straight forward , if we have a object whose prototype is null, we need to build up the braces object so that it can catch up the data about prototype being null(so that we can use these data to print relevant information on failure). But definitely we need to come up with some text that says it has empty prototype (I had kept it as proto-null while I was building locally). I did those changes and I ran the example given by the OP and got the below message:

    { AssertionError [ERR_ASSERTION]: Expected inputs to be strictly deep-equal:
    + actual - expected
    
    + proto-null {
    - {
        id: '123'
      }
        at Object.<anonymous> (/Users/anto/programs/node/node-hack/22141.js:83:12)
        at Module._compile (internal/modules/cjs/loader.js:689:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
        at Module.load (internal/modules/cjs/loader.js:599:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
        at Function.Module._load (internal/modules/cjs/loader.js:530:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
        at startup (internal/bootstrap/node.js:266:19)
        at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
      generatedMessage: true,
      name: 'AssertionError [ERR_ASSERTION]',
      code: 'ERR_ASSERTION',
      actual: proto-null { id: '123' },
      expected: { id: '123' },
      operator: 'deepStrictEqual' }
    

    The actual,expected looked:

      actual: proto-null { id: '123' },
      expected: { id: '123' },
    

    which seems good to me -- clearly indicates prototype is not equal. Well, of course, if we do these changes, we need to work on utils.inspect test case as well (on the text that we might add), ex : https://github.com/nodejs/node/blob/master/test/parallel/test-util-inspect.js#L256

    Let me know your thoughts on this. Thanks!

  17. BridgeAR commented on Aug 13, 2018

    @BridgeAR
    BridgeAR
    on Aug 13, 2018
    Member
    More actions

    @antsmartian I think it is actually a good idea to change the output in util.inspect() to visualize the null prototype. My first thought was to only fix this in assert but it should be useful in general to know if the entry has a prototype or not.

    If the inspect output provides the necessary information the output is automatically fixed for assert. The question is how it should be visualized. proto-null seems sub-par. [Object: null prototype] would work. The same as e.g., [Promise: null prototype].

  18. antsmartian commented on Aug 13, 2018

    @antsmartian
    antsmartian
    on Aug 13, 2018
    Contributor
    More actions

    @BridgeAR

    If the inspect output provides the necessary information the output is automatically fixed for assert.

    Yes, as I have shown above.

    The question is how it should be visualized. proto-null seems sub-par.[Object: null prototype] would work. The same as e.g., [Promise: null prototype].

    Yes, using [Object: null prototype] should be good enough as we will get the following assert error:

    actual: [Object: null prototype] { id: '123' },
    expected: { id: '123' },
    

    I will work on this and raise a PR.

    Note: You have actually mentioned the wrong person in your reply , instead of me ;)

  19. BridgeAR
    added a commit that references this issue on Oct 2, 2018

    util: handle null prototype on inspect

    f4e4ef5
  20. BridgeAR
    closed this as completedin f4e4ef5on Oct 2, 2018
  21. antsmartian
    added a commit that references this issue on Oct 30, 2018

    util: handle null prototype on inspect

    a39f341
  22. MylesBorins
    added a commit that references this issue on Nov 12, 2018

    util: handle null prototype on inspect

    3df083c
  23. rvagg
    added a commit that references this issue on Nov 28, 2018

    util: handle null prototype on inspect

    ef72b01
  24. MylesBorins
    added a commit that references this issue on Nov 29, 2018

    util: handle null prototype on inspect

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.confirmed-bugIssues and PRs for confirmed bugs.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.