Created
July 26, 2022 20:13
-
-
Save aarongodin/7c438650a75fa95adaeaa6fc8b97367e to your computer and use it in GitHub Desktop.
Recursive partial object matching with Jest expectations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import expect from "expect" | |
import lodash from "lodash" | |
function objectToNestedExpectations(input: any) { | |
const target = {} as any | |
for (const [key, value] of Object.entries(input)) { | |
if (lodash.isPlainObject(value)) { | |
target[key] = objectToNestedExpectations(value) | |
} else { | |
target[key] = value | |
} | |
} | |
return expect.objectContaining(target) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment