ChrisRTech
ChrisRTech

Reputation: 577

How to use sinon to stub aws-param-store getParametersByPath()

I'm using the package aws-param-store and I'm trying to write Unit tests that stub calls to getParametersByPath(). Package can be found here: https://www.npmjs.com/package/aws-param-store

Here is my sinon code to stub the call. The function getParametersByPath is an async function so I'm trying to return a resolved promise to stub it:

const awsParameterStore = require('aws-param-store');
const sinon = require("sinon");
let sandbox = sinon.createSandbox();

// In My Test:
let parms = new Map();
parms.set("key1","value1");
parms.set("key2","value2");  

sandbox.stub(awsParameterStore,'getParametersByPath').callsFake(async function(prefix){
    console.log("INSIDE STUB for getParametersByPath:" + prefix);       
    return Promise.resolve(parms);
});

My app makes a call to the function like this:

let parameters = await awsParameterStore.getParametersByPath("/foo");

However, instead of getting back the Map of dummy parameters, I get an empty object {}. I can see that the stub is getting called. Any ideas on how to properly stub this so I can return some dummy parameters in my unit tests? Thanks!

Upvotes: 0

Views: 337

Answers (2)

Lin Du
Lin Du

Reputation: 102207

Here is the unit test solution:

index.js:

const awsParameterStore = require('aws-param-store');

async function main() {
  let parameters = await awsParameterStore.getParametersByPath('/foo');
  return parameters;
}

module.exports = main;

index.test.js:

const main = require('./');
const sinon = require('sinon');
const awsParameterStore = require('aws-param-store');
const { expect } = require('chai');
let sandbox = sinon.createSandbox();

describe('59787603', () => {
  it('should pass', async () => {
    let parms = new Map();
    parms.set('key1', 'value1');
    parms.set('key2', 'value2');
    sandbox.stub(awsParameterStore, 'getParametersByPath').resolves(parms);

    const actual = await main();
    expect(actual).to.be.eql(parms);
  });
});

Unit test results with 100% coverage:

  59787603
    ✓ should pass


  1 passing (11ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.js |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

source code: https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/59787603

Upvotes: 0

elbik
elbik

Reputation: 1897

I tried to check your code, and it's completely fine.

import test from 'ava'

const awsParameterStore = require('aws-param-store');

const sinon = require('sinon')

test('test stub', async t => {
    let parms = new Map();
    parms.set("key1","value1");
    parms.set("key2","value2");
    parms.set("key3","value3");
    parms.set("key4","value4");

    sinon.stub(awsParameterStore, 'getParametersByPath').callsFake(async function(prefix){
        console.log("INSIDE STUB for getParametersByPath:" + prefix)
        return Promise.resolve(parms);
    })

    const res = await awsParameterStore.getParametersByPath('/some-prefix')

    console.log(JSON.stringify(res))

    res.forEach((value, key) => {
        console.log(`[${key}]= ${value}`)
    })

    t.true(true)
})

What is weir:

console.log - show that result is {}

but if you debug or log the value one by one, you can see that stub works fine:

Debugger attached.
INSIDE STUB for getParametersByPath:/some-prefix
{}
[key1]= value1
[key2]= value2
[key3]= value3
[key4]= value4
  ✔ test stub

UPD: problem is how to log Map by JSON.stringify(). So, you must log the map as:

console.log(JSON.stringify([...res]))

Upvotes: 1

Related Questions