Function assertObjectMatch

  • Make an assertion that expected object is a subset of actual object, deeply. If not, then throw a diff of the objects, with mismatching properties highlighted.

    Parameters

    • actual: Record<PropertyKey, any>

      The actual value to be matched.

    • expected: Record<PropertyKey, unknown>

      The expected value to match.

    • Optionalmsg: string

      The optional message to display if the assertion fails.

    Returns void

    import { assertObjectMatch } from "@std/assert";

    assertObjectMatch({ foo: "bar" }, { foo: "bar" }); // Doesn't throw
    assertObjectMatch({ foo: "bar" }, { foo: "baz" }); // Throws
    assertObjectMatch({ foo: 1, bar: 2 }, { foo: 1 }); // Doesn't throw
    assertObjectMatch({ foo: 1 }, { foo: 1, bar: 2 }); // Throws
    import { assertObjectMatch } from "@std/assert";

    assertObjectMatch({ foo: { bar: 3, baz: 4 } }, { foo: { bar: 3 } }); // Doesn't throw
    assertObjectMatch({ foo: { bar: 3 } }, { foo: { bar: 3, baz: 4 } }); // Throws