Skip to content

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

Description

@damianobarbati
$ 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' },

Activity

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