Jest is a popular JavaScript testing framework that provides a variety of powerful features for testing your code. One such feature is the spyOn method, which allows you to create a spy on a specific method of an object.
In this article, we’ll take a look at how to use the spyOn method, using the following code example as a guide:
//code to be tested
private generateExpirationTime() {
return new Date(Date.now() + 60 * 60 * 1000);
}
private generateRandomTokenId() {
return Math.random().toString(36).slice(2);
}
//the test
test.only('should return sessionToken for valid credentials', async() => {
jest.spyOn(global.Math, 'random').mockReturnValueOnce(0);
jest.spyOn(global.Date, 'now').mockReturnValueOnce(0);
UserCredentialsDbAccessMock.getUserCredential.mockResolvedValueOnce({
username:'someUser',
accessRights:[1,2,3]
})
const expectedSessionToken:SessionToken ={
userName:'someUser',
accessRights:[1,