Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
---
name: "ci"

on:
push:
branches:
- master
branches: ["master"]
pull_request:
branches:
- master
types: [opened, synchronize]

jobs:
ci:
runs-on: "ubuntu-latest"
name: Build and Test
runs-on: "ubuntu-latest"

steps:
- uses: actions/checkout@v2
steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 14.18.0
- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 14
cache: 'yarn'

- name: Install
run: yarn
- name: Install dependencies
run: yarn

- name: Build
run: yarn build
- name: Build
run: yarn build

- name: Lint
run: yarn lint
- name: Lint
run: yarn lint

- name: Test
run: yarn test
- name: Test
run: yarn test
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ yarn start
```

### Testing
Following template leverages testing on Jest framework with serverless jest plugin for running tests against serverless handlers.
Following template leverages testing on Jest framework with serverless-plugin-test-helper for running tests against serverless handlers.
```bash
yarn test
```

Code below shows example of test for validating health endpoint
```typescript
describe('handler', () => {
const endpoint = lambdaWrapper.wrap(handler, { handler: 'health' })

test('returns 200 HTTP', async () => {
const response = await endpoint.run({})
const handler = health(new ApiGatewayEvent(), context, jest.fn())

expect(response.statusCode).toEqual(StatusCodes.OK)
expect(response.body).toEqual(ReasonPhrases.OK)
await expect(handler).resolves.toMatchObject({
body: ReasonPhrases.OK,
statusCode: StatusCodes.OK,
})
})
})

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"@middy/validator": "^2.5.2",
"@types/aws-lambda": "^8.10.84",
"@types/jest": "^27.0.2",
"@types/serverless-jest-plugin": "^0.3.0",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"aws-lambda": "^1.0.6",
Expand All @@ -39,7 +38,7 @@
"reflect-metadata": "^0.1.13",
"serverless": "^2.64.1",
"serverless-bundle": "^5.0.2",
"serverless-jest-plugin": "^0.4.0",
"serverless-plugin-test-helper": "^2.6.4",
"serverless-offline": "^8.2.0",
"ts-jest": "^27.0.7",
"typesafe-api-gateway": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package:

plugins:
- serverless-bundle
- serverless-jest-plugin
- serverless-plugin-test-helper
- serverless-offline

custom:
Expand Down
20 changes: 12 additions & 8 deletions tests/foo.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lambdaWrapper } from 'serverless-jest-plugin'
import { context } from 'serverless-plugin-test-helper'
import { StatusCodes } from 'http-status-codes'
import type { APIGatewayProxyEvent } from 'aws-lambda'

Expand All @@ -11,18 +11,22 @@ const proxyEventFactory = (body: unknown): APIGatewayProxyEvent<{ body: Foo }> =
}

describe('POST /foo', () => {
const endpoint = lambdaWrapper.wrap({ foo }, { handler: 'foo' })

test('returns 400 HTTP', async () => {
const response = await endpoint.run(proxyEventFactory({ invalid: 'invalid' }))
const event = proxyEventFactory({ invalid: 'invalid' })
const handler = foo(event, context, jest.fn())

expect(response.statusCode).toEqual(StatusCodes.BAD_REQUEST)
expect(response.body).toEqual('Event object failed validation')
await expect(handler).resolves.toMatchObject({
body: 'Event object failed validation',
statusCode: StatusCodes.BAD_REQUEST,
})
})

test('returns 200 HTTP', async () => {
const response = await endpoint.run(proxyEventFactory({ bar: 'aaaa', foo: 'aaaaaaaaaaaaa' }))
const event = proxyEventFactory({ bar: 'aaaa', foo: 'aaaaaaaaaaaaa' })
const handler = foo(event, context, jest.fn())

expect(response.statusCode).toEqual(StatusCodes.OK)
await expect(handler).resolves.toMatchObject({
statusCode: StatusCodes.OK,
})
})
})
15 changes: 7 additions & 8 deletions tests/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { lambdaWrapper } from 'serverless-jest-plugin'
import { StatusCodes, ReasonPhrases } from 'http-status-codes'
import { APIGatewayProxyEvent } from 'aws-lambda'
import { context, ApiGatewayEvent } from 'serverless-plugin-test-helper'

import * as handler from '../src/health'
import { health } from '../src/health'

describe('GET /health', () => {
const endpoint = lambdaWrapper.wrap(handler, { handler: 'health' })

test('returns 200 HTTP', async () => {
const response = await endpoint.run({} as unknown as APIGatewayProxyEvent)
const handler = health(new ApiGatewayEvent(), context, jest.fn())

expect(response.statusCode).toEqual(StatusCodes.OK)
expect(response.body).toEqual(ReasonPhrases.OK)
await expect(handler).resolves.toMatchObject({
body: ReasonPhrases.OK,
statusCode: StatusCodes.OK,
})
})
})
Loading