Skip to content

Strange equals comparison testing jest mock calls - workaround incl #445

@sebastianrothbucher

Description

@sebastianrothbucher

Per se, unexpected is great for (amongst other things) comparing more complex structures - including the calls to a mock.

There is a glitch, however: The following line fails w/ a somewhat strange error message:

expect(resp.status.mock.calls, 'to equal', [[500]]); // one call, w/ 500 as one and only arg

msg is:

    UnexpectedError:
    expected [ [ 500 ] ] to equal [ [ 500 ] ]

    Mismatching constructors Array should be Array

What does work is this:

const normalizeArray=(obj) => (Array.isArray(obj) ? [].concat(obj).map(c => normalizeArray(c)): obj); // hopefully an utils.js ;-)
expect(normalizeArray(resp.status.mock.calls), 'to equal', [[500]]);

I didn't yet figure out what the root cause is. And there's a workaround, so it's not really critical.

Just wanted to let you know about the workaround (and I'm sure grateful if s/o can point me to an existing solution).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions