From 6abc8c3497c440dfac66336a50fd6f56efc38bac Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Sat, 8 Oct 2022 04:25:41 -0700 Subject: [PATCH] fix: handles function-object fixes #228 --- ts/createSatisfier.ts | 8 ++++++++ ts/satisfies.spec.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/ts/createSatisfier.ts b/ts/createSatisfier.ts index ef0cb10..143c3dd 100644 --- a/ts/createSatisfier.ts +++ b/ts/createSatisfier.ts @@ -85,12 +85,20 @@ function diff(expected: any, actual: any, path: Diff['path'] = [], _index?: numb } if (expectedType === 'function') { + if (Object.keys(expected).length > 0) { + const ro = diffObject(expected, actual, path, _index) + if (ro.length === 0) return ro + } const r = (expected as Predicate)(actual, path) if (r === true) return noDiff return r ? r : [{ path, expected, actual }] } // expected is an object + return diffObject(expected, actual, path, _index) +} + +function diffObject(expected: any, actual: any, path: Diff['path'] = [], _index?: number): Diff[] { if (actual === undefined || actual === null) { return [{ path, expected, actual }] } diff --git a/ts/satisfies.spec.ts b/ts/satisfies.spec.ts index 60abe05..d9a9c1b 100644 --- a/ts/satisfies.spec.ts +++ b/ts/satisfies.spec.ts @@ -39,6 +39,10 @@ describe('when actual is object', () => { test('deep expected property can be predicate', () => { expect(satisfies({ a: { b: 'foo' } }, { a: { b: v => v === 'foo' } })).toBe(true) }) + + it.only('works with function-object', () => { + expect(satisfies({ a: 1 }, Object.assign(() => { }, { a: 1 }))).toBe(true) + }) }) describe('when actual is array', () => {