Skip to content

assert.any for assert.deepEqual to match on types instead of values #55319

Open
@aldipower

Description

What is the problem this feature will solve?

Given for example a Date.now in a nested object I would like be able to run deepEqual on the object.
Such situation often happens if you are testing responses from APIs or something similar.

This obviously will throw an AssertionError:

        assert.deepEqual({
            a: 1,
            b: Date.now()
        }, {
            a: 1,
            b: 1728380577053,
        });

What is the feature you are proposing to solve the problem?

One option would be to introduce an asymmetric matcher assert.any that could dynamically check on the given type in the expected object.
Checking the type is often enough in such situation.

This would pass:

        assert.deepEqual({
            a: 1,
            b: Date.now()
        }, {
            a: 1,
            b: assert.any(Number),
        });

Another option would be to let you define custom asymmetric matchers:

        assert.deepEqual({
            a: 1,
            b: Date.now()
        }, {
            a: 1,
            b: assert.asymetricMatcher((v) => typeof v === "Number"),
        });

What alternatives have you considered?

I tried to use another assertion library which are capable of doing this: Vitest, unexpected.js, Jest
But this gives mangled output and I am not satisfied to bring in such big libraries for the sake of only doing asymmetric matching. Also I explain my tests in a documentation for other developers and I would like to use the Node built-in tools for simplicity.

Activity

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

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions