Skip to content

Instantly share code, notes, and snippets.

@cdanielsen
Created December 18, 2020 05:16
Show Gist options
  • Save cdanielsen/a1db3c11621373e94123dbcd1f7b83d1 to your computer and use it in GitHub Desktop.
Save cdanielsen/a1db3c11621373e94123dbcd1f7b83d1 to your computer and use it in GitHub Desktop.
ts-custom-matcher
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