Skip to content

Commit 641d316

Browse files
authored
Adds option for recursively allowing additional props (#1)
1 parent cdfc27f commit 641d316

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/deep-equals.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ interface ErrorWithPath extends Error {
88
}
99

1010
export interface Options {
11-
strict?: boolean
11+
strict?: boolean,
12+
allowAdditionalProps?: boolean,
1213
}
1314

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

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

9899
if (expected && expected[$any]) {
99100
if (typeof expected === "object" && Object.keys(expected).length > 0) {

test/functional.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,15 @@ test("can compare recursive data structures", t => {
9999
foo: assert.satisfies(foo => assert.deepEquals(foo, { parent: foo }))
100100
}))
101101
})
102+
103+
test("can match on subsets", t => {
104+
assert.deepEquals({foo: {bar: "baz", extra: true}, extra: true}, {foo: {bar: "baz"}}, {allowAdditionalProps: true})
105+
t.pass()
106+
})
107+
108+
test("subsets only match when option is passed", t => {
109+
t.throws(() => assert.deepEquals(
110+
{foo: {bar: "baz", extra: true}, extra: true},
111+
{foo: {bar: "baz"}}
112+
))
113+
})

0 commit comments

Comments
 (0)