Created
December 18, 2020 05:16
-
-
Save cdanielsen/a1db3c11621373e94123dbcd1f7b83d1 to your computer and use it in GitHub Desktop.
ts-custom-matcher
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
export {}; | |
declare global { | |
// eslint-disable-next-line @typescript-eslint/no-namespace | |
namespace jest { | |
interface Matchers<R> { | |
toBeMemberOfEnum(): R; | |
} | |
} | |
} | |
expect.extend({ | |
toBeMemberOfEnum(received, enumeration) { | |
const enumValues = Object.keys(enumeration).map(k => enumeration[k]) | |
const pass = enumValues.find(received) | |
if (pass) { | |
return { | |
message: () => | |
`expected ${received} not to be member of enum ${enumeration}. | |
${enumeration} values: ${enumValues} | |
`, | |
pass: true, | |
}; | |
} else { | |
return { | |
message: () => | |
`expected ${received} to be member of enum ${enumeration}. | |
${enumeration} values: ${enumValues} | |
`, | |
pass: false, | |
}; | |
} | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment