Skip to content

Instantly share code, notes, and snippets.

@jpmoura
Created August 12, 2021 23:50
Show Gist options
  • Save jpmoura/720171a4e5ded4bd8bc6ba69271d0b34 to your computer and use it in GitHub Desktop.
Save jpmoura/720171a4e5ded4bd8bc6ba69271d0b34 to your computer and use it in GitHub Desktop.
Mocking bcrypt library with Jest and TypeScript
// Using compare method as example
const bcryptCompare = jest.fn().mockRejectedValue(new Error('Random error'));
(bcrypt.compare as jest.Mock) = bcryptCompare;
//call method that uses bcrypt.compare with async
const bcryptCompare = jest.fn().mockResolvedValue(true);
(bcrypt.compare as jest.Mock) = bcryptCompare;
//call method that uses bcrypt.compare with async
@VicDosya
Copy link

Thank you!

@jpmoura
Copy link
Author

jpmoura commented Jan 13, 2023

You're welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment