107 lines
2.9 KiB
JavaScript

'use strict';
const expect = require('chai').expect;
const AwsCompileFunctions = require('../awsCompileFunctions');
const Serverless = require('../../../Serverless');
describe('awsCompileFunctions', () => {
let serverless;
let awsCompileFunctions;
const functionsObjectMock = {
first: {
name_template: 'name-template-name',
handler: 'first.function.handler',
provider: {
aws: {
timeout: 6,
memorySize: 1024,
runtime: 'nodejs4.3',
},
},
},
second: {
name_template: 'name-template-name',
handler: 'second.function.handler',
provider: {
aws: {
timeout: 6,
memorySize: 1024,
runtime: 'nodejs4.3',
},
},
},
};
const compiledFunctionResourcesArrayMock = [
{
first: {
Type: 'AWS::Lambda::Function',
Properties: {
Code: {
S3Bucket: 'new-service-dev-aws_useast1',
S3Key: '',
},
FunctionName: 'new-service-first',
Handler: 'first.function.handler',
MemorySize: 1024,
Role: { 'Fn::GetAtt': ['IamRoleLambda', 'Arn'] },
Runtime: 'nodejs4.3',
Timeout: 6,
},
},
},
{
second: {
Type: 'AWS::Lambda::Function',
Properties: {
Code: {
S3Bucket: 'new-service-dev-aws_useast1',
S3Key: '',
},
FunctionName: 'new-service-second',
Handler: 'second.function.handler',
MemorySize: 1024,
Role: { 'Fn::GetAtt': ['IamRoleLambda', 'Arn'] },
Runtime: 'nodejs4.3',
Timeout: 6,
},
},
},
];
beforeEach(() => {
serverless = new Serverless();
serverless.init();
awsCompileFunctions = new AwsCompileFunctions(serverless);
awsCompileFunctions.serverless.service.functions = functionsObjectMock;
awsCompileFunctions.serverless.service.service = 'new-service';
});
describe('#compileFunctions()', () => {
it('should throw an error if the stage option is not given', () => {
expect(() => awsCompileFunctions.compileFunctions()).to.throw(Error);
});
it('should throw an error if the region option is not given', () => {
expect(() => awsCompileFunctions.compileFunctions()).to.throw(Error);
});
it('should create corresponding function resources', () => {
const options = { stage: 'dev', region: 'aws_useast1' };
awsCompileFunctions.compileFunctions(options);
expect(
JSON.stringify(awsCompileFunctions.serverless.service.compiledFunctionResources[0])
).to.equal(
JSON.stringify(compiledFunctionResourcesArrayMock[0])
);
expect(
JSON.stringify(awsCompileFunctions.serverless.service.compiledFunctionResources[1])
).to.equal(
JSON.stringify(compiledFunctionResourcesArrayMock[1])
);
});
});
});