I try to mock with jest the verify function of the npm module jsonwebtoken. The function return a decoded token but i want to pass a custom return of this function my unit test.
I made express request that check the validity of access tokent before proceed to request. But I want to mock the moment of the token check to return directly the user value. And pass easily this step. I put you the concern part of code.
But typescript send me this error: Property 'mockReturnValue' does not exist on type '{ (token: string, secretOrPublicKey: Secret, options?: VerifyOptions | undefined): string | object; (token: string, secretOrPublicKey: string | Buffer | { key: string | Buffer; passphrase: string; } | GetPublicKeyOrSecret, callback?: VerifyCallback | undefined): void; (token: string, secretOrPublicKey: string | ... ...'.
So the mock isn't working and I don't understant. I follow the mock axios step on Jest.io but it's didn't seem apply to jsonwebtoken.
Is everyone know what is the problem or how to mock this jsonwebtoken module on jest ?
users.test.ts
import jwt from 'jsonwebtoken'
jest.mock('jwt')
jwt.verify.mockReturnValue({
userId: String(member._id),
email: String(member.email),
permissionLevel: member.permissionLevel,
username: String(member.username),
})
describe('### /GET users', () => {
it('it should return 200 (Users List)', async (done) => {
const res = await request(app).set('Authorization', 'Bearer').get('/users')
expect(res.status).toBe(200)
})
})
Validation.ts
public isAccessTokenValid = (req: Request, res: Response, next: NextFunction): void => {
if (req.cryptedAccessToken) {
try {
req.accessToken = jwt.verify(req.cryptedAccessToken, ACCESS_TOKEN_SECRET)
next()
} catch (err) {
res.status(498).send({ error: err.message })
}
} else res.status(401).send({ error: 'cryptedAccessToken field not present in request' })
}
Best regards