小编kub*_*lpl的帖子

如何在玩笑中模拟重载方法?

我正在使用 jsonwebtoken 库来验证模块中的令牌。jsonwebtoken 多次导出 verify 方法(重载)。

export function verify(token: string, secretOrPublicKey: Secret, options?: VerifyOptions): object | string;

export function verify(
    token: string,
    secretOrPublicKey: Secret | GetPublicKeyOrSecret,
    callback?: VerifyCallback,
): void;

export function verify(
    token: string,
    secretOrPublicKey: Secret | GetPublicKeyOrSecret,
    options?: VerifyOptions,
    callback?: VerifyCallback,
): void;

Run Code Online (Sandbox Code Playgroud)

我的模块:

private validateToken(token: string): void {
        const publicKeyToPem = this.convertPublicKeyToPEM(this.ssoPublicKey);
        try {
            this.decodedToken = jwt.verify(token, publicKeyToPem);
        } catch (e) {
            throw new Error(e);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试在单元测试中模拟验证方法。

    test('should return true if token is correct', () => …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js typescript jestjs

7
推荐指数
1
解决办法
4450
查看次数

标签 统计

jestjs ×1

node.js ×1

typescript ×1

unit-testing ×1