Какой лучший способ для Mock вызвать aws ssm в NodeJS? - PullRequest
0 голосов
/ 15 марта 2020

Я пытаюсь смоделировать вызов службы ssm на aws:

const ssm = require("../awsclients/aws-client");

const getSecret = async secretName => {
  // eslint-disable-next-line no-console
  console.log(`Getting secret for ${secretName}`);
  const params = {
    Name: secretName,
    WithDecryption: true
  };

  const result = await ssm.getParameter(params).promise();
  return result.Parameter.Value;
};

module.exports = { getSecret };

Любое предложение, я новичок в nodeJS с AWS -Lambdas. ?

1 Ответ

1 голос
/ 16 марта 2020

Вы можете использовать библиотеку заглушек / макетов, например sinon. js.

Например,

index.js:

const ssm = require('./aws-client');

const getSecret = async (secretName) => {
  // eslint-disable-next-line no-console
  console.log(`Getting secret for ${secretName}`);
  const params = {
    Name: secretName,
    WithDecryption: true,
  };

  const result = await ssm.getParameter(params).promise();
  return result.Parameter.Value;
};

module.exports = { getSecret };

aws-client.js:

module.exports = {
  getParameter() {
    return this;
  },
  async promise() {
    return 'real value';
  },
};

index.test.js:

const { getSecret } = require('./');
const ssm = require('./aws-client');
const sinon = require('sinon');
const { expect } = require('chai');

describe('60695567', () => {
  it('should get secret', async () => {
    const promiseStub = sinon.stub().resolves({ Parameter: { Value: '123' } });
    sinon.stub(ssm, 'getParameter').callsFake(() => ({
      promise: promiseStub,
    }));
    const actual = await getSecret('token');
    expect(actual).to.be.eq('123');
    sinon.assert.calledWithExactly(ssm.getParameter, { Name: 'token', WithDecryption: true });
    sinon.assert.calledOnce(promiseStub);
  });
});

Результаты модульных испытаний с отчетом о покрытии:

  60695567
Getting secret for token
    ✓ should get secret


  1 passing (25ms)

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |      80 |      100 |   33.33 |      80 |                   
 aws-client.js |   33.33 |      100 |       0 |   33.33 | 3,6               
 index.js      |     100 |      100 |     100 |     100 |                   
---------------|---------|----------|---------|---------|-------------------
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...