Skip to content

Instantly share code, notes, and snippets.

View jpmoura's full-sized avatar
🏠
Working from home

Joao Pedro Santos de Moura jpmoura

🏠
Working from home
View GitHub Profile
@jpmoura
jpmoura / jest-mock-bcrypt-example.ts
Created August 12, 2021 23:50
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