sls-dev-tools
Version:
The Dev Tools for the Serverless World
185 lines (168 loc) • 5.96 kB
JavaScript
"use strict";
var _fs = _interopRequireDefault(require("fs"));
var _helpers = require("../helpers");
var _serverlessConfigParser = _interopRequireDefault(require("../serverlessConfigParser"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const TEST_YAML_FILE_EMPTY = "";
const TEST_YAML_FILE = `
service:
name: test-\${opt:testArg1}
provider:
name: aws
runtime: nodejs10.x
region: eu-west-1
stage: \${opt:stage, 'dev'}
functions:
hello:
handler: hello.main
memorySize: 1024
timeout: 6
events:
- http:
method: get
path: hello
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
`;
const TEST_YAML_DEFAULT_STACKNAME = `
service: test-\${opt:testArg1}
provider:
name: aws
runtime: nodejs10.x
region: eu-west-1
stage: \${opt:stage, 'dev'}
`;
const TEST_YAML_FILE_REF = `
service:
name: test-\${opt:testArg1}
provider:
name: aws
runtime: nodejs10.x
region: eu-west-1
stage: \${opt:stage, 'dev'}
functions:
hello:
handler: hello.main
memorySize: 1024
timeout: 6
events:
- http:
method: get
path: hello
authorizer:
type: COGNITO_USER_POOLS
authorizerId: !Ref ApiGatewayAuthorizer
`; // eslint-disable-next-line no-template-curly-in-string
const STACK_NAME_OPT = "test-${opt:testArg1}"; // eslint-disable-next-line no-template-curly-in-string
const STACK_NAME_MANY_OPTS = "test-${opt:testArg1}-${opt:testArg2}";
const STACK_NAME_MANY_NO_OPTS = "test-backend-arg2";
const STACK_NAME_NO_OPT = "test-backend";
const CMD_VAR_1 = "--testArg1";
const CMD_VAL_1 = "backend";
const CMD_VAR_2 = "-testArg2";
const CMD_VAL_2 = "arg2";
const INVALID_ARGUMENT_EXIT_CODE = 9;
const setupConfigParser = yamlFileString => {
_fs.default.readFileSync.mockReturnValue(yamlFileString);
_fs.default.existsSync.mockReturnValue(true);
const program = {
args: [CMD_VAR_1, CMD_VAL_1],
location: "~/Dev/testProject/backend"
};
return new _serverlessConfigParser.default(program);
};
jest.mock("fs");
describe("Serverless Config Options", () => {
/*
* Transform Args
*/
it("should transform 1 arg (double dash) to dict", () => {
const args = [CMD_VAR_1, CMD_VAL_1];
const result = {
testArg1: CMD_VAL_1
};
expect((0, _helpers.transformArgsToDict)(args)).toStrictEqual(result);
});
it("should transform 1 arg (single dash) to dict", () => {
const args = [CMD_VAR_2, CMD_VAL_2];
const result = {
testArg2: CMD_VAL_2
};
expect((0, _helpers.transformArgsToDict)(args)).toStrictEqual(result);
});
it("should not remove dashes from middle of option in service name", () => {
const args = ["--env-name", CMD_VAL_1];
const result = {
"env-name": CMD_VAL_1
};
expect((0, _helpers.transformArgsToDict)(args)).toStrictEqual(result);
});
it("should transform multiple args (mixed dash) to dict", () => {
const args = [CMD_VAR_1, CMD_VAL_1, CMD_VAR_2, CMD_VAL_2];
const result = {
testArg1: CMD_VAL_1,
testArg2: CMD_VAL_2
};
expect((0, _helpers.transformArgsToDict)(args)).toStrictEqual(result);
});
/*
* Replace service.name Opt
*/
it("should replace the service name option with stored option", () => {
const testOptions = {
testArg1: CMD_VAL_1
};
expect((0, _helpers.replaceStacknameOpt)(STACK_NAME_OPT, testOptions)).toBe(STACK_NAME_NO_OPT);
});
it("should replace multiple service name option with their respective stored options", () => {
const testOptions = {
testArg1: CMD_VAL_1,
testArg2: CMD_VAL_2
};
expect((0, _helpers.replaceStacknameOpt)(STACK_NAME_MANY_OPTS, testOptions)).toBe(STACK_NAME_MANY_NO_OPTS);
});
it("should not replace the stack name where there is not opt variable in the YAML configuration file", () => {
const testOptions = {
testArg1: CMD_VAL_1
};
expect((0, _helpers.replaceStacknameOpt)(STACK_NAME_NO_OPT, testOptions)).toBe(STACK_NAME_NO_OPT);
});
it("should output an error message when an opt varibale exists in serverless configuration but no option is passed", () => {
console.error = jest.fn();
process.exit = jest.fn();
(0, _helpers.replaceStacknameOpt)(STACK_NAME_OPT, []);
expect(console.error).toHaveBeenCalledWith(_helpers.CYAN_STRING_FORMAT, `Your project requires stack name option ${CMD_VAR_1} to be passed when starting sls-dev-tools`);
expect(process.exit).toHaveBeenCalledWith(INVALID_ARGUMENT_EXIT_CODE);
});
});
describe("Serverless Config Parsing", () => {
it("should read YAML file and replace opt in service.name with stored arg", () => {
const SLS = setupConfigParser(TEST_YAML_FILE);
expect(SLS.getStackName("dev")).toBe("test-backend-dev");
});
it("should get a stackname given a stage", () => {
const SLS = setupConfigParser(TEST_YAML_FILE);
const stage = "test";
expect(SLS.getStackName(stage)).toBe("test-backend-test");
});
it("should continue as normal when YAML file is empty", () => {
const SLS = setupConfigParser(TEST_YAML_FILE_EMPTY);
expect(SLS.getStage()).toBe("dev");
expect(SLS.getStackName("dev")).toBe(null);
expect(SLS.getRegion()).toBe(null);
});
it(`should parse the stack name from a serverless config file when a default
value for 'service' (not service.name) is used`, () => {
const SLS = setupConfigParser(TEST_YAML_DEFAULT_STACKNAME);
const stage = "test";
expect(SLS.getStackName(stage)).toBe("test-backend-test");
});
it("should continue as normal when the config contains AWS intrinsic function tags", () => {
const SLS = setupConfigParser(TEST_YAML_FILE_REF);
expect(SLS.getStage()).toBe("dev");
expect(SLS.getStackName("dev")).toBe("test-backend-dev");
expect(SLS.getRegion()).toBe("eu-west-1");
});
});