Skip to content

Commit a591fc8

Browse files
committed
Add tests
1 parent d428a49 commit a591fc8

File tree

9 files changed

+112
-5
lines changed

9 files changed

+112
-5
lines changed

.eslintrc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"es6": true
66
},
77
"parserOptions": {
8-
"sourceType": "module"
8+
"sourceType": "module",
9+
"ecmaVersion": 2019
910
},
1011
"extends": "eslint:recommended",
1112
"rules": {
@@ -22,6 +23,18 @@
2223
"indent": [
2324
"error",
2425
2
26+
],
27+
"semi": [
28+
"error",
29+
"always"
30+
],
31+
"object-curly-spacing": [
32+
"error",
33+
"always"
34+
],
35+
"array-bracket-spacing": [
36+
"error",
37+
"never"
2538
]
2639
}
2740
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.nyc_output/
12
.vscode/
3+
coverage/
24
dist/
35
node_modules/

.mocharc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"spec": "test/**/*.spec.js",
3+
"slow": 10,
4+
"timeout": 50,
5+
"require": "@babel/register"
6+
}

.nycrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"all": true,
3+
"check-coverage": true,
4+
"lines": 95,
5+
"statements": 95,
6+
"functions": 95,
7+
"branches": 90,
8+
"include": [
9+
"src/**/*.js"
10+
],
11+
"reporter": [
12+
"lcov",
13+
"text"
14+
]
15+
}

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "babel --out-dir dist src",
88
"lint": "eslint .",
99
"format": "npm run lint -s -- --fix",
10-
"test": "npm run lint -s"
10+
"test": "npm run lint -s && nyc mocha --colors"
1111
},
1212
"repository": {
1313
"type": "git",
@@ -35,7 +35,14 @@
3535
"@babel/cli": "^7.10.5",
3636
"@babel/core": "^7.11.4",
3737
"@babel/preset-env": "^7.11.0",
38+
"@babel/register": "^7.10.5",
3839
"babel-plugin-add-module-exports": "^1.0.2",
39-
"eslint": "^7.7.0"
40+
"chai": "^4.2.0",
41+
"eslint": "^7.7.0",
42+
"eslint-plugin-chai-friendly": "^0.6.0",
43+
"eslint-plugin-mocha": "^8.0.0",
44+
"mocha": "^8.1.1",
45+
"nyc": "^15.1.0",
46+
"sinon": "^9.0.3"
4047
}
4148
}

src/functionValuePlugin.js renamed to src/function-value-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class FunctionValuePlugin {
55
this.variableResolvers = {
66
'fn.arn': (value) => this.getFunctionARNStatement(value),
77
'fn.name': (value) => this.getFunctionNameStatement(value)
8-
}
8+
};
99
}
1010

1111
getFunctionLogicalId(value) {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { FunctionValuePlugin } from './functionValuePlugin';
1+
import { FunctionValuePlugin } from './function-value-plugin';
22

33
export default FunctionValuePlugin;

test/.eslintrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"env": {
3+
"mocha": true
4+
},
5+
"plugins": [
6+
"mocha",
7+
"chai-friendly"
8+
],
9+
"parserOptions": {
10+
"sourceType": "module"
11+
},
12+
"rules": {
13+
"mocha/no-identical-title": [
14+
"error"
15+
],
16+
"chai-friendly/no-unused-expressions": [
17+
"error"
18+
]
19+
}
20+
}

test/function-value-plugin.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Plugin from '../src/index';
2+
import sinon from 'sinon';
3+
import { expect } from 'chai';
4+
5+
describe('plugin', () => {
6+
const functionName = 'health';
7+
const logicalId = 'HealthLambdaFunction';
8+
let variableResolvers;
9+
10+
before(() => {
11+
const service = { getAllFunctions: sinon.stub().returns([functionName]) };
12+
const provider = {
13+
naming: { getLambdaLogicalId: sinon.stub().returns(logicalId) }
14+
};
15+
const serverless = {
16+
service,
17+
getProvider: sinon.stub().returns(provider)
18+
};
19+
20+
variableResolvers = new Plugin(serverless).variableResolvers;
21+
});
22+
23+
[
24+
{ type: 'arn', expected: `!GetAtt ${logicalId}.ARN` },
25+
{ type: 'name', expected: `!Ref ${logicalId}` }
26+
].forEach(test => {
27+
it(`will generate function ${test.type} snippet`, async () => {
28+
const resolver = `fn.${test.type}`;
29+
const value = `${resolver}:${functionName}`;
30+
const result = await variableResolvers[resolver](value);
31+
32+
expect(result).to.equal(test.expected);
33+
});
34+
});
35+
36+
it('will fail if function not found', async () => {
37+
try {
38+
await variableResolvers['fn.arn']('fn.arn:test');
39+
throw new Error('should not get here');
40+
} catch {
41+
// success!
42+
}
43+
});
44+
});

0 commit comments

Comments
 (0)