出于测试目的,我需要模拟 jwt-decode 函数,但我在这里找到的建议都没有帮助。使用 jwtDecode 的代码如下所示
import jwtDecode from 'jwt-decode';
...
const { exp } = jwtDecode(accessToken);
Run Code Online (Sandbox Code Playgroud)
在测试中我需要模拟这个返回exp值。我尝试按照Mock jwt-decode in Jest中的建议来模拟它
jest.mock('jwt-decode', () => () => ({ exp: 123456 }));
const { exp } = jwtDecode('123456');
Run Code Online (Sandbox Code Playgroud)
但这会返回
InvalidTokenError:指定的令牌无效:无法读取未定义的属性“替换”