Skip to content

Commit 1100076

Browse files
added test step to github workflow
1 parent 24de835 commit 1100076

File tree

13 files changed

+107
-147
lines changed

13 files changed

+107
-147
lines changed

.github/workflows/nodejs.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ jobs:
2424
- run: npm run build --if-present
2525
- run: npm run local
2626
env:
27-
DEBUG: INFO
27+
LOG_LEVEL: WARN
2828
NODE_ENV: production
2929
REGION: eu-west-1
3030
- run: npm run local
3131
env:
32-
DEBUG: DEBUG
32+
LOG_LEVEL: DEBUG
33+
NODE_ENV: production
34+
REGION: us-west-1
35+
- run: npm test
36+
env:
37+
LOG_LEVEL: ERROR
3338
NODE_ENV: production
3439
REGION: us-west-1
3540
- name: define version

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
.ONESHELL:
12
.EXPORT_ALL_VARIABLES:
3+
.PHONY: pre-commit changelog release test
24

3-
.PHONY: pre-commit changelog release
5+
SHELL := /bin/bash
46

57
help:
68
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@@ -17,4 +19,14 @@ changelog: ## Update changelog
1719
git-chglog -o CHANGELOG.md --next-tag `semtag final -s minor -o`
1820

1921
release: ## Create release version
20-
semtag final -s minor
22+
semtag final -s minor
23+
24+
validatejs: ## Validate solution
25+
@npm run dev:install
26+
@npm run format
27+
@npm run lint
28+
29+
test: LOG_LEVEL=ERROR
30+
test: ## Test solution
31+
$(if ${LOG_LEVEL},,$(error Must pass LOG_LEVEL=<DEBUG|INFO|WARN|ERROR>))
32+
@npm test

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
"scripts": {
77
"build": "tsc -p tsconfig.build.json",
88
"format": "prettier --write \"src/**/*.ts\"",
9-
"lint": "tslint --project tsconfig.json",
9+
"lint": "tslint --project tsconfig.build.json",
1010
"pretest": "npm run lint",
1111
"all": "npm run build && npm prune --production && rm -f dist/lambda.zip && zip -q -r dist/lambda.zip node_modules dist && npm ci",
12-
"testt": "mocha -r ts-node/register test/**/*.spec.ts",
12+
"test": "mocha --colors -r ts-node/register test/**/*.spec.ts",
1313
"start": "tsc && node dist/index.js",
1414
"local": "tsc -p tsconfig.build.json && npm run 001",
1515
"dev:install": "npm install --only=dev && npm install",
1616
"prod:install": "npm install --production",
17-
"001": "./node_modules/.bin/lambda-local -f dist/index -e '{\"json\":\"here\"}'"
17+
"001": "./node_modules/.bin/lambda-local -f dist/index -e test/fixtures/sns.event.json"
1818
},
1919
"keywords": [
2020
"aws",

src/config.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import * as env from 'env-var';
22

33
const NODE_ENV: string = env
4-
.get('NODE_ENV')
5-
.required()
4+
.get('NODE_ENV', 'development')
65
.asString();
76

87
const REGION: string = env
98
.get('REGION')
109
.required()
1110
.asString();
1211

13-
const DEBUG: string = env
14-
.get('DEBUG')
12+
const LOG_LEVEL: string = env
13+
.get('LOG_LEVEL')
1514
.required()
1615
.asString();
1716

18-
export { NODE_ENV, REGION, DEBUG };
17+
export { NODE_ENV, REGION, LOG_LEVEL };

src/index.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1+
import Log from '@dazn/lambda-powertools-logger';
12
import { Handler } from 'aws-lambda';
2-
import moment from 'moment';
33
import middy from 'middy';
4-
import Log from '@dazn/lambda-powertools-logger';
4+
import moment from 'moment';
55

6-
import { NODE_ENV } from './config';
6+
import { LambdaResponse } from './utils';
77

88
Log.debug('lambda executing...');
99

10-
export const handler: Handler = middy(
11-
async (event: any): Promise<any> => {
12-
Log.info(`Event: ${JSON.stringify(event)}.`);
13-
let hw: string = 'Hello Dummy Function';
14-
Log.info(`${hw}. Environment: ${NODE_ENV}`);
15-
Log.info(
16-
`event ${JSON.stringify(event)}`,
17-
);
10+
export const lambdahandler: Handler = async (event: any): Promise<LambdaResponse> => {
11+
Log.debug(`Event: ${ JSON.stringify(event) }.`);
12+
const hw: string = 'Dummy Function';
13+
Log.debug(`${ hw }`);
14+
15+
return {
16+
statusCode: 200,
17+
body: JSON.stringify({
18+
time: moment.utc().toISOString(),
19+
records: event.Records.length,
20+
}),
21+
};
22+
};
1823

19-
return {
20-
statusCode: 200,
21-
body: JSON.stringify({
22-
time: moment.utc().toISOString(),
23-
}),
24-
};
25-
},
26-
);
24+
export const handler: Handler = middy(lambdahandler);

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
interface Response {
1+
interface LambdaResponse {
22
statusCode: number;
33
body: string;
44
}
55

66
export {
7-
Response
7+
LambdaResponse,
88
};

test/fixtures/defait.message.event.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/fixtures/docs.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/fixtures/sns.event.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:region:account:sns-name:uuid",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "",
10+
"TopicArn": "arn:aws:sns:region:account:sns-name",
11+
"Subject": "ALARM: \"cpu-utilization-low\" in US West (N. California)",
12+
"Message": "{ \"AlarmName\":\"cpu-utilization-low\" }",
13+
"Timestamp": "2020-01-19T09:09:41.459Z",
14+
"SignatureVersion": "1",
15+
"Signature": "",
16+
"SigningCertUrl": "",
17+
"UnsubscribeUrl": "",
18+
"MessageAttributes": {}
19+
}
20+
}
21+
]
22+
}

test/handler.non.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)