Skip to content

Commit ca1f9e3

Browse files
Merge pull request #4 from ikhvost/fix/testing-library
fix testing library
2 parents daf2da8 + 724f2d2 commit ca1f9e3

File tree

7 files changed

+96
-1229
lines changed

7 files changed

+96
-1229
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
---
21
name: "ci"
32

43
on:
54
push:
6-
branches:
7-
- master
5+
branches: ["master"]
86
pull_request:
9-
branches:
10-
- master
7+
types: [opened, synchronize]
8+
119
jobs:
1210
ci:
13-
runs-on: "ubuntu-latest"
11+
name: Build and Test
12+
runs-on: "ubuntu-latest"
1413

15-
steps:
16-
- uses: actions/checkout@v2
14+
steps:
15+
- name: Check out code
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 2
1719

18-
- name: Use Node.js
19-
uses: actions/setup-node@v1
20-
with:
21-
node-version: 14.18.0
20+
- name: Setup Node.js environment
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: 14
24+
cache: 'yarn'
2225

23-
- name: Install
24-
run: yarn
26+
- name: Install dependencies
27+
run: yarn
2528

26-
- name: Build
27-
run: yarn build
29+
- name: Build
30+
run: yarn build
2831

29-
- name: Lint
30-
run: yarn lint
32+
- name: Lint
33+
run: yarn lint
3134

32-
- name: Test
33-
run: yarn test
35+
- name: Test
36+
run: yarn test

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ yarn start
3737
```
3838

3939
### Testing
40-
Following template leverages testing on Jest framework with serverless jest plugin for running tests against serverless handlers.
40+
Following template leverages testing on Jest framework with serverless-plugin-test-helper for running tests against serverless handlers.
4141
```bash
4242
yarn test
4343
```
4444

4545
Code below shows example of test for validating health endpoint
4646
```typescript
4747
describe('handler', () => {
48-
const endpoint = lambdaWrapper.wrap(handler, { handler: 'health' })
49-
5048
test('returns 200 HTTP', async () => {
51-
const response = await endpoint.run({})
49+
const handler = health(new ApiGatewayEvent(), context, jest.fn())
5250

53-
expect(response.statusCode).toEqual(StatusCodes.OK)
54-
expect(response.body).toEqual(ReasonPhrases.OK)
51+
await expect(handler).resolves.toMatchObject({
52+
body: ReasonPhrases.OK,
53+
statusCode: StatusCodes.OK,
54+
})
5555
})
5656
})
5757

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"@middy/validator": "^2.5.2",
1919
"@types/aws-lambda": "^8.10.84",
2020
"@types/jest": "^27.0.2",
21-
"@types/serverless-jest-plugin": "^0.3.0",
2221
"@typescript-eslint/eslint-plugin": "^5.1.0",
2322
"@typescript-eslint/parser": "^5.1.0",
2423
"aws-lambda": "^1.0.6",
@@ -39,7 +38,7 @@
3938
"reflect-metadata": "^0.1.13",
4039
"serverless": "^2.64.1",
4140
"serverless-bundle": "^5.0.2",
42-
"serverless-jest-plugin": "^0.4.0",
41+
"serverless-plugin-test-helper": "^2.6.4",
4342
"serverless-offline": "^8.2.0",
4443
"ts-jest": "^27.0.7",
4544
"typesafe-api-gateway": "^1.0.0",

serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package:
66

77
plugins:
88
- serverless-bundle
9-
- serverless-jest-plugin
9+
- serverless-plugin-test-helper
1010
- serverless-offline
1111

1212
custom:

tests/foo.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lambdaWrapper } from 'serverless-jest-plugin'
1+
import { context } from 'serverless-plugin-test-helper'
22
import { StatusCodes } from 'http-status-codes'
33
import type { APIGatewayProxyEvent } from 'aws-lambda'
44

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

1313
describe('POST /foo', () => {
14-
const endpoint = lambdaWrapper.wrap({ foo }, { handler: 'foo' })
15-
1614
test('returns 400 HTTP', async () => {
17-
const response = await endpoint.run(proxyEventFactory({ invalid: 'invalid' }))
15+
const event = proxyEventFactory({ invalid: 'invalid' })
16+
const handler = foo(event, context, jest.fn())
1817

19-
expect(response.statusCode).toEqual(StatusCodes.BAD_REQUEST)
20-
expect(response.body).toEqual('Event object failed validation')
18+
await expect(handler).resolves.toMatchObject({
19+
body: 'Event object failed validation',
20+
statusCode: StatusCodes.BAD_REQUEST,
21+
})
2122
})
2223

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

26-
expect(response.statusCode).toEqual(StatusCodes.OK)
28+
await expect(handler).resolves.toMatchObject({
29+
statusCode: StatusCodes.OK,
30+
})
2731
})
2832
})

tests/health.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { lambdaWrapper } from 'serverless-jest-plugin'
21
import { StatusCodes, ReasonPhrases } from 'http-status-codes'
3-
import { APIGatewayProxyEvent } from 'aws-lambda'
2+
import { context, ApiGatewayEvent } from 'serverless-plugin-test-helper'
43

5-
import * as handler from '../src/health'
4+
import { health } from '../src/health'
65

76
describe('GET /health', () => {
8-
const endpoint = lambdaWrapper.wrap(handler, { handler: 'health' })
9-
107
test('returns 200 HTTP', async () => {
11-
const response = await endpoint.run({} as unknown as APIGatewayProxyEvent)
8+
const handler = health(new ApiGatewayEvent(), context, jest.fn())
129

13-
expect(response.statusCode).toEqual(StatusCodes.OK)
14-
expect(response.body).toEqual(ReasonPhrases.OK)
10+
await expect(handler).resolves.toMatchObject({
11+
body: ReasonPhrases.OK,
12+
statusCode: StatusCodes.OK,
13+
})
1514
})
1615
})

0 commit comments

Comments
 (0)