I have been trying to write unit test in javascript for the method which uses jwt token validation. So the results are fetched only if the token is valid.
I want to mock the jwt token and return results. Is there any way to do it ? I tried using ava test framework, mock require, sinon but I am unable to do it.
Any thoughts ?
Code:
I am trying to mock jwt.verify
**unit test:**
const promiseFn = Promise.resolve({ success: 'Token is valid' });
mock('jsonwebtoken', {
verify: function () {
return promiseFn;
}
});
const jwt = require('jsonwebtoken');
const data = jwt.verify(testToken,'testSecret');
console.log(data)
**Error :**
ERROR
{"name":"JsonWebTokenError","message":"invalid token"}
So the issue here is that, its actually verifying the token but not invoking the mock.