Skip to content

Commit

Permalink
Merge pull request #91 from alexcasalboni/proper-mocking-with-sinon-t…
Browse files Browse the repository at this point in the history
…est-utils

Refactor utils test with sinon
  • Loading branch information
alexcasalboni committed Jun 10, 2020
2 parents 0410445 + ec21652 commit 267620f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion test/unit/test-lambda.js
Expand Up @@ -123,7 +123,7 @@ describe('Lambda Functions', async() => {
});

afterEach('Global mock utilities afterEach', () => {
// restore everything to it's natural order
// restore everything to its natural order
sandBox.restore();
});

Expand Down
66 changes: 36 additions & 30 deletions test/unit/test-utils.js
@@ -1,8 +1,8 @@
'use strict';

const sinon = require('sinon');
const expect = require('expect.js');

// const AWS = require('aws-sdk');
var AWS = require('aws-sdk-mock');

process.env.sfCosts = `{"us-gov-west-1": 0.00003,"eu-north-1": 0.000025,
Expand All @@ -18,6 +18,8 @@ process.env.AWS_REGION = 'af-south-1';

const utils = require('../../lambda/utils');

const sandBox = sinon.createSandbox();

// AWS SDK mocks
AWS.mock('Lambda', 'getAlias', {});
AWS.mock('Lambda', 'getFunctionConfiguration', {MemorySize: 1024});
Expand All @@ -43,10 +45,6 @@ describe('Lambda Utils', () => {
utils.invokeLambdaWithProcessors,
];

// TODO fix me (use proper mocking in test-lambda.js)
const getLambdaPower = utils.getLambdaPower;
const invokeLambdaProcessor = utils.invokeLambdaProcessor;

// just returns the utility name for convenience
function _fname(func) {
const keys = Object.keys(utils);
Expand All @@ -68,6 +66,11 @@ describe('Lambda Utils', () => {
});
});

afterEach('Global mock utilities afterEach', () => {
// restore everything to its natural order
sandBox.restore();
});

describe('stepFunctionsCost', () => {
it('should return expected step base cost', () => {
process.env.sfCosts = '{"us-gov-west-1": 0.00003, "default": 0.000025}';
Expand Down Expand Up @@ -96,27 +99,29 @@ describe('Lambda Utils', () => {

describe('getLambdaPower', () => {
it('should return the memory value', async() => {
const value = await getLambdaPower('arn:aws:lambda:us-east-1:XXX:function:YYY');
const value = await utils.getLambdaPower('arn:aws:lambda:us-east-1:XXX:function:YYY');
expect(value).to.be(1024);
});
});

describe('verifyAliasExistance', () => {

it('should return true if the alias exists', async() => {
utils.getLambdaAlias = async() => {
return { FunctionVersion: '1' };
};
sandBox.stub(utils, 'getLambdaAlias')
.callsFake(async() => {
return { FunctionVersion: '1' };
});
const aliasExists = await utils.verifyAliasExistance('arnOK', 'aliasName');
expect(aliasExists).to.be(true);
});

it('should return false if the alias does not exists', async() => {
utils.getLambdaAlias = async() => {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
throw error;
};
sandBox.stub(utils, 'getLambdaAlias')
.callsFake(async() => {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
throw error;
});
const aliasExists = await utils.verifyAliasExistance('arnOK', 'aliasName');
expect(aliasExists).to.be(false);
});
Expand Down Expand Up @@ -319,35 +324,36 @@ describe('Lambda Utils', () => {
});

describe('invokeLambdaProcessor', () => {
// TODO proper mocking

var invokeLambdaCounter;
beforeEach('mock API call', () => {
invokeLambdaCounter = 0;
});

it('should invoke the processing function', async() => {
utils.invokeLambda = async(_arn, _alias, payload) => {
invokeLambdaCounter++;
return {
Payload: '{"OK": "OK"}',
};
};
const data = await invokeLambdaProcessor('arnOK', {});
sandBox.stub(utils, 'invokeLambda')
.callsFake(async() => {
invokeLambdaCounter++;
return {
Payload: '{"OK": "OK"}',
};
});
const data = await utils.invokeLambdaProcessor('arnOK', {});
expect(invokeLambdaCounter).to.be(1);
expect(data).to.be('{"OK": "OK"}');
});

it('should explode if processor fails', async() => {
utils.invokeLambda = async(_arn, _alias, _payload) => {
invokeLambdaCounter++;
return {
Payload: '{"KO": "KO"}',
FunctionError: 'Unhandled',
};
};
sandBox.stub(utils, 'invokeLambda')
.callsFake(async() => {
invokeLambdaCounter++;
return {
Payload: '{"KO": "KO"}',
FunctionError: 'Unhandled',
};
});
try {
const data = await invokeLambdaProcessor('arnOK', {});
const data = await utils.invokeLambdaProcessor('arnOK', {});
expect(data).to.be(null);
} catch (ex) {
expect(ex.message.includes('failed with error')).to.be(true);
Expand Down

0 comments on commit 267620f

Please sign in to comment.