gpt4 book ai didi

typescript - 如何使用 Jest 和 TypeScript 正确 stub /模拟 AWS SecretManager

转载 作者:行者123 更新时间:2023-12-05 04:55:40 29 4
gpt4 key购买 nike

我正在尝试使用 Jest 对 aws-sdk SecretsManager 进行单元测试并遇到一些问题,我有一个简单的缓存客户端

import { SecretsManager } from 'aws-sdk';
import { SecretsManagerIntegrationError } from '../errors';
const cache = new Map();

interface SecretsManagerConfig {
region?: string;
}

export class SecretsManagerClient {
secretsManager: SecretsManager;

constructor(config: SecretsManagerConfig) {
this.secretsManager = new SecretsManager(config);
}

async getSecretFromSecretsManager(secretName: string) {
try {
const params = { SecretId: secretName };
const data = await this.secretsManager.getSecretValue(params).promise();

if (data.SecretString) {
return JSON.parse(data.SecretString);
} else {
return Buffer.from(data.SecretBinary as any, 'base64').toString('ascii');
}
} catch (error) {
throw new SecretsManagerIntegrationError(`Error retrieving secrets: ${error}`);
}
}

async getSecret(secretName: string) {
if (!cache.get(secretName)) {
const secret = await this.getSecretFromSecretsManager(secretName);
cache.set(secretName, secret);
return secret;
}

return cache.get(secretName);
}
}

在我的测试文件中,我已经这样 mock 了 sdk.. 看起来很冗长,而且 sinon 处理的东西非常直接,这个断言失败了,我不确定为什么..

import { SecretsManagerClient } from '../../src/clients/secretsManagerClient';

jest.mock('aws-sdk', () => {
return {
SecretsManager: jest.fn(() => ({
getSecretValue: jest.fn(() => ({
promise: jest.fn(() => ({
SecretString: JSON.stringify('cool-secrets-here'),
})),
})),
})),
};
});

describe('SecretsManagerClient', () => {
let secretsManager: SecretsManagerClient;

beforeEach(() => {
secretsManager = new SecretsManagerClient({});
});

describe('when a cached value does not exist', () => {
it('queries the secretsManager', async () => {
const getSecretFromSecretsManager = jest.spyOn(secretsManager, 'getSecretFromSecretsManager');

await secretsManager.getSecret('cool-secret');

expect(getSecretFromSecretsManager).toHaveBeenCalledTimes(1);
});
});

<!-- fails --->
describe('when a cached value exists', () => {
it('uses the cached value', async () => {
const getSecretFromSecretsManager = jest.spyOn(secretsManager, 'getSecretFromSecretsManager');

await secretsManager.getSecret('cool-secret');
await secretsManager.getSecret('cool-secret');

expect(getSecretFromSecretsManager).toHaveBeenCalledTimes(1);
});
});
});

最佳答案

我已经使用 sinon stub 实现了对 AWS 服务的模拟。代码示例如下。

import AWS from 'aws-sdk'
import sinon from 'sinon'

let sinonSandbox

const beforeEach = (done) => {
sinonSandbox = sinon.sandbox.create()
done()
}

const afterEach = done => {
sinonSandbox.restore()
done()
}
lab.test('test name', (done) => {
sinonSandbox.stub(AWS, 'SecretsManager')
.returns({
getSecretValue: () => {
return {
SecretValue: 'Secret-Value'
}
}
})
done()
})

您还可以创建一个 aws-sdk-mock npm 模块来模拟所有 AWS SDK 服务和方法。

它真的很容易使用。只需使用服务、方法和 stub 函数调用 AWS.mock。

AWS.mock('SecretsManager', 'getSecretValue', function(params, callback) {
callback(null, 'success');
});

然后在测试后通过调用恢复方法:

AWS.restore('SecretsManager', 'getSecretValue');

赛农链接:

Sinon Stub Latest Version

stub-promise-aws-sdk-node-js

aws-sdk-mock 链接:

aws-sdk-mock

https://www.npmjs.com/package/aws-sdk-mock

关于typescript - 如何使用 Jest 和 TypeScript 正确 stub /模拟 AWS SecretManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65297707/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com