Skip to content

Instantly share code, notes, and snippets.

@stared
Created February 13, 2020 23:17
Show Gist options
  • Save stared/bfcaad992a19d2e20b189549434d11aa to your computer and use it in GitHub Desktop.
Save stared/bfcaad992a19d2e20b189549434d11aa to your computer and use it in GitHub Desktop.
Example of extending jest matchers in TypeScript
export {}
declare global {
namespace jest {
interface Matchers<R> {
myMatcher: (received: string) => R
}
}
}
function myMatcher<T>(this: jest.MatcherUtils, received: string, expected: string): jest.CustomMatcherResult {
const pass = received === expected
return {
pass,
message: (): string => `expected ${received} to be ${expected}`,
}
}
expect.extend({
myMatcher,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment