Skip to content

Commit

Permalink
test: add end-to-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 5, 2024
1 parent 653b871 commit 69e4f7e
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 148 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,38 @@ jobs:

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: "20.x"
cache: "npm"

- name: Authenticate with NPM registry
run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc

- name: Install dependencies
run: npm ci --no-audit
- name: Compile

- name: Check typescript
run: npx tsc

- name: Check source code with eslint
run: npx eslint .

- name: Check if source code is properly formatted
run: npx prettier -c ./

- name: Compile
run: npm run prepublishOnly

- name: Deploy
run: ./cli.js > http-api-mock.json

- name: End-to-end tests
run: npx tsx --test e2e.spec.ts

- name: Destroy
run: ./cli.js destroy `jq -r '.stackName' http-api-mock.json`

- name: Semantic release
run: npx semantic-release
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
npm-debug.log
dist/
cdk.out/
cdk.out/
http-api-mock.json
40 changes: 0 additions & 40 deletions cdk/resources/http-api-mock-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,6 @@ export const handler = async (
query !== undefined ? `?${query.toString()}` : ''
}`

console.log({
TableName: process.env.REQUESTS_TABLE_NAME,
Item: {
methodPathQuery: {
S: `${event.httpMethod} ${pathWithQuery}`,
},
timestamp: {
S: new Date().toISOString(),
},
requestId: {
S: context.awsRequestId,
},
method: {
S: event.httpMethod,
},
path: {
S: pathWithQuery,
},
resource: { S: resource },
query:
query === undefined
? { NULL: true }
: {
M: [...query.entries()].reduce(
(o, [k, v]) => ({ ...o, [k]: v }),
{},
),
},
body: {
S: event.body ?? '{}',
},
headers: {
S: JSON.stringify(event.headers),
},
ttl: {
N: `${Math.round(Date.now() / 1000) + 5 * 60}`,
},
},
})

await db.send(
new PutItemCommand({
TableName: process.env.REQUESTS_TABLE_NAME,
Expand Down
66 changes: 66 additions & 0 deletions e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { listRequests } from './src/requests.js'
import { registerResponse } from './src/responses.js'
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { readFile } from 'node:fs/promises'
import path from 'node:path'

const { responsesTableName, apiURL, requestsTableName } = JSON.parse(
await readFile(path.join(process.cwd(), 'http-api-mock.json'), 'utf-8'),
)

const db = new DynamoDBClient({})

void describe('end-to-end tests', () => {
void it('should respond with 404 if no response is configured', async () => {
const res = await fetch(new URL('/prod/foo', apiURL), {
method: 'POST',
body: JSON.stringify({ some: 'data' }),
headers: {
'content-type': 'application/json; charset=utf-8',
},
})
assert.equal(res.ok, false)
assert.equal(res.status, 404)
})

void it('should store all requests', async () => {
const pathSegment = crypto.randomUUID()
await fetch(new URL(`/prod/${pathSegment}`, apiURL))
const request = (await listRequests(db, requestsTableName)).find(
({ path }) => path === pathSegment,
)
assert.notEqual(request, undefined)
})

void it('should return a configured response', async () => {
const pathSegment = crypto.randomUUID()
await registerResponse(db, responsesTableName, {
method: 'PUT',
path: pathSegment,
queryParams: {
foo: 'bar',
},
body: [
`Content-Type: application/json`,
'',
JSON.stringify({ success: true }),
].join('\n'),
statusCode: 201,
})
const res = await fetch(
new URL(
`/prod/${pathSegment}?${new URLSearchParams({ foo: 'bar' }).toString()}`,
apiURL,
),
{
method: 'PUT',
},
)
assert.equal(res.ok, true)
assert.equal(res.status, 201)
assert.equal(res.headers.get('Content-Type'), 'application/json')
assert.deepEqual(await res.json(), { success: true })
})
})
Loading

0 comments on commit 69e4f7e

Please sign in to comment.