Skip to content

Instantly share code, notes, and snippets.

@aarongodin
Created July 26, 2022 20:13
Show Gist options
  • Save aarongodin/7c438650a75fa95adaeaa6fc8b97367e to your computer and use it in GitHub Desktop.
Save aarongodin/7c438650a75fa95adaeaa6fc8b97367e to your computer and use it in GitHub Desktop.
Recursive partial object matching with Jest expectations
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