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

Crashes with an unmeaningful error when payloads have different structure #22

Closed
lolmaus opened this issue Jun 8, 2023 · 2 comments
Closed

Comments

@lolmaus
Copy link

lolmaus commented Jun 8, 2023

import match from 'match-json';

const expected = {
          insights: [
            {
              label: 'completed',
              type: 'counter',
              data: {
                value: 2,
              },
            },
            {
              label: 'pending',
              type: 'counter',
              data: {
                value: 0,
              },
            },
            {
              label: 'reversed',
              type: 'counter',
              data: {
                value: 0,
              },
            },
            {
              label: 'declined',
              type: 'counter',
              data: {
                value: 0,
              },
            },
          ],
        };

const actual = {
          insights: [
            {
              label: 'completed',
              type: 'counter',
              data: {
                value: 2,
              },
            },
            {
              label: 'pending',
              type: 'counter',
              data: {
                amount: {
                  value: 0,
                },
              },
            },
            {
              label: 'reversed',
              type: 'counter',
              data: {
                value: 0,
              },
            },
            {
              label: 'declined',
              type: 'counter',
              data: {
                value: 0,
              },
            },
          ],
        }

match(expected, actual);

This code crashes with:

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at compareObjects (match.js:63:30)
    at matchCore (match.js:29:14)
    at eval (match.js:64:45)
    at Array.every (<anonymous>)
    at compareObjects (match.js:64:32)
    at matchCore (match.js:29:14)
    at eval (match.js:64:45)
    at Array.every (<anonymous>)
    at compareObjects (match.js:64:32)

It takes a lot of effort debugging via Pause on uncaught assertions in order to find out, that the usage of the library was legit and the payloads were simply not identical at insights[1].data.

Should instead provide a meaningful error message, letting you know which key is different.

@ozkxr
Copy link
Owner

ozkxr commented Jun 18, 2023

Sorry for the late response. I was off guard. I'm now looking into it.

@ozkxr ozkxr closed this as completed in 4da5785 Jun 19, 2023
@ozkxr
Copy link
Owner

ozkxr commented Jun 19, 2023

Thanks for the heads up. It should be fixed in version 1.3.6 which is now live on npm.

Just one thing: the actual value is the first argument and the expected one is the second.

It should be: match(actual, expected).

But the REAL problem was match were just checking for the length of the keys array in objects and assuming the keys were the same if the array length was the same.

I expect now it works as intended. If not, please don't hesitate in filing another issue. Your feedback is really appreciated.

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

No branches or pull requests

2 participants