Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adds option for recursively allowing additional props #1

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/deep-equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ interface ErrorWithPath extends Error {
}

export interface Options {
strict?: boolean
strict?: boolean,
allowAdditionalProps?: boolean,
}

type InternalComparisonFunction = (actual: any) => true | Error
Expand Down Expand Up @@ -93,7 +94,7 @@ export function satisfies(compare: UserComparisonFunction): Comparison {

export function deepEquals(actual: any, expected: any, path: Array<string | number>, opts: Options = {}): true | Error {
let result: true | Error
let allowAdditionalProps: boolean = false
let allowAdditionalProps: boolean = opts.allowAdditionalProps === true

if (expected && expected[$any]) {
if (typeof expected === "object" && Object.keys(expected).length > 0) {
Expand Down
12 changes: 12 additions & 0 deletions test/functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ test("can compare recursive data structures", t => {
foo: assert.satisfies(foo => assert.deepEquals(foo, { parent: foo }))
}))
})

test("can match on subsets", t => {
assert.deepEquals({foo: {bar: "baz", extra: true}, extra: true}, {foo: {bar: "baz"}}, {allowAdditionalProps: true})
t.pass()
})

test("subsets only match when option is passed", t => {
t.throws(() => assert.deepEquals(
{foo: {bar: "baz", extra: true}, extra: true},
{foo: {bar: "baz"}}
))
})